Jump to content


Photo

foreach problem


  • Please log in to reply
9 replies to this topic

#1 theblazingangel

theblazingangel
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 20 August 2006 - 09:05 PM

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

<?php

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

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

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

?>

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

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

thanks

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 20 August 2006 - 09:18 PM

You compare number with string
PHP convert string into number and it is equak 0
try
if ($data == "data1") { $rowNum = "$row"; }


#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 10:09 PM

<?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 />'; 
}
?>

That prints only:

Integer "0" is equal to string "whatever".


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.

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 20 August 2006 - 10:59 PM

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
<?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';
?>


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 20 August 2006 - 11:00 PM

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


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

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

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

The numeric value of a string is the value of any numeric characters up to the first non-numeric character.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 11:44 PM

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


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

http://us2.php.net/m...ring.conversion

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 20 August 2006 - 11:49 PM

it says

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).


Which is what I just said.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 11:50 PM

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

#9 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 21 August 2006 - 06:04 AM

Hmmmm. Ok, so I guess I just don't compare numbers with strings very often.. :P

#10 theblazingangel

theblazingangel
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 21 August 2006 - 12:01 PM

cheers everyone ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users