theblazingangel Posted August 20, 2006 Share Posted August 20, 2006 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 happennor if you replace [i]whatever[/i] with a numbersoftware bug?im using php v5.1.5 btw and it also happened on v5.1.4thanks Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/ Share on other sites More sharing options...
sasa Posted August 20, 2006 Share Posted August 20, 2006 You compare number with stringPHP convert string into number and it is equak 0try[code]if ($data == "data1") { $rowNum = "$row"; }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77743 Share on other sites More sharing options...
448191 Posted August 20, 2006 Share Posted August 20, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77755 Share on other sites More sharing options...
sasa Posted August 20, 2006 Share Posted August 20, 2006 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]<?phpif(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] Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77772 Share on other sites More sharing options...
Barand Posted August 20, 2006 Share Posted August 20, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77773 Share on other sites More sharing options...
hitman6003 Posted August 20, 2006 Share Posted August 20, 2006 [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]It doesn't always convert to zero. See here:http://us2.php.net/manual/en/language.types.string.php#language.types.string.conversion Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77789 Share on other sites More sharing options...
Barand Posted August 20, 2006 Share Posted August 20, 2006 it says[quote]The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).[/quote]Which is what I just said. Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77793 Share on other sites More sharing options...
hitman6003 Posted August 20, 2006 Share Posted August 20, 2006 Sorry Barand...I completely missed your post for some reason...didn't mean to reiterate what you said. Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77794 Share on other sites More sharing options...
448191 Posted August 21, 2006 Share Posted August 21, 2006 Hmmmm. Ok, so I guess I just don't compare numbers with strings very often.. :P Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-77925 Share on other sites More sharing options...
theblazingangel Posted August 21, 2006 Author Share Posted August 21, 2006 cheers everyone ;) Quote Link to comment https://forums.phpfreaks.com/topic/18132-foreach-problem/#findComment-78005 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.