Triple equals in validations [duplicate] - php

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
I've read somewhere that === is more secure than ==
So should I use something like this:
if ($user === "") {
echo "you are not logged in";
}
instead of
if ($user == "") {
echo "you are not logged in";
}
? The question is probably stupid, but I never really used === so I don't know much about it from security standpoint.
I am strictly interested in why === is more secure than ==
The duplicate doesn't address that.

Related

Type conversion string / integer [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 1 year ago.
I know that this is caused by type conversion, but can someone explain me, what exactly is happening here, when:
(2 == '2abd') // true
(3 == '3a') // true
I was a bit surprised when running into a bug by comparing a string variable against strings and integers.
Thank you very much!
If we convert 2abd and 3a to integers, you'll get 2 and 3 accordingly.
Since 2 == 2 and 3 == 3 both statements return true.
Online test case:
<?php
var_dump(2 == '2abd'); // true
var_dump(3 == '3a'); // true
var_dump((int) '2abd'); // 2
var_dump((int) '3a'); // 3
var_dump(2 == 2); // true
var_dump(3 == 3); // true
For more info about operator precedence, please take a look at:
PHP's help page regarding this
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Rules about converting to integers

PHP 0 vs False ambiguity [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've been trying to make a system where certain flags in a MySQL database are checked with php. To see if a certain letter is in the string, I use the function strpos(). If the letter I'm checking for happens to be the first one in the string the function returns 0. However this result seems to be synonymous with the Boolean value for false. I tested this theory with the below code:
if(0 == false){
echo 'true';
}
low and behold this evaluates to true and echoes the output inside the block. So, is there a logical way to distinguish between a character being at index 0 or not being part of the string at all? One option that occurred to me was to basically prepend the flags string with some character like an underscore. This way no meaningful characters would ever be at index 0 of the function's return value. That seems like a very dirty patchy way to fix things though. If you have any other ideas I would greatly appreciate your help.
Thanks for reading.
Use ===:
if(0 === false){
// will never be true
echo 'true';
}

Not sure which character it is [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I'm following a PHP tutorial, but I've come across a chracter and I have no idea what it is.
Here's a picture of it http://prntscr.com/7wktf6
Please tell me what it is and how to get it
That is the "&" symbol, a.k.a. "and" or ampersand
In the picture you took, two ampersands (&) next to each other in a logic statement means "and". Example,
(0 == 1 && 2 == 2) and (0 == 1 && 2 == 3)
evaluiate to false because one or more of the statments on either side of the "&&" is false. While,
(0 == 0 && 1 == 1)
evaluates to true because they are both true.
Here is more information on the matter.

Three equals signs in php comparison [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 9 years ago.
I was working on some code made by someone else and came across in if statement that looks like this
IF($res === true){
do_something();}
This code is called many times and seems to be working. What is it doing?
I know one equals is assignment and two is comparison. What does the three equals sign operator do?
The easiest way to see it, is that == checks equality and === checks identicality. Equality will check the value, but identicality will check the variable type too.
Examples:
var_dump('true' == true); // bool(true)
var_dump('true' === true); // bool(false)
This is the "identical" operator. They'll need to be exactly the same to pass the condition.
The 3 different equals
Three === means that result has to be identical. Worded it wrong first time :)

What does === mean in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “===” mean?
i am seeing === often in php statements, but don't know what it mean.
e.g if ($pwd === PwdHash($pass,substr($pwd,0,9))).
thanks
It tests equality, but unlike == it requires that the two operands be of the same type as well as value.
For instance, "1" == 1 will be true, but "1" === 1 is false because the type is different.
php has two types of equal comparison operator == and ===
== check for the equalization but not strict mean it will return true for ('123'==123)
=== is a strict equal operator it will return false for the ('123'===123)
read more about these from here

Categories