PHP : comparison operator === not working on hosting server - php

In the file defines.php, I have define("YES",1);.
I am including this file in the login.php page as
require_once('/defines.php');
where I have this check if ($row['registered'] === YES). This is not evaluating to true. If I use double equals, it works. I did
echo $row['registered'];
echo YES;
and I am getting the output as
1
1
On my localhost machine, however, === is working fine. Why is this strange behaviour? Is there any dependency on production server?
PS : My hosing server is using PHP v5.4
EDIT
Var dump :
string(1) "1" int(1)
But I have tinyint type in database, why I am getting string data type?

Your database driver returns the value of registered as string
you can either typecast your integer values and them compare them
using ===
(int)$row['registered']
or use the == operator instead
Check this answer https://stackoverflow.com/a/80649/2255129 for the differences of === and ==

the problem is
$var1 = '1';
$var2 = 1;
// this will be right
if ( $var1 == $var2 )
{ echo "'1' = 1"; }
// This will never be right
if ( $var1 === $var2 )
{ echo "'1' != 1"; }
in PHP you can not use === to match string and int or boolean.
eg.
1 == '1' = true
1 === '1' = false
1 === 1 = true
true === 1 = false
true == 1 = true
thats mean if you use === and not == its need to be same type like string === string or int === int
you need to use == if you want to match int and string, i do not recommend you combine string and int in a if statement.
convert you string to int, and make a match agin like this
$var1 = (int) $row['registered'];
$var2 = (int) YES;
if ( $var1 === $var2 )
{ echo 'your right'; }
else
{ echo 'you got a problem'; }

Related

Variable with value 0 equal string results in true

I have acrossed some really weird behaviour in PHP statment when having example below:
Logically it shouldn't return 1 in this context. Why this is happening ? Just was wondering.
$test = 0;
var_dump($test); // gives int 0
$test = ($test == 'test') ? 1 : 0;
var_dump($test); //gives int 1
This is because of type juggling. 'test' is equal to 0 because (int)'test' actually is 0. Thus, your condition is true and 1 is the result.
In your particular case you may want to know how PHP converts strings to numbers.
Just try with === to compare also type of values:
$test = ($test === 'test') ? 1 : 0;

PHP Casting Error

<?php
isInt("4");
isInt("Test");
function isInt($id){
echo $id." ".(int)$id;
if($id == (int)$id)echo "True.<br />";
else echo "False.<br />";
}
?>
The output results in:
4 4 True.
Test 0 True.
You'll notice the 2nd results in true, which by the output, it should echo false.
I realize there is a built in function in php is_int().
I also realize that in the if statement if I put a 3rd equals sign: if($id === (int)$id) then it will return false for the 2nd one, but it will also return false for the 1st one too.
Can someone explain to me why PHP does this, and maybe a fix for this? (I am running PHP 5.4.22)
Basically what I want to accomplish is isInt("4") to echo true, isInt(4) to echo true, and isInt("Text") to echo false;
If you use the triple equals, it checks the type along with the result.
For the first one, "4" is a string, not an int (the literal 4 is an int, but since you're using quotes, it's a string).
For the second one, "Test" is a string as well.
For your second version:
$id = 'Test';
'Test' == (int)'Test';
'Test' == 0
0 == 0
TRUE
Strings which contain no digits at the beginning of the string will always get auto-converted to integer 0 when used in an integer context.
If you'd had 123Test instead:
$id = '123Test';
'Test' == int('Test');
'Test' == 123
0 == 123
FALSE
to get desired value , check this..
if($id === (int)$id)echo "True.<br />";

php variables comparison == vs ===

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.

Why does (0 == 'Hello') return true in PHP?

Hey, if you have got the following code and want to check if $key matches Hello I've found out, that the comparison always returns true if the variable is 0. I've came across this when an array for a special key and wondered why it's wasn't working as expected.
See this code for an example.
$key = 1;
if ($key != 'Hello') echo 'Hello'; //echoes hello
$key = 2;
if ($key != 'Hello') echo 'Hello'; //echoes hello
$key = 0;
if ($key != 'Hello') echo '0Hello'; //doesnt echo hello. why?
if ($key !== 'Hello') echo 'Hello'; //echoes hello
Can anyone explain this?
The operators == and != do not compare the type. Therefore PHP automatically converts 'Hello' to an integer which is 0 (intval('Hello')). When not sure about the type, use the type-comparing operators === and !==. Or better be sure which type you handle at any point in your program.
Others have already answered the question well. I only want to give some other examples, you should be aware of, all are caused by PHP's type juggling. All the following comparisons will return true:
'abc' == 0
0 == null
'' == null
1 == '1y?z'
Because i found this behaviour dangerous, i wrote my own equal method and use it in my projects:
/**
* Checks if two values are equal. In contrast to the == operator,
* the values are considered different, if:
* - one value is null and the other not, or
* - one value is an empty string and the other not
* This helps avoid strange behavier with PHP's type juggling,
* all these expressions would return true:
* 'abc' == 0; 0 == null; '' == null; 1 == '1y?z';
* #param mixed $value1
* #param mixed $value2
* #return boolean True if values are equal, otherwise false.
*/
function sto_equals($value1, $value2)
{
// identical in value and type
if ($value1 === $value2)
$result = true;
// one is null, the other not
else if (is_null($value1) || is_null($value2))
$result = false;
// one is an empty string, the other not
else if (($value1 === '') || ($value2 === ''))
$result = false;
// identical in value and different in type
else
{
$result = ($value1 == $value2);
// test for wrong implicit string conversion, when comparing a
// string with a numeric type. only accept valid numeric strings.
if ($result)
{
$isNumericType1 = is_int($value1) || is_float($value1);
$isNumericType2 = is_int($value2) || is_float($value2);
$isStringType1 = is_string($value1);
$isStringType2 = is_string($value2);
if ($isNumericType1 && $isStringType2)
$result = is_numeric($value2);
else if ($isNumericType2 && $isStringType1)
$result = is_numeric($value1);
}
}
return $result;
}
Hope this helps somebody making his application more solid, the original article can be found here:
Equal or not equal
pretty much any non-zero value gets converted to true in php behind the scenes.
so 1, 2,3,4, 'Hello', 'world', etc would all be equal to true, whereas 0 is equal to false
the only reason !== works is cause it is comparing data types are the same too
Because PHP does an automatic cast to compare values of different types. You can see a table of type-conversion criteria in PHP documentation.
In your case, the string "Hello" is automatically converted to a number, which is 0 according to PHP. Hence the true value.
If you want to compare values of different types you should use the type-safe operators:
$value1 === $value2;
or
$value1 !== $value2;
In general, PHP evaluates to zero every string that cannot be recognized as a number.
In php, the string "0" is converted to the boolean FALSE http://php.net/manual/en/language.types.boolean.php

Not equal to != and !== in PHP

I've always done this: if ($foo !== $bar)
But I realized that if ($foo != $bar) is correct too.
Double = still works and has always worked for me, but whenever I search PHP operators I find no information on double =, so I assume I've always have done this wrong, but it works anyway. Should I change all my !== to != just for the sake of it?
== and != do not take into account the data type of the variables you compare. So these would all return true:
'0' == 0
false == 0
NULL == false
=== and !== do take into account the data type. That means comparing a string to a boolean will never be true because they're of different types for example. These will all return false:
'0' === 0
false === 0
NULL === false
You should compare data types for functions that return values that could possibly be of ambiguous truthy/falsy value. A well-known example is strpos():
// This returns 0 because F exists as the first character, but as my above example,
// 0 could mean false, so using == or != would return an incorrect result
var_dump(strpos('Foo', 'F') != false); // bool(false)
var_dump(strpos('Foo', 'F') !== false); // bool(true), it exists so false isn't returned
!== should match the value and data type
!= just match the value ignoring the data type
$num = '1';
$num2 = 1;
$num == $num2; // returns true
$num === $num2; // returns false because $num is a string and $num2 is an integer
$a !== $b TRUE if $a is not equal to $b, or they are not of the same type
Please Refer to http://php.net/manual/en/language.operators.comparison.php
You can find the info here: http://www.php.net/manual/en/language.operators.comparison.php
It's scarce because it wasn't added until PHP4. What you have is fine though, if you know there may be a type difference then it's a much better comparison, since it's testing value and type in the comparison, not just value.

Categories