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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.