Not null bug in PHP or not? - php

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

Related

use php NULL like array

<?php
ini_set('display_errors',1);
ini_set('error_reporting',-1);
$data = null;
var_export( $data['name']);
echo PHP_EOL;
var_dump($data['name']);
Why the result is null, but no notice or warning occured?
Because null is an undefined type, $data['name'] therefore creates its own array.
If you check the data type of $data before assigning null, you will get the undefined variable notice.
echo gettype($data);
$data = null;
After you assigned null to $data, you will see NULL for its value and its data type.
$data = null;
echo gettype($data);
var_dump($data);
According to the documentation of NULL, you're getting NULL for $data['name'] means that it has not been set to any value yet.
The special NULL value represents a variable with no value. NULL is
the only possible value of type null.
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
The following two examples show that the previously defined variable is not auto-converting to array, and keep its original data type.
Example 1:
$data = null; // $data is null
var_dump($data['name']); // null
var_dump($data[0]); // null
Example 2:
$data = 'far'; // $data is string
$data[0] = 'b'; // $data is still string
echo $data; // bar
Just reread documentation about type casting in php.
http://php.net/manual/en/language.types.type-juggling.php
PHP is not so strict as c/c++ or java :-)

php undefined index notice not raised when indexing null variable

I'm curious to know if the following behaviour in PHP is intended or not. And, if it is intended, it is considered acceptable to initialize an array from a null variable by creating an index into it (as is done in the first code snippet)?
error_reporting(E_ALL);
$arr = null;
echo ($arr["blah"]===null) ? "null" : $arr["blah"];
$arr["blah"] = "somevalue";
echo "<br>";
echo ($arr["blah"]===null) ? "null" : $arr["blah"];
var_dump ($arr);
This outputs
null
somevalue
array (size=1)
'blah' => string 'somevalue' (length=9)
However, if the array is initialized first (see code below), I get the exact same output, but an "Undefined Index" notice is given when I first try $arr["blah"]
error_reporting(E_ALL);
$arr = array();
echo ($arr["blah"]===null) ? "null" : $arr["blah"];
$arr["blah"] = "somevalue";
echo "<br>";
echo ($arr["blah"]===null) ? "null" : $arr["blah"];
var_dump ($arr);
PHP won't attempt the comparison if the array is null.
In the second circumstance, a comparison does occur because the array is set. PHP does not check to see if it is empty.
Your ternary is attempting to access the variable $arr["blah"], not checking to see if it is set before doing a comparison.
The proper way to write this would be:
error_reporting(E_ALL);
$arr = array();
if(isset($arr["blah"])) echo ($arr["blah"]===null) ? "null" : $arr["blah"];
$arr["blah"] = "somevalue";
echo "<br>";
if(isset($arr["blah"])) echo ($arr["blah"]===null) ? "null" : $arr["blah"];
var_dump ($arr);
Actually, John Vargo was correct. If a variable is null, accessing it as if it were an array will simply return null without notices. This will change in the upcoming 7.4 version, then it will produce a notice.
Notice: Trying to access array offset on value of type null
The actual output is still the same.

PHP check for valid function return

I have this code:
$myVariable = someRanDomFunction($blah)
The problem is that someRanDomFunction() might return an array, an object, an empty array, boolean value or NULL.
What if the best way to check that $myVariable has some data?
right now i'm doing this:
!empty($myVariable )
Will it cover all cases?
or maybe i should do ($myVariable != NULL && !empty($myVariable ))?
UPDATE*
By 'some data' i mean TRUE if its bool, not empty array, and any value other than NULL
var_dump(empty(NULL)); // returns bool(true)
empty is enough. If you say a boolean is no data, check also for !is_bool. (empty(false) returns also true)
Or better, use only if ($result); this should be enough.
Or what do you call "some data"?
UPDATE: What you need:
php > var_dump(!empty([]));
bool(false)
php > var_dump(!empty(""));
bool(false)
php > var_dump(!empty(false));
bool(false)
php > var_dump(!empty(true));
bool(true)
php > var_dump(!empty(null));
bool(false)
Simply doing an implicit boolean check will exclude false, array(), and null.
if ($myVariable) {
//will execute as long as myVariable isn't an empty array, false, null, or 0 / '0'
}
!empty() does exactly the same thing, with added suppression of warnings about undefined variables / array indices. It's a little wordier, though.
If you check for null, you need to use !== (strict checking http://php.net/manual/en/language.operators.comparison.php)
If your someRanDomFunction might return an array, object, bool or null this is bad way. Something wrong with logic.
However you might using an OR (||) operator, not AND.
($myVariable != NULL || !empty($myVariable) || $myVariable == false)
If you expect some specific data beforehand, it is better to check for it explicitly. PHP has a bunch of functions to do so they start with is_
Basically empty is checking for null, so your second way is redundant.
You can also use isset
if the array, the object and boolean true is data AND The rest: an empty array, boolean false value or NULL, are not "data".
The solution then, would be this one:
$myVariable = someRanDomFunction($blah);
if(!empty($myVariable)) {
//...
}

check if variable is null - incorrect validation

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

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