Why is "01" == "1", "000333" == "333", etc true? [duplicate] - php

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 6 years ago.
While developing a PHP application, I ran into this strange quirk.
Apparently, the string '01' == '1', '05' == '5', '03111' == '3111'.
I tried this -
php > $numbers = ["1", "2", "3", "4", "5"];
php > in_array("01", $numbers);
true
php > var_dump("01" == "1");
true
php > var_dump("00003333" == "3333")
true
How do I prevent this (i.e return false for the in_array call) , and why is it happening in the first place?

You should use strict comparison in in_array
in_array("01", $numbers, true);
and === instead of ==
then php compare also types

Related

This can't be TRUE [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 5 years ago.
$vartest = 0;
if ($vartest == "This can't be TRUE") { echo "But it is TRUE"; }
>> But it is TRUE
By mistake I made a wrong declaration somewhere in my program, and discovered this strange behaviour.
I used $vartest = ā€œā€ most of the time, but somewhere $vartest = 0 slipped into the program.
Because I spent hours to find the error Iā€™m posting this just for awareness.
Just one question. The variable $vartest is empty, but why does PHP find this TRUE ?
You had to compare on equality and type with "===" and not only with "==" for equality with typeconversion. Try this:
$vartest = 0;
if ($vartest === "This can't be TRUE") { echo "But it is TRUE"; }

Why string date is equal to 0 [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 6 years ago.
I just wondering below code ! I had never seen and heard before .Why string date is equal to 0 ? Is there any documentation for that..
<?php
$p = "date";
$n = 0;
$m = 1;
var_dump($p == $n);//true
var_dump($p == $m);//false
var_dump($n == $m);//false
?>
Yes, you compare string with int so string is converted to int first. int from "date" string is 0
See this
you have used ==
0 is an int, so in this case it is going to convert 'date' into int. Which is not parseable as one, and will become 0. that is why you are getting true. try === opertor
That's how it works:
Reference : Manual [See the table]
Loose comparisons with ==
"PHP" == 0 is true
"PHP" == 1 is false
Strict comparisons with ===
"PHP" === 0 is false
"PHP" === 1 is false
So is your case with "date"

Simple If else is not working [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 8 years ago.
<?php
var_dump($isHoster); // prints int(0)
if ($isHoster == 'all')
$conditionsHoster = '0, 1';
else
$conditionsHoster = intval($isHoster);
var_dump($conditionsHoster); // prints string(4) "0, 1"
?>
What is going on?? Who can explain that?
This never happened to me...
0 == 'all' is true in php because php tries to convert 'all' to int and (int) 'all' is 0; you should write
if ($isHoster === 'all')
var_dump($isHoster); // prints int(0)
if ($isHoster === 'all')
$conditionsHoster = '0, 1';
else
$conditionsHoster = intval($isHoster);
var_dump($conditionsHoster);
Its because you are comparing Int with String.
Before comparing convert $isHoster to string like $isHoster = (string) $isHoster; or user === to compare.

== in php causing false positives when variable is zero [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
php == vs === operator [duplicate]
Why does 1234 == '1234 test' evaluate to true? [duplicate]
(6 answers)
Closed 9 years ago.
I have the strangest php behavior I've never noticed before:
$array =array(0,1,2, 'parent');
foreach ($array as $value)
{
if ($value=='parent')
{
echo $value;
echo '<br>';
continue;
}
}
exit;
Will return
0
parent
I was wondering if anyone could explain to me why it matches the 0 to the string 'parent'?
what Mark said but to expound, 0 is a number so the == operator casts the 'parent' string to a number, which gives 0. The == operator does not care about type. So they match.
The "==" operator in php does not compare the type of the objects, it converts the objects to another type. So in your case you are comparing a numerical object with a string object. So it changes the type of your string 'parent'. Since that string does not contain any numerical value it probably changes it to 0.
See
http://il.php.net/manual/en/language.operators.comparison.php and
http://il.php.net/manual/en/language.types.string.php#language.types.string.conversion
for more explanations

Which one is true while making kontrol with php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL) === 0)
or
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL) == 0)
Should I use == or === here?
Where can I use ===?
=== is used to make a strict comparison, that is, compare if the values are equal and of the same type.
Look at #Ergec's answer for an example.
In your case you should do just:
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL) == false)
or simply
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL))
because filter_input() returns:
Value of the requested variable on success, FALSE if the filter fails,
or NULL if the variable_name variable is not set. If the flag
FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is
not set and NULL if the filter fails.
In PHP double equals matches only VALUE
if (2 == "2") this returns TRUE
Triple equals matches VALUE AND TYPE
if (2 === "2") this returns FALSE
You are looking for !== FALSE in that case.
filter_input will either return the filtered variable or FALSE.
=== checks if both sides are equal and of the same type.
!== checks if both sides are not equal and of the same type.

Categories