Help with this expression
-
It returns false because you are comparing a string literal to a number. If your string was if("0"==0) it should work. You may want to try to write the statement like this.
<?php
$test_array = array(1,"a",3,"b",5,"c",7,"d","e",0,"f","g","h","i","j","k");
$i = 0
foreach($test_array as $x)
{
if($x==0)
{
echo "found You at index ".$i."!<br>";
exit;
}
else
{
echo "still looking<br>";
}
}
$i++;
?>This should help you...
I've tried that, and it returned: still looking found You at index 1! So PHP thought that "a" was equal to 0 ... again, string is equal to zero, like in my first post ... what's going on ? is it maybe just error in my php installation ?? Right now, I'm very confused :D BTW, I've just tried that script on some other web server, result is the same ... is this kind of bug in PHP or what :D ? Thanks
-
Hello ! I have very simple expression in if statement: "d"==0 So in my code it looks like this: if ("d"==0) { //do something } Of course, booth sides aren't always like that, they are variables, but I saw that I had bug somewhere and it turned out that error occurs always when on one side is string and on other zero ... Can anyone explain to me why does this return true ?? I can't see why string would be equal to zero ... Thanks :)
-
I've tried that, and it returned: still looking found You at index 1! So PHP thought that "a" was equal to 0 ... again, string is equal to zero, like in my first post ... what's going on ? is it maybe just error in my php installation ?? Right now, I'm very confused :D BTW, I've just tried that script on some other web server, result is the same ... is this kind of bug in PHP or what :D ? Thanks
Sorry made a mistake in the code. This is what it should look like. See if you can spot the change. Understanding the difference between this code and the first one I posted will answer your question. You can then test it further by moving the 0 (zero) to a different location within the array.
<?php
$test_array = array(1,"a",3,"b",5,"c",7,"d","e",0,"f","g","h","i","j","k");
$i = 0;
foreach($test_array as $x)
{
if($x==0)
{
echo "found You at index ".$i."!<br>";
exit;
}
else
{
echo "still looking<br>";
}
$i++;
}
?> -
xx77abs wrote:
Can anyone explain to me
Read the manual. http://www.php.net/manual/en/language.types.php[^]
-
Sometimes people learn more and better by example or a quick question, no need to discourage him.
-
Sorry made a mistake in the code. This is what it should look like. See if you can spot the change. Understanding the difference between this code and the first one I posted will answer your question. You can then test it further by moving the 0 (zero) to a different location within the array.
<?php
$test_array = array(1,"a",3,"b",5,"c",7,"d","e",0,"f","g","h","i","j","k");
$i = 0;
foreach($test_array as $x)
{
if($x==0)
{
echo "found You at index ".$i."!<br>";
exit;
}
else
{
echo "still looking<br>";
}
$i++;
}
?>Thank you for your help, but if you mean that error was this line: $i++; outside of foreach loop, I've already fixed it before trying the sample and it still don't work ... http://x.xx77abs.com/example.php[^] As you can see, it tells me that if found zero at index 1, and you can see source code that was executed (it is yours :) ) ;)
-
Thank you for your help, but if you mean that error was this line: $i++; outside of foreach loop, I've already fixed it before trying the sample and it still don't work ... http://x.xx77abs.com/example.php[^] As you can see, it tells me that if found zero at index 1, and you can see source code that was executed (it is yours :) ) ;)
I made a change to the code and got it to give the expected results.
foreach($test\_array as $x) { echo $x."<br>"; if($x==0 && is\_numeric($x))
The echo of $x shows it is (without the is_numeric function) stopping on "a", not what I would expect. Running the code with the is_numeric gives the correct result of index 9. I also tested === with the same results as the is_numeric. Without spending more time researching this I can only assume that php is parsing "a" as being equal to 0. I suspect this maybe due to something the parser is doing to allow the testing of a string and an integer. OK, I just did a little research and found this in the PHP manual...
String conversion to numbers
When a string is evaluated in a numeric context, the resulting value and type are determined as follows.If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer . In all other cases it will be evaluated as a float .
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). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.
For more information on this conversion, see the Unix manual page for strtod(3).
A quick google on strtod shows me this...
Return Value
On success, the function returns the converted floating point number as a double value.
If no valid conversion could be performed, a zero value (0.0) is returned.
If the correct value is out of the range of representable values, a positive or negative HUGE_VAL is returned, and the global variable errno is set to ERANGE.
If the correct value would cause underflow, zero is returned and errno is set to ERANGE.This looks to be the issue...but this is just a guess.
-
I made a change to the code and got it to give the expected results.
foreach($test\_array as $x) { echo $x."<br>"; if($x==0 && is\_numeric($x))
The echo of $x shows it is (without the is_numeric function) stopping on "a", not what I would expect. Running the code with the is_numeric gives the correct result of index 9. I also tested === with the same results as the is_numeric. Without spending more time researching this I can only assume that php is parsing "a" as being equal to 0. I suspect this maybe due to something the parser is doing to allow the testing of a string and an integer. OK, I just did a little research and found this in the PHP manual...
String conversion to numbers
When a string is evaluated in a numeric context, the resulting value and type are determined as follows.If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer . In all other cases it will be evaluated as a float .
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). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.
For more information on this conversion, see the Unix manual page for strtod(3).
A quick google on strtod shows me this...
Return Value
On success, the function returns the converted floating point number as a double value.
If no valid conversion could be performed, a zero value (0.0) is returned.
If the correct value is out of the range of representable values, a positive or negative HUGE_VAL is returned, and the global variable errno is set to ERANGE.
If the correct value would cause underflow, zero is returned and errno is set to ERANGE.This looks to be the issue...but this is just a guess.
and php does force a conversion of strings when they appear in a comparisons
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
source: http://www.php.net/manual/en/language.operators.comparison.php[^] I also noted that on this page that speaks to comparisons they have the following sample code
<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> trueswitch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
?>So even the manual supports the "a" == 0, but I can not find out exactly why it does this. The questions I now have is does the function used in the conversion return a zero as a result or is this an error from the function which also returns a zero?
-
I made a change to the code and got it to give the expected results.
foreach($test\_array as $x) { echo $x."<br>"; if($x==0 && is\_numeric($x))
The echo of $x shows it is (without the is_numeric function) stopping on "a", not what I would expect. Running the code with the is_numeric gives the correct result of index 9. I also tested === with the same results as the is_numeric. Without spending more time researching this I can only assume that php is parsing "a" as being equal to 0. I suspect this maybe due to something the parser is doing to allow the testing of a string and an integer. OK, I just did a little research and found this in the PHP manual...
String conversion to numbers
When a string is evaluated in a numeric context, the resulting value and type are determined as follows.If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer . In all other cases it will be evaluated as a float .
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). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.
For more information on this conversion, see the Unix manual page for strtod(3).
A quick google on strtod shows me this...
Return Value
On success, the function returns the converted floating point number as a double value.
If no valid conversion could be performed, a zero value (0.0) is returned.
If the correct value is out of the range of representable values, a positive or negative HUGE_VAL is returned, and the global variable errno is set to ERANGE.
If the correct value would cause underflow, zero is returned and errno is set to ERANGE.This looks to be the issue...but this is just a guess.
-
xx77abs wrote:
Can anyone explain to me
Read the manual. http://www.php.net/manual/en/language.types.php[^]
<Pretentious> Raid tha manyuhl. :E <Pretentious> Aw raid eh own mah meaxbile. :E
-
Hello ! I have very simple expression in if statement: "d"==0 So in my code it looks like this: if ("d"==0) { //do something } Of course, booth sides aren't always like that, they are variables, but I saw that I had bug somewhere and it turned out that error occurs always when on one side is string and on other zero ... Can anyone explain to me why does this return true ?? I can't see why string would be equal to zero ... Thanks :)
This happens because strings, not just "a", are type cast to integer type when compared against an integer, and the integer that they are equivalent to is zero, which equals zero. :)
[Honestly Illustrated](http://honestlyillustrated.com)