超简单的PHP,输出什么?

WBOY
Release: 2016-06-20 12:49:56
Original
969 people have browsed it

<?php     if($i="")    {     echo "a";     }     else{     echo "b";     }
Copy after login


回复讨论(解决方案)

为什么输出b?

$i=""
把一个空串赋值给 $i 并返回该值给系统

echo 2; //out 2$a = 2;echo $a; //out 2echo $a = 2; //out 2
Copy after login
但 php 中空串视为 假
所以 if($i="") 将进入 else 分支,从而输出 b

相当于将空赋值给$i,然后再判断$i , 由于是空为假,所以就进了else分支。

你改成 if($i="1") 就会进入if分支了。

楼上正解,哈哈

if (1  == 1){//标示比较值}
= 只是赋值而已

if($i="");
表示先把""赋给$i

然后判断$i的值。
$i为空字符串,所以 if($i="") 为假,因此执行 else echo "b"; 

估计你应该是想 $i==""吧,判断$i是否为空字符串。

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!