This first loop executes when $cnt=3 and the other other than $cnt=3. Whatever the value of $cnt only the first loop executes whether $cnt=3 or $cnt==3.
$ary = explode(".", $string);
开发者_如何学Go$cnt = count($ary);
if ($cnt="3") {
//executes when cnt=3
$fnm = $d[0];
$fnxt = $d[1].".".$d[2];
} else {
//executes when anything other than when cnt=3
$fnm = $d[0];
$fnxt = $d[1];
}
I might be missing something here, what exactly wrong here?
Thanks Jean
You're missing an =
sign on the comparison. It should be:
if ($cnt == 3)
As it is, you're assigning 3 to $cnt
, and since the assignment operator returns its value, the test becomes if (3)
, which of course always succeeds.
NB: count()
returns an integer, which is why my version above compares against 3
rather than "3"
You are missing an "="
if ($cnt="3") { // This is an assignment, which returns true.
This should be:
if ($cnt == "3") { // This is a comparison.
$cnt="3"
assigns the value "3"
to $cnt
, and the expression as a whole evaluates to "3"
, which is true, which causes the if
block to always be executed. In order to test whether $cnt
is equal to "3"
, use the ==
operator: $cnt == "3"
.
精彩评论