check if variable is null - incorrect validation - php

This problem is a bit strange. Why is showed "Is not null", if the value sent is null? Any reason for that?
Parametersapplication/x-www-form-urlencoded
lists_owned null
Source
lists_owned=null
<?php
$lists_owned = $_POST['lists_owned'];
var_dump($lists_owned); // string(4) "null"
if(!is_null($_POST['lists_owned'])) {
echo "Is not null"; I see this echo
}
?>
thanks

"null" is not null. If you want to check for "null" then you should be using equality.
if($_POST['lists_owned'] != 'null') {

This is because your post value is a string called 'null' and not an actual null value.

Looks like your value is actually the string "null", not the value null. Ie
<?php
$x = "null";
$y = null;
var_dump($x);
var_dump($y);
?>
Output
string(4) "null"
NULL

Related

What is the actual function difference as to its application on a code, between if(isset) and !empty()....based on my code here? [duplicate]

Could you help me to improve my coding style?:) In some tasks I need to check - is variable empty or contains something. To solve this task, I usually do the following.
Check - is this variable set or not? If it's set - I check - it's empty or not?
<?php
$var = '23';
if (isset($var)&&!empty($var)){
echo 'not empty';
}else{
echo 'is not set or empty';
}
?>
And I have a question - should I use isset() before empty() - is it necessary? TIA!
It depends what you are looking for, if you are just looking to see if it is empty just use empty as it checks whether it is set as well, if you want to know whether something is set or not use isset.
Empty checks if the variable is set and if it is it checks it for null, "", 0, etc
Isset just checks if is it set, it could be anything not null
With empty, the following things are considered empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
From http://php.net/manual/en/function.empty.php
As mentioned in the comments the lack of warning is also important with empty()
PHP Manual says
empty() is the opposite of (boolean) var, except that no warning is
generated when the variable is not set.
Regarding isset
PHP Manual says
isset() will return FALSE if testing a variable that has been set to NULL
Your code would be fine as:
<?php
$var = '23';
if (!empty($var)){
echo 'not empty';
}else{
echo 'is not set or empty';
}
?>
For example:
$var = "";
if(empty($var)) // true because "" is considered empty
{...}
if(isset($var)) //true because var is set
{...}
if(empty($otherVar)) //true because $otherVar is null
{...}
if(isset($otherVar)) //false because $otherVar is not set
{...}
In your particular case: if ($var).
You need to use isset if you don't know whether the variable exists or not. Since you declared it on the very first line though, you know it exists, hence you don't need to, nay, should not use isset.
The same goes for empty, only that empty also combines a check for the truthiness of the value. empty is equivalent to !isset($var) || !$var and !empty is equivalent to isset($var) && $var, or isset($var) && $var == true.
If you only want to test a variable that should exist for truthiness, if ($var) is perfectly adequate and to the point.
You can just use empty() - as seen in the documentation, it will return false if the variable has no value.
An example on that same page:
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>
You can use isset if you just want to know if it is not NULL. Otherwise it seems empty() is just fine to use alone.
Here are the outputs of isset() and empty() for the 4 possibilities: undeclared, null, false and true.
$a=null;
$b=false;
$c=true;
var_dump(array(isset($z1),isset($a),isset($b),isset($c)),true); //$z1 previously undeclared
var_dump(array(empty($z2),empty($a),empty($b),empty($c)),true); //$z2 previously undeclared
//array(4) { [0]=> bool(false) [1]=> bool(false) [2]=> bool(true) [3]=> bool(true) }
//array(4) { [0]=> bool(true) [1]=> bool(true) [2]=> bool(true) [3]=> bool(false) }
You'll notice that all the 'isset' results are opposite of the 'empty' results except for case $b=false. All the values (except null which isn't a value but a non-value) that evaluate to false will return true when tested for by isset and false when tested by 'empty'.
So use isset() when you're concerned about the existence of a variable. And use empty when you're testing for true or false. If the actual type of emptiness matters, use is_null and ===0, ===false, ===''.
Empty returns true if the var is not set. But isset returns true even if the var is not empty.
$var = 'abcdef';
if(isset($var))
{
if (strlen($var) > 0);
{
//do something, string length greater than zero
}
else
{
//do something else, string length 0 or less
}
}
This is a simple example. Hope it helps.
edit: added isset in the event a variable isn't defined like above, it would cause an error, checking to see if its first set at the least will help remove some headache down the road.

Not null bug in PHP or not?

I don't know if it is a bug or what but here it is. I var_dump my
variable status_id it says:
var_dump($status_id);
string(4) "null"
but when I var_dump it says:
var_dump($status_id != NULL);
bool(true)
How can I transform it to null again because I need it to my filter?
You $status_id is a string 'null', not null or NULL. Check the live demo for a good understanding.
You can compare it with $status_id == 'null'
or set it to null with $status_id = $status_id == 'null' ? null : $status_id;
Your variable $status_id is not null, it has a string value as "null". Check this out:
<?php
$val="null";
var_dump($val);
// Output: string(4) "null"
$val2;
var_dump($val2);
// Output: NULL
Just this var_dump($status_id != NULL); will trigger an PHP Notice Error
Notice: Undefined variable: status_id
Better to use
if( !isset($status_id) ) {}
Or if it is set, but without value
if( !empty($status_id) ) {}
The PHP var_dump() function returns the data type and value:
so you are getting string value "null" not NULL

How to distinguish between "0" and null?

I have a database with some football scores (goal differences).
p12_score1 contains 3, -2, 0 and NULL
All I want to do is to print all score differences (including 0) and ignore the row that has no value (NULL in mysql). However is_null also considers the value 0 a null and won't print it.
IF(!$p12_score1==NULL){
$equal[] = $p12_score1;
}
else {
echo "null";
}
print_r ($equal);
is_null does not consider the value 0 equal to null.
Your code uses == for the comparison, which does not make this distinction. Use the identical operator === or is_null instead:
// One way to do it:
if(!$p12_score1 !== NULL)
// Another:
if(!is_null($p12_score1))
However is_null also considers the value 0 a null and won't print it.
No. http://codepad.org/mjiGfhTB
However, you use the comparisong operator
$p12_score1==NULL
and in this case: In loosely typed languaged null is equal to 0 (and some others ;)). However, PHP supports the identity-comparisong
$p12_score1===NULL
A sidenote: !$a == $b is slightly ugly. Use the real "not equal" !=, or !==.
Try this:
IF(!empty($p12_score1)){
$equal[] = $p12_score1;
}
else {
echo "null";
}
print_r ($equal);
Try
if($p12_score1 !== null) {
$equal[] = $p12_score1;
} else {
echo "null";
}
print_r ($equal);
If you want to do this in PHP instead of sql you have to change your code to this:
IF (!$p12_score1 === NULL)
This way you check that $p12_score1 is exactly null.
The function is_null should work. Another option is is_int. I tried both and both work correctly.

php : How to check a field have blank/empty/NULL value?

I want to display an error when a variable have a BLANK value or EMPTY or NULL value. for example variable is shown below:
$mo = strtotime($_POST['MondayOpen']);
and
var_dump($_POST['MondayOpen']) returns string(0) "".
Now I go with below approach
First want to find which type of variable $mo is ?(string or
integer or other)
Which function is better to find that $mo having no value.
I conduct a test with $mo and got these results
is_int($mo);//--Return nothing
is_string($mo); //--Return bool(false)
var_dump($mo); //--Return bool(true)
var_dump(empty($mo));//--Return bool(true)
var_dump($mo==NULL);//--Return bool(true)
var_dump($mo=='');//--Return nothing
Please suggest an optimum and right approach to check the variable integrity
var_dump outputs variables for debugging purposes, it is not used to check the value in a normal code. PHP is loosely typed, most of the time it does not matter if your variable is a string or an int although you can cast it if you need to make sure it is one, or use the is_ functions to check.
To test if something is empty:
if ( empty( $mo ) ) {
// error
}
empty() returns true if a variable is 0, null, false or an empty string.
doing strtotime will return false if it cannot convert to a time stamp.
$mo = strtotime($_POST['MondayOpen']);
if ($mo !== false)
{
//valid date was passed in and $mo is type int
}
else
{
//invalid date let the user know
}
PHP offers a function isset to check if a variable is not NULL and empty to check if a variable is empty.
To return the type, you can use the PHP function gettype
if (!isset($mo) || is_empty($mo)) {
// $mo is either NULL or empty.
// display error message
}
You can check its type using:
gettype($mo);
but null and empty are different things, you can check with these functions:
if (empty($mo))
{
// it is empty
}
if (is_null($mo))
{
// it is null
}
Another way to check if variable has been set is to use the isset construct.
if (isset($mo))
{
// variable has been set
}

Unexpected PHP behaviour in isset and empty

array(4) { ["id"]=> int(0) ["pass"]=> string(0) "" ["log_in"]=> string(3) "no" }
The problem in id when using isset it return true because it's 0. When using empty I think it's doing the same. What is the best function to know whether it's set or not?
empty returns false for these:
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var (a variable declared, but without a value in a class)
You need to use isset instead to check if it is there.
See this article on empty vs isset for more info.
if($array['id'] === 0) {
true
} ?
a simple if condition will do for you
if(id)
{
}
try it if it works.
if id is a database id is positive integer
so if($id>0) is one simple solution
OR
preg_match('#^[0-9]*$#', $id)
another
Take a look at this chart on the PHP site.

Categories