The usage of == and === in php [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “===” mean?
I am confused with the use of those operators in php, I am not quite sure when should I use === and when ==.
for example why/when should I write:
if( $some_method_that_returns_something_or_false() === FALSE) {
//do stuff
}
and when with ==?
Also, does === means I must return bool FALSE or I can return 0? When it is considered an bad practice to use === or ==?
Also when putting something like this:
if($some_method_that_returns_true_or_false()) {
}
is that $some_method_that_returns_true_or_false() == TRUE or
some_method_that_returns_true_or_false() === TRUE?

=== means exact value, so for true it has to be true, while == checks for the meaning of the value, so true will be also a value of '1' or a whatever String.

== is used for checking equallity and === is used for checking the equality as well as type.
And
if($some_method_that_returns_true_or_false()) {
}
is checking for $some_method_that_returns_true_or_false() == TRUE

Related

My error handlers return false when they shouldn't [duplicate]

This question already has answers here:
In php, is 0 treated as empty?
(18 answers)
Closed 4 years ago.
I have the following error handlers in my PHP file:
if (empty($title) || empty($description) || empty($price)) {
header("Location: ../listing1.php?error=emptyfields");
exit();
} elseif (!is_numeric($price)) {
header("Location: ../listing1.php?error=onlynumbers");
exit();
}
But when I type in accordance with the is_numeric error handler it returns false telling it's error=emptyfields. I have tried switching positions but it still returns false and now I'm lost, though when I type in anything above 0 it returns true.
PHP's empty() function will consider the values 0 and "0" to be empty. Thus, even though the value is numeric and is properly set, it will still result in incorrect behavior.
You're going to need to be more careful about your first if condition. Instead of just empty($price), maybe consider doing (empty($price) && $price !== 0 && $price !== "0"). This is a much lengthier, more verbose validation, but it should eliminate the error.

PHP Syntax: "or" in "If" Statement [duplicate]

This question already has answers here:
How to have multiple conditions on the same if statement? [duplicate]
(5 answers)
Closed 8 years ago.
hello I just wanted to check and see if this would be correct PHP syntax:
if ($input == "DeOnTRAY96#localhost"){
echo"
Projects: 1"?>
<br>
<?php
echo"
Admin: yes
";
}
elseif ($input == NULL){
die("Please enter password.");
}else{
header("Location:Invalidpassword.php");
exit;
}
Right where is says
if($input == "DeOnTRAY96#localhost"){
Could I put
if($input == "DeOnTRAY96#localhost" or "somethingelse"){
And still have it work?
You don't want
if($input == "DeOnTRAY96#localhost" or "somethingelse"){
You want
if($input == "DeOnTRAY96#localhost" or $input == "somethingelse"){
I might suggest using === instead of == in this case, as you'd like a type sensitive comparison.
Additionally, for $input == NULL you should use is_null($input). Null is weird in most programming languages, so language specific functions for testing are usually the way to go (rather than comparison)
OR syntax in PHP:
if($var == 'something' || $var == 'something else')
{
//do something
}
For reference:
|| means OR
&& means AND
For a more future-proof solution, consider in_array. I use it for as few as two options, if there's even the slightest chance there may be more added.
if( in_array($input, ["DeOnTRAY96#localhost", "somethingelse"]))
Once you get to four or more options, it's probably better to do something more like:
$whitelist = [
"DeOnTRAY96#localhost"
, "somethingelse"
, "anotheroption"
, "too many options to do inline!"
];
if( in_array($input, $whitelist))

Return integer 0

Sorry for bad english , used Google.translate
There is a code that returns a value to a int, if set . Otherwise it returns false
if (isset($this->variable))
return intval ($this->variable);
else
return false;
On the receiving side condition
if ($return_value) {
// Here code
}
The problem is that if the returned value is 0, this is false, and the code is executed . But as the value of 0 is also important to me . If returned as a string , it is still treated as false.
define ('false', 'value') does not work.
Introduced his constant for this , but you have to rewrite a bunch of code for additional testing
(if($return_value! == my_false_constant)
That is not quite satisfied.
What options are there to solve this problem ?
if ($return_value !== false) {
}
Using !== (or ===) instead of just != or == also tests the type of the value.
Use strict comparison with ===.
See: http://us3.php.net/manual/en/types.comparisons.php
if(1 === true) //returns FALSE
This will work:
(if($return_value !== false){
// do work
}
Comparisons:
== means same value
=== means same value AND same type
! == means not (same value)
!== means not (same value and same type)
SO:
0 == false //is true
0 === false //is false
Simply ! == does not equal !==, as is not valid PHP code

Checking if a variable is null [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
is_null($x) vs $x === null in PHP
In the following context !== null, !is_null() and isset() all produce the same result:
$foo = null;
function foo() {
if ($this->foo !== null) {
...
}
if (!is_null($this->foo)) {
...
}
if (isset($this->foo)) {
...
}
}
Which one is the quickest and which would you recommend in that context?
If you're not sure that the variable exists, use isset.
Example:
$content = (isset($_POST['content'])) ? $_POST['content'] : null;
Otherwise, use strict comparison to null.
if ($content === null) { }
(Really, I'm just pushing my opinion on your with the strict comparison. I just think it looks better than is_null, and it's probably an extremely small bit faster.)
Use isset only if the variable may not be set. I.e. if you do not know whether the variable exists at this point or not. Since you do know that it should exist at this point, don't use isset.
The difference between !== null and !is_null is negligible and mostly depends on your preference. Personally, I like !== null.
if ($this->foo !== null) {
//...
}
I prefer this condition.
According to me
if($this->foo!=""){
//...
}
this is the quickest one and produce the result quickly

PHP: what's an alternative to empty(), where string "0" is not treated as empty? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fixing the PHP empty function
In PHP, empty() is a great shortcut because it allows you to check whether a variable is defined AND not empty at the same time.
What would you use when you don't want "0" (as a string) to be considered empty, but you still want false, null, 0 and "" treated as empty?
That is, I'm just wondering if you have your own shortcut for this:
if (isset($myvariable) && $myvariable != "") ;// do something
if (isset($othervar ) && $othervar != "") ;// do something
if (isset($anothervar) && $anothervar != "") ;// do something
// and so on, and so on
I don't think I can define a helper function for this, since the variable could be undefined (and therefore couldn't be passed as parameter).
This should do what you want:
function notempty($var) {
return ($var==="0"||$var);
}
Edit: I guess tables only work in the preview, not in actual answer submissions. So please refer to the PHP type comparison tables for more info.
notempty("") : false
notempty(null) : false
notempty(undefined): false
notempty(array()) : false
notempty(false) : false
notempty(true) : true
notempty(1) : true
notempty(0) : false
notempty(-1) : true
notempty("1") : true
notempty("0") : true
notempty("php") : true
Basically, notempty() is the same as !empty() for all values except for "0", for which it returns true.
Edit: If you are using error_reporting(E_ALL), you will not be able to pass an undefined variable to custom functions by value. And as mercator points out, you should always use E_ALL to conform to best practices. This link (comment #11) he provides discusses why you shouldn't use any form of error suppression for performance and maintainability/debugging reasons.
See orlandu63's answer for how to have arguments passed to a custom function by reference.
function isempty(&$var) {
return empty($var) || $var === '0';
}
The key is the & operator, which passes the variable by reference, creating it if it doesn't exist.
if(isset($var) && ($var === '0' || !empty($var)))
{
}
if ((isset($var) && $var === "0") || !empty($var))
{
}
This way you will enter the if-construct if the variable is set AND is "0", OR the variable is set AND not = null ("0",null,false)
The answer to this is that it isn't possible to shorten what I already have.
Suppressing notices or warnings is not something I want to have to do, so I will always need to check if empty() or isset() before checking the value, and you can't check if something is empty() or isset() within a function.
function Void($var)
{
if (empty($var) === true)
{
if (($var === 0) || ($var === '0'))
{
return false;
}
return true;
}
return false;
}
If ($var != null)

Categories