Home  >  Article  >  Backend Development  >  $file=isset($file) && $file?$file:'index'这段代码什么意思?该怎么处理

$file=isset($file) && $file?$file:'index'这段代码什么意思?该怎么处理

WBOY
WBOYOriginal
2016-06-13 11:50:041173browse

$file=isset($file) && $file?$file:'index'这段代码什么意思?
$file=isset($file) && $file?$file:'index';
上面代码什么意思?最好能举个例子,上面实际执行了什么?
------解决方案--------------------
判断是否存在$file变量,如果存在则取值为$file 如果不存在则为index
------解决方案--------------------
? : 三目运算符
等价于
if(isset($file) && $file){
   $file=$file;
}else{
   $file='index';
}
------解决方案--------------------
? : 三元运算符 (?)问号前面是判断条件 如果条件为真则取:(冒号)前面的值 如果判断条件为假则取:(冒号)后面的值

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn