What is different between isset() and !empty [duplicate] - php

This question already has answers here:
isset vs empty vs is_null
(14 answers)
What's the difference between 'isset()' and '!empty()' in PHP?
(7 answers)
Closed 8 years ago.
I want to know what is different between isset() and !empty?
I know that isset() tests if a variable is set and not null, while
empty() can return true when the variable is set to certain values.
But logically when isset() which doesn't empty (without using "empty()"), there is a variable in text or textarea whatever so we don't need empty().

isset method checks if a variable exists or not. On the other hand !empty knows that a variable exists but it needs to check its value.

The difference is quite small, but significant enough to not make mistakes with both statements. When you declare a variable like this:
<?php
foo = "";
?>
You will end up with different values depending on what you use. isset() will return true, as the variable foo was set to nothing. However !empty will return false, as the variable foo does not contain anything. Basically isset() only checks for NULL values, where !empty checks for for everything that is considered 0 (so NULL, 0, 0.0 etc. but also 0 as a string, for example).

Related

Checking if variable exist and if it has a certain value in one line [duplicate]

This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 months ago.
I have always thought that if I want to check if a variable exists and has a certain value I have to use two if conditions:
if(isset($x)){
if($x->age==5){}
}
But I realized its also possible to do it in one line this way:
if(isset($x) && ($x->age==5)){}
Can someone tell me why the second variation will not result in an error if $x is null. Given that $x is null and doesn't have the property age? Would it be trying to access a property that doesn't exist?
$x=null;
Because $x is null, isset($x) is false. Then, because of the logical operator "AND" (&&), the condition cannot be fully validated, so, the test is stopped here and ($x->age==5) is not executed.
For a shorter code, as of PHP 8.0.1, you can use the NullSafe Operator (?->)
if ($x?->age == 5) { }

PHP logical operator [duplicate]

This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 4 years ago.
When the isset() is executed the following check $_SESSION['mat'] == "1" is executed or the second verification is directly skipped since it is the first false?
Is there is where I have the doubt?
if(isset($_SESSION['mat']) and $_SESSION['mat']=="1"){}
If the first part of your if statement already returns false the second part will not be evaluated. Your if statement looks good this way and shouldn't throw any index out of bounds errors.
When the isset is executed the following check is executed ($ _ SESSION ['mat'] == "1") or the second verification is directly skipped since it is the first false.
isset() will check if a variable is set otherwise it will return false. All the code inside the if() statement will be executed, so in your case, if the $_SESSION array variable isn't set, the second control, in your case and (that can be expressed also using &&) will be skipped.

Difference between NULL, 0 ,False and ''? [duplicate]

This question already has answers here:
Null vs. False vs. 0 in PHP
(18 answers)
Closed 4 years ago.
What is the exact difference between null, 0, false, and an empty string in PHP? Is null similar to None in python?
Well, after massive comments and then poof deletion of them... may as well try to help you out.
null is the absence of value.
0 can be a numeric value, or a representation of a boolean FALSE, or a string. PHP doesn't really have variable typing, so depending on what you are checking for using ==0 or ===0 or ==false or ===false may be appropriate.
You may want to read over this - https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ as well as the PHP manual for isset() and empty()

isset() is returning only true [duplicate]

This question already has answers here:
isset() function is returning true even when item is not set
(4 answers)
Closed 8 years ago.
I am trying to create validations in PHP to check if fields are empty or not.
But my isset's are returning only true when I'm using
var_dump(isset($_POST['name']));
It's not returning false in any conditions which is not validating my forms properly.
PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.
isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.
isset()
isset — Determine if a variable is set and is not NULL
In other words, it returns true only when the variable is not null.
empty()
empty — Determine whether a variable is empty
In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.
is_null()
is_null — Finds whether a variable is NULL
In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.
An empty string is a set string. It's not the same, you also have to check the length of the string or if it is empty or not

PHP: isset vs is_null - When do I need to use one over another? [duplicate]

This question already has answers here:
isset vs empty vs is_null
(14 answers)
Closed 8 years ago.
I am trying to comprehend how PHP's isset() and is_null() functions are different from one another.
I do understand their difference from the manual, but cannot come up with any example of when I absolutely need to use one function over another.
It seems I can use one or the other and it wouldn't matter, except that isset doesn't throw a Notice error if the reference didn't exist, while is_null would.
If I suppress such type of errors, then in terms of functionality these 2 functions would be exactly similar.
Have I understood it correctly?
null means nothing, no value, whereas you use isset() say whether a variable isset, for example..
$var = null;
isset($var); //returns false, checks whether the variable isset,
//but it's null so will return false
is_null($var); //Returns true, checks whether the value of variable is null
From Docs :
If a variable has been unset with unset(), it will no longer be set.
isset() will return FALSE if testing a variable that has been set to
NULL.
Going further, we alter the variable value
$var = 'hello';
isset($var); //will return true as the variable is no more null
is_null($var); //will return false as variable holds a string now
If I suppress such type of errors
No you shouldn't, always turn on the error reporting, writing bad code won't help you anyways.

Categories