Jump to content

Archived

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

theblazingangel

foreach problem

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

Share this post


Link to post
Share on other sites
You compare number with string
PHP convert string into number and it is equak 0
try[code]
if ($data == "data1") { $rowNum = "$row"; }
[/code]

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
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]

It doesn't always convert to zero.  See here:

http://us2.php.net/manual/en/language.types.string.php#language.types.string.conversion

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sorry Barand...I completely missed your post for some reason...didn't mean to reiterate what you said.

Share this post


Link to post
Share on other sites
Hmmmm. Ok, so I guess I just don't compare numbers with strings very often.. :P

Share this post


Link to post
Share on other sites

×

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.