Home » Php » Foreach loop issues in php

Foreach loop issues in php

Posted by: admin July 12, 2020 Leave a comment

Questions:

Here is some code I have: (p just echos plus adds a newline)

foreach ($vanSteps as $k => $reqInfo) 
{
    p($k);
    if ('van' == $k) { p('The key is the van, continue'); continue; }//continue if we reached the part of the array where van is key
    //do stuff
}

and I’m getting this output:

0
The key is the van, continue
1
2
3
van
The key is the van, continue

Why does the if statement return true when the key is 0? This foreach loop handles logic that applies when the key == 0 (and any other key except if the key is ‘van’) and this messes up the logic because it’s return true when key is 0.

Any help?

Thank you.

How to&Answers:

Use === for this comparison. When PHP compares string and integer it first casts string to integer value and then does comparison.

See Comparison Operators in manual.

Answer:

In PHP 'van' == 0 is true. This is because when using == to compare a string and a number, the string is converted to a number (as described in the second link below); this makes the comparison internally become 0 == 0 which is of course true.

The suggested alternative for your needs, would be to use a strict equality comparison using ===.

See Comparison Operators and String conversion to numbers

Answer:

In PHP, when you compare 2 types, it has to convert them to the same type. In your case, you compare string with int.
Internally this gets converted to

if((int)'van'==0)....
and then
if((int)'van'==1)....

(int)’any possible string’ will be 0🙂 So you either have to manually convert the both values to the same type, or use === as a comparison operator, instead of the loose =.
An exception from this rule(as pointed out in the comments) would be if the string start with a number, or can be interpreted as a number in any way(1, 0002, -1 etc). In this case, the string would be interpreted as a number, diregarding the end of the non-numeric end-of-string

Take a look at http://php.net/manual/en/types.comparisons.php for more details.

Answer:

This works fine:

$array = array(0=>"a",1=>"b","van"=>"booya!");

function p($v){ echo "{$v}<br />"; }

foreach ($array as $k => $reqInfo) 
{
    p($k);
    if ('van' === $k) { p('The key is the van, continue'); continue; }//continue if we reached the part of the array where van is key
    //do stuff
}

Output:

0
1
van
The key is the van, continue

Note the ===.

Answer:

Read the Comparison with Various Types table
When one of the operand is number, the other operand is converted to number too. Since ‘van’ is non-numeric sting, it’s converted to 0. You should use === operator in the case, which also checks the variable type

Answer:

That’s becuase 'van' == 0 (true).

Instead, you should use 'van' === 0 (false).

In short, use === instead of ==.

Answer:

Its interpreting the 'van' as a boolean value (false) which 0 is equal to.

To check for exact matches in type and value in PHP you must use === instead of ==