Why does this always return true:
$s = '334rr';
$i = (int)$s;
if ($i == $s) {
echo true;
} else {
echo false;
}
If I echo $i it gives the value of 334, which is different from $s which is 334rr.
From the manual:
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.
So: ($i == $s) is the same as ($i == (int)$s) for the values you've given.
Use === to avoid type-juggling.
When compare string with integer using ==, string will try to case into integer.
Try this
$s = '334rr';
$i = intval($s);
if ($i == $s) {
echo true;
} else {
echo false;
}
Comparing strings to an int is not recommended. You should use the === instead which will verify the same data type as well as the same value.
PHP converts the $s string to an integer when comparing to another integer ($i).
It basically does this (well, i don't know what it does internally, but it boils down to this):
if($i == (int) $s)
Which makes the statement true
Related
This is driving me nuts. I've spent a couple hours trying to debug this but I'm getting nowhere. $total should increment by 1 everytime $result != 'REMAKE' but it's only incrementing whenever $result == 1 (and not when $result == 0). I cannot figure out why. I've tried $total += 1 but it makes no difference.
didAccountWinMatch() will return either 0, 1, or 'REMAKE'.
$total = 0;
$wins = 0;
if (isset($match)) {
foreach ($match as $key => $value) {
$result = didAccountWinMatch($accountId, $match[$key]);
if ($result != "REMAKE") {
$total ++;
$wins += $result;
}
}
} else {
$total = 0;
$wins = 0;
}
Use !== instead of !=. If you use the loose comparison operator between an integer and a string, the string is converted to an integer first. Converting the string REMAKE to an integer results in 0, so the code is equivalent to
if ($result != 0)
when $result is an integer.
The strict comparison operator !== doesn't perform type conversion; if the operands are different types it returns false.
I have two variables such as:
var1 = "z";
var2 = "A";
how can I check if var1 is after in the alphabet than var2 (in this case it should return true)?
I think everyone who has answered agrees that strcmp() is the right answer, but every answer provided so far will give you incorrect results. Example:
echo strcmp( "Z", "a" );
Result: -1
echo strcmp( "z", "A" );
Result: 1
strcmp() is comparing the binary (ord) position of each character, not the position in the alphabet, as you desire. If you want the correct results (and I assume that you do), you need to convert your strings to the same case before making the comparison. For example:
if( strcmp( strtolower( $str1 ), strtolower( $str2 ) ) < 0 )
{
echo "String 1 comes before string 2";
}
Edit: you can also use strcasecmp(), but I tend to avoid that because it exhibits behavior that I've not taken the time to understand on multi-byte strings. If you always use an all-Latin character set, it's probably fine.
What did you try?... pretty sure this works
<?php
if(strcmp($var1,$var2) > 0) {
return true;
}
If you're comparing a single character, you can use ord(string). Note that uppercase values compare as less than lowercase values, so convert the char to lowercase before doing the comparison.
function earlierInAlphabet($char1, $char2)
{
$char1 = strtolower($char1);
$char2 = strtolower($char2);
if(ord($char1) < ord($char2))
return true;
else
return false;
}
function laterInAlphabet($char1, $char2)
{
$char1 = strtolower($char1);
$char2 = strtolower($char2);
if(ord($char1) > ord($char2))
return true;
else
return false;
}
If you're comparing a string (or even a character) then you can also use strcasecmp(str1, str2):
if(strcasecmp($str1, $str2) > 0)
// str1 is later in the alphabet
return strcmp($var1,$var2) > 0?
You should use http://php.net/manual/en/function.strcmp.php
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
So
return strcmp(var1, var2) > 0 // will return true if var1 is after var2
This solution may be over-the-top for just two variables; but this is for ever if you need to solve if a bunch of variables (2+) would be in the right order...
<?php
$var1='Z';
$var2='a';
$array1 = array();
$array[] = $var1;
$array1[] = $var2;
$array2 = sort($array1);
if($array2 === $array1){
return true;
}else{
return false;
}
?>
Other than that if you only want to do it with two variables this should work just fine.
<?php
return (strcmp($var1,$var2) > 0);
?>
I have this code
$arr = array(
"0"=>"http://site.com/somepage/param1/param2/0",
"1"=>"http://site.com/somepage/param1/param2/1",
"thispage" => "http://site.com/somepage/param1/param2/2",
"3"=> "http://site.com/somepage/param1/param2/3"
);
foreach ($arr as $k=>$v) {
if ($k == "thispage") {
echo $k." ";
}
else {
echo ''.$k.' ';
}
}
Its surprise, for first element "0"=>"http://site.com/somepage/param1/param2/0", not created link, (for other elements works fine)
If replace first element key 0 on something other, for example 4, now links created. What is wrong ?
This is happening because 0 == "thispage" and the first key is 0. To find out more about this, take a look at the PHP manual page about Type Juggling.
Use === ("is identical to") instead of == ("is equal to"), because 0 is equal to "thispage", but not identical.
This is what happens with ==:
$key takes the integer value of 0
PHP tries to compare 0 == "thispage"
in order to make the comparison, it needs to cast "thispage" to integer
the resulting comparison is 0 == 0, which is true
If you use ===:
$key takes the integer value of 0
PHP tries to compare 0 === "thispage"
since 0 is of a different type (integer) than "thispage" (string), the result is false
This is What you are doing wrong.
if ($k === "thispage") {
echo .$k." ";
}
Do the:
if ($k === "thispage")
You have to use identical comparison operator === as equal comparison operator won't help here, because
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.
thispage converted to number will return 0, so your if statement will match if you use equal comparison operator ==. When you do identical comparison === if type does not match it returns false.
You can read about comparison operators here.
Try this:
if ($k === "thispage") {
echo $k." ";
}
http://us.php.net/manual/en/language.types.array.php:
A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08").
So in your case Stings "1", "2" and "3" are treated as integers.
To fix this use the === operator that check for type along with value.
The reason for the result you see is the comparison operator you use. == is too imprecise sometimes and can result in wierd things like this. Using the === will compare the values for exactness and will prevent the issue you have.
so:
foreach ($arr as $k=>$v) {
// this is the important thing
if ($k === "thispage") {
echo $k." ";
}
else {
echo ''.$k.' ';
}
}
this is code:
$s = 0;
$d = "dd";
if ($s == $d) {
var_dump($s);
die(var_dump($d));
}
result is:
int 0
string 'dd' (length=2)
Please explain why.
why ($s == $d) results as true?
Of course, if === is used it will results as false but why this situation requires ===?
Shouldn't it be returned false in both situations?
Because (int)$d equals with 0 and 0=0
you must use strict comparison === for different character tyes (string) with (int)
Your $d is automatically converted to (int) to have something to compare.
When you compare a number to a string, the string is first type juggled into a number. In this case, dd ends up being juggled into 0 which means that it equates to true (0==0).
When you change the code to:
<?php
$s = 1;
$d = "dd";
if ($s == $d)
{
var_dump($s);
die(var_dump($d));
}
?>
You will find that it doesn't pass the if statement at all.
You can more details by reading up on comparison operators and type juggling.
The string "dd" is converted to int, and thus 0.
Another example :
if ( "3kids" == 3 )
{
return true;
}
And yes, this returns true because "3kids" is converted to 3.
=== does NOT auto convert the items to the same type.
Also : 0 == false is correct, but 0 === false is not.
See : http://php.net/manual/en/language.types.type-juggling.php
The string will try to parsed into a number, returns 0 if it is not in right number format.
As seen in the php website :
http://php.net/manual/en/language.operators.comparison.php
var_dump(0 == "a"); // 0 == 0 -> true
In PHP, == should be pronounce "Probably Equals".
When comparing with ==, PHP will juggle the file-types to try and find a match.
A string with no numbers in it, is evaluated to 0 when evaluated as an int.
Therefore they're equals.
I have the following code
while($row = $usafisRSP->fetch_assoc())
{
$hidden_keys = array('Applicantid', 'unique_num', 'regs_time' ....);
$hidden_fields = array_intersect_key($row, array_fill_keys($hidden_keys, NULL));
$hidden_values = array();
foreach ($hidden_fields as $key => $value) {
// fill the values array using the values from fields array
$hidden_values[$value] = "$key = ".base64_decode($value)."";
if(base64_decode($value)== 0)
{
$hidden_values[$value] = "";
}
echo $hidden_values[$value];
The question is about "if($hidden_values[$value] == 0)" ... Basically I want to do not display/echo the $hidden_values[$value] if it's value of $value is 0. Sometimes $value is 0 or some words like (23 avenue).
I think you ran into three catches with PHP type comparisons and equalities:
Any string not beginning with a number will always loosely equal 0. So basically, if(base64_decode($value)== 0) will likely always resolve to true, even if decoded $value is "Adam".
Return value of base64_decode is a string, so if 0 is the result, it will be string 0, not integer 0. This means if(base64_decode($value) === 0) wouldn't even work if decoded $value is "0". Another catch is base64_decode may return false on errors, again failing this strict equality check.
A non-empty string (other than "0") will always loosely equal true. So this is the only comparison you really need for your case.
I think this is what you want, replacing the last 5 lines...
if(base64_decode($value)) echo $hidden_values[$value];
else $hidden_values[$value] = "";
} // closing your for loop
Is this what you're looking for?
foreach( $hidden_values as $value ) {
if( $value !== 0 ) {
echo $value;
}
}