Jump to content

foreach problem


theblazingangel

Recommended Posts

help! why does the text get printed when checking array entry 0? it doesnt for any of the other array entries and shouldnt ever be printed...

[code]<?php

$test = array("data1","data2","data3");
$rowNum = "none";

foreach ($test as $row => $data)
{
if ($data == "data1") { $rowNum = $row; }
}

if ($rowNum == "whatever") { echo "error"; }

?>[/code]

if you replace [i]if ($data == "data1")[/i] with [i]if ($data == "data2")[/i] it doesnt happen
nor if you replace [i]whatever[/i] with a number

software bug?
im using php v5.1.5 btw and it also happened on v5.1.4

thanks
Link to comment
https://forums.phpfreaks.com/topic/18132-foreach-problem/
Share on other sites

[code]<?php
$int = 0;
$str = 'whatever';
if($str == $int) {
echo ucwords(gettype($int)).' "'.$int.'" is equal to '.gettype($str).' "'.$str.'".<br />';
}
$int = 1;
if($str == $int) {
echo ucwords(gettype($int)).' "'.$int.'" is equal to '.gettype($str).' "'.$str.'".<br />';
}
$bool = false;
if($str == $bool) {
echo ucwords(gettype($bool)).' "'.$bool.'" is equal to '.gettype($str).' "'.$str.'".<br />';
}
$null = null;
if($str == $null) {
echo ucwords(gettype($null)).' "'.$null.'" is equal to '.gettype($str).' "'.$str.'".<br />';
}
?>[/code]

That prints only:[quote]Integer "0" is equal to string "whatever".[/quote]

I too now use 5.1.4, and I can't recall this behaviour ("0" evaluating as equal to all strings) in previous versions...

The string doesn't evaluate as equal against the other int, nor against the boolean or null.... Why should it evaluate to equal against "0"? It's not right.
Link to comment
https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77755
Share on other sites

i use php 4
if string start with number PHP convert string in this number, if start with no number simbol it convert it in 0 (not '0')
try[code]
<?php
if(12 == '12pm') echo '12 = 12pm '; else echo '12 != 12pm ';
if (12 == '12 + 6') echo '12 = 12 + 6 '; else echo '12 != 12 + 9 ';
if(12 == 'pm12') echo '12 = pm12 '; else echo '12 != pm12 ';
if (100 == '1E2') echo '100 == 1E2'; else echo '100 != 1E2';
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77772
Share on other sites

[quote]I too now use 5.1.4, and I can't recall this behaviour ("0" evaluating as equal to all strings) in previous versions...[/quote]

As long as I can remember using PHP (since 2002) this has been the case, I've run the code below in v4 and v5 and results are the same

[code]$str = '12x345';
echo $str * 1;  //--> 12

$str = 'x';
echo $str * 1;  //--> 0[/code]

The numeric value of a string is the value of any numeric characters up to the first non-numeric character.
Link to comment
https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77773
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.