Reputation: 735
I'm trying to write a script that when a user uploads a file and does not enter a name an error is returned. I've tried using is_null, empty, and isset and they all do not work. Eg, below, is_null returns an error even when a name is entered. Can anyone help?
$caption = $_REQUEST[$name_input_name];
if(is_null($caption)) {
$file->error = 'Please Enter a Title';
return false;
}
Upvotes: 41
Views: 39020
Reputation: 2853
PHP empty()
vs is_null()
vs isset()
:
"foo" | "" | 0 | "0" | FALSE | NULL | [] | (object) [] | undefined | |
---|---|---|---|---|---|---|---|---|---|
empty() |
FALSE | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE | FALSE | TRUE |
is_null() |
FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE (ERROR) |
isset() |
TRUE | TRUE | TRUE | TRUE | TRUE | FALSE | TRUE | TRUE | FALSE |
If you want to check if there's any value other than null
or undefined, use isset($var)
(because !is_null()
generates a warning on undefined variables.)
In general, my recommendation is that you use isset($var)
if you want to check if a variable is defined and has any value other than null
. It also avoids warnings on undefined variables, making it safer.
PRACTICAL APPLICATION
If you want to check if a value is non-blank text or any number including zero, it gets a little bit trickier:
if (!empty($var) || $var === 0 || $var === '0') {
// $var is non-blank text, true, 0 or '0'
// $var is NOT an empty string, null, false or undefined
}
UPDATE:
Here are some tests you can run to verify that the above code works:
<?php
function checkValue($v) {
// Return true if a value is non-blank text, true, or zero.
return (!empty($v) || $v === 0 || $v === '0');
}
// Test cases
$testCases = [
'empty string = FALSE' => ['', false],
'null = FALSE' => [null, false],
'false = FALSE' => [false, false],
'undefined = FALSE' => [@$undefined, false], // Suppressing notice for undefined variable
'true = TRUE' => [true, true],
'zero (int) = TRUE' => [0, true],
'zero (string) = TRUE' => ['0', true],
'non-zero int = TRUE' => [42, true],
'non-zero string = TRUE' => ['hello', true],
'float = TRUE' => [3.14, true],
'empty array = TRUE' => [[], false],
'non-empty array = TRUE' => [[1, 2, 3], true],
'object = TRUE' => [(object) ['a' => 1], true],
];
// Run the tests
foreach ($testCases as $description => [$input, $expected]) {
$result = checkValue($input);
$status = $result === $expected ? 'PASS' : 'FAIL';
echo "{$description}: {$status}\n";
}
Upvotes: 21
Reputation: 3615
Not so easy to get the difference between isset()
, is_null()
and empty()
. It is more subtle than we would like to believe.
Since, it is not easy to clearly explain the difference with words, I suggest to look at this page : there is a detailed table containing behavior for each function. It clearly and fully explains the differences.
By the way, I'm truly convinced than many PHP scripts contain security breach due to the misunderstood of the behaviors.
Upvotes: 4
Reputation: 11
I wrote this php page that hopefully will be of help to you. If you run it, it shows the interaction of these particular types of variables with these functions (plus one I made for myself), and also it compares them with each other with the == operator: https://pastebin.com/whPFMams
<!doctype html>
<html>
<head>
<style>
td.first{
font-weight: bold;
}
h3 {
text-align: center;
}
</style>
</head>
<body>
<h3>FUNCTIONS COMPARISON</h3><br>
<pre>
<h4>Variables</h4>
class obj{};
$n;
Array:
"a" => -1,
"b" => 0,
"c" => 0.0,
"d" => '',
"e" => ' ',
"f" => '0',
"g" => "\0",
"h" => true,
"i" => false,
"j" => null,
"k" => array(),
"l" => new obj(),
"m" => new stdClass(),
"n" => $n (undefined)
z = (undeclared)
<h4>My function</h4>
function isvalid($value,$strict=false){
if( !is_null($value) && value!==false && (!empty($value) || $value == 0) ){
if($strict)
{
if(is_bool($value) || is_array($value) || is_object($value))
{return false;}
else
{return true;}
}
else
{return true;}
}
else
{return false;}
}
<h4>Var_dump</h4>
<?php
error_reporting(E_ERROR | E_PARSE);
class obj{};
function isvalid($value,$strict=false){
if( !is_null($value) && $value!==false && (!empty($value) || $value == 0) ){
if($strict)
{
if(is_bool($value) || is_array($value) || is_object($value))
{return false;}
else
{return true;}
}
else
{return true;}
}
else
{return false;}
}
$n;
$values = [
"a" => -1,
"b" => 0,
"c" => 0.0,
"d" => '',
"e" => ' ',
"f" => '0',
"g" => "\0",
"h" => true,
"i" => false,
"j" => null,
"k" => array(),
"l" => new obj(),
"m" => new stdClass(),
"n" => $n
//"z"
];
echo var_dump($values)."\n z = "; echo var_dump($z)."\n";
?>
</pre><br />
<table border = "1">
<tr>
<th>== and other</th>
<?php
echo "<th>"; echo var_dump($values["a"]); echo "<br> : a = -1 </th>";
echo "<th>"; echo var_dump($values["b"]); echo "<br> : b = 0 </th>";
echo "<th>"; echo var_dump($values["c"]); echo "<br> : c = 0.0 </th>";
echo "<th>"; echo var_dump($values["d"]); echo "<br> : d = '' </th>";
echo "<th>"; echo var_dump($values["e"]); echo "<br> : e = ' ' </th>";
echo "<th>"; echo var_dump($values["f"]); echo "<br> : f = '0' </th>";
echo "<th>"; echo var_dump($values["g"]); echo "<br> : g = '/0' </th>";
echo "<th>"; echo var_dump($values["h"]); echo "<br> : h = true </th>";
echo "<th>"; echo var_dump($values["i"]); echo "<br> : i = false </th>";
echo "<th>"; echo var_dump($values["j"]); echo "<br> : j = null </th>";
echo "<th>"; echo var_dump($values["k"]); echo "<br> : k = empty array </th>";
echo "<th>"; echo var_dump($values["l"]); echo "<br> : l = empty object (Notices with numbers)</th>";
echo "<th>"; echo var_dump($values["m"]); echo "<br> : m = empty stdClass (Notices with numbers)</th>";
echo "<th>"; echo var_dump($values["n"]); echo "<br> : n = undefined </th>";
echo "<th>"; echo var_dump($z); echo "<br> : z = undeclared (Notices)</th>";
?>
</tr>
<?php
foreach ($values as $key => $val){
echo '
<tr>
<td class="first">'; echo var_dump($val); echo '<br> : '.$key.'</td>';
foreach ($values as $key2 => $val2){
echo '<td>'; echo $val==$val2 ? var_dump($val==$val2) : /*var_dump($val==$val2).*/''; echo'</td>';
}
echo '<td>'; echo $val==$z ? var_dump($val==$z) : /*var_dump($val==$z).*/''; echo'</td>';
echo '</tr>';
}
//z
echo '
<tr>
<td class="first">'; echo var_dump($z); echo '<br> : z (Notices)</td>';
foreach ($values as $key2 => $val2){
echo '<td>'; echo $z==$val2 ? var_dump($z==$val2) : /*var_dump($z==$val2).*/''; echo'</td>';
}
echo '<td>'; echo $z==$z ? var_dump($z==$z) : /*var_dump($z==$z).*/''; echo'</td>';
echo '</tr>';
//!empty || ==0
echo '
<tr>
<td class="first"> !empty || ==0 </td>';
foreach ($values as $key => $val){
echo '<td>'; echo !empty($val) || $val==0 ? var_dump(!empty($val) || $val==0) : /*var_dump(!empty($val) || $val==0.*/''; echo'</td>';
}
echo '<td>'; echo !empty($z) || $z==0 ? var_dump(!empty($z) || $z==0) : /*var_dump(!empty($z) || $z==0).*/''; echo'</td>';
echo '</tr>';
//isvalid
echo '
<tr>
<td class="first"> isvalid() </td>';
foreach ($values as $key => $val){
echo '<td>'; echo isvalid($val) ? var_dump(isvalid($val)) : /*var_dump(isvalid($val)).*/''; echo'</td>';
}
echo '<td>'; echo isvalid($z) ? var_dump(isvalid($z)) : /*var_dump(isvalid($z)).*/''; echo'</td>';
echo '</tr>';
//isvalid strict
echo '
<tr>
<td class="first"> isvalid(strict) </td>';
foreach ($values as $key => $val){
echo '<td>'; echo isvalid($val,true) ? var_dump(isvalid($val,true)) : /*var_dump(isvalid($val,true)).*/''; echo'</td>';
}
echo '<td>'; echo isvalid($z,true) ? var_dump(isvalid($z,true)) : /*var_dump(isvalid($z,true)).*/''; echo'</td>';
echo '</tr>';
//!is_null
echo '
<tr>
<td class="first"> !is_null </td>';
foreach ($values as $key => $val){
echo '<td>'; echo !is_null($val) ? var_dump(!is_null($val)) : /*var_dump(!is_null($val)).*/''; echo'</td>';
}
echo '<td>'; echo !is_null($z) ? var_dump(!is_null($z)) : /*var_dump(!is_null($z)).*/''; echo'</td>';
echo '</tr>';
//isset
echo '
<tr>
<td class="first"> isset (No Notices)</td>';
foreach ($values as $key => $val){
echo '<td>'; echo isset($val) ? var_dump(isset($val)) : /*var_dump(isset($val)).*/''; echo'</td>';
}
echo '<td>'; echo isset($z) ? var_dump(isset($z)) : /*var_dump(isset($z)).*/''; echo'</td>';
echo '</tr>';
//!empty
echo '
<tr>
<td class="first"> !empty (No Notices) </td>';
foreach ($values as $key => $val){
echo '<td>'; echo !empty($val) ? var_dump(!empty($val)) : /*var_dump(!empty($val)).*/''; echo'</td>';
}
echo '<td>'; echo !empty($z) ? var_dump(!empty($z)) : /*var_dump(!empty($z)).*/''; echo'</td>';
echo '</tr>';
// if
echo '
<tr>
<td class="first"> if </td>';
foreach ($values as $key => $val){
echo '<td>'; echo $val ? var_dump(true) : /*var_dump(false).*/''; echo'</td>';
}
echo '<td>'; echo $z ? var_dump(true) : /*var_dump(false).*/''; echo'</td>';
echo '</tr>';
?>
<tr>
<th>functions</th>
<?php
echo "<th>"; echo var_dump($values["a"]); echo "<br> : a = -1 </th>";
echo "<th>"; echo var_dump($values["b"]); echo "<br> : b = 0 </th>";
echo "<th>"; echo var_dump($values["c"]); echo "<br> : c = 0.0 </th>";
echo "<th>"; echo var_dump($values["d"]); echo "<br> : d = '' </th>";
echo "<th>"; echo var_dump($values["e"]); echo "<br> : e = ' ' </th>";
echo "<th>"; echo var_dump($values["f"]); echo "<br> : f = '0' </th>";
echo "<th>"; echo var_dump($values["g"]); echo "<br> : g = '/0' </th>";
echo "<th>"; echo var_dump($values["h"]); echo "<br> : h = true </th>";
echo "<th>"; echo var_dump($values["i"]); echo "<br> : i = false </th>";
echo "<th>"; echo var_dump($values["j"]); echo "<br> : j = null </th>";
echo "<th>"; echo var_dump($values["k"]); echo "<br> : k = empty array </th>";
echo "<th>"; echo var_dump($values["l"]); echo "<br> : l = empty object (Notices with numbers)</th>";
echo "<th>"; echo var_dump($values["m"]); echo "<br> : m = empty stdClass (Notices with numbers)</th>";
echo "<th>"; echo var_dump($values["n"]); echo "<br> : n = undefined </th>";
echo "<th>"; echo var_dump($z); echo "<br> : z = undeclared (Notices)</th>";
?>
</tr>
</table>
<br>
(Notices) : This line is full of Notices. <br>
(Notices with numbers) : This line has Notices about number types interactions. <br>
(No Notices) : This line has absolutely no Notices.
</body>
</html>
Upvotes: 1
Reputation: 1387
is_null()
emits a WARNING if variable is not set, but isset()
and empty()
don't.
$a - variable with not null value (e.g. TRUE)
$b - variable with null value. `$b = null;`
$c - not declared variable
$d - variable with value that cast to FALSE (e.g. empty string, FALSE or empty array)
$e - variable declared, but without any value assigned
$a->a - declared, but not assigned object property. (`public $a;`)
A::$a - declared, but not assigned static class property.
| $a | $b | $c | $d | $e | $a->a | A::$a |
---------+-------+-------+-------+-------+-------+-------+-------+
is_null()| FALSE | TRUE |TRUE*W | FALSE | TRUE*W| TRUE | TRUE |
---------+-------+-------+-------+-------+-------+-------+-------+
isset() | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE |
---------+-------+-------+-------+-------+-------+-------+-------+
empty() | FALSE | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE |
---------+-------+-------+-------+-------+-------+-------+-------+
null === | FALSE | TRUE |TRUE*W | FALSE | TRUE*W| TRUE | TRUE |
---------+-------+-------+-------+-------+-------+-------+-------+
null == | FALSE | TRUE |TRUE*W | TRUE | TRUE*W| TRUE | TRUE |
---------+-------+-------+-------+-------+-------+-------+-------+
TRUE*W - function return TRUE, but same time emits WARNING.
On empty() function documentation page you can read, that:
The following things are considered to be empty:
....
$var; (a variable declared, but without a value)
It can be misleading that code $var;
is defining a variable, but does not assign any value to it, but it is wrong. Variable $var
is still undefined and type recognize functions, like is_null()
emits warnings if you pass $var
as an argument.
But it is not right for unsettled class or object properties. Declaring them without assigning some value automatically assigns NULL.
UPD Typed properties in PHP 7.4 DO NOT assigned by NULL by default. If you does not set any value to them, they are considered as unassigned.
Some low level descriptions:
isset()
and empty()
are core functions, that will be compiled directly to specific opcode according to zval type:
ZEND_ISSET_ISEMPTY_THIS
ZEND_ISSET_ISEMPTY_CV
ZEND_ISSET_ISEMPTY_VAR
ZEND_ISSET_ISEMPTY_DIM_OBJ
ZEND_ISSET_ISEMPTY_PROP_OBJ
ZEND_ISSET_ISEMPTY_STATIC_PROP
Furthermore they will compile by the same function zend_compile_isset_or_empty
Function is_null()
is type recognizer function, like is_numeric
, is_recource
, is_bool
, etc. And will be called like user-defined function with opcodes INIT_FCALL_BY_NAME/DO_FCALL_BY_NAME
and so.
/* {{{ proto bool is_null(mixed var)
Returns true if variable is null
Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
PHP_FUNCTION(is_null)
{
php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
}
Upvotes: 36
Reputation: 81
Ref this: different between isset, is_null, empty in PHP
Upvotes: 0
Reputation: 3105
isset() — Determine if a variable is set and not NULL
empty() - Determine if a variable is empty.
is_null() - Determine if a variable is null
Upvotes: 0
Reputation: 7706
is_null is the dual of isset except isset does not print notices if the variable is null.
>$ciao;
>var_export(is_null($ciao));
>PHP Notice: Undefined variable: ciao in php shell code on line 1
>true
>var_export(!isset($ciao));
>true
Upvotes: 0
Reputation: 1019
You can try this :
if(!isset($_REQUEST['name_input_name']))
{
$file->error = 'Please Enter a Title';
return false;
}
$caption = $_REQUEST['name_input_name'];
Note : $_REQUEST is an Global array that store the data in key=>value pair. consider "name_input_name" as value extracted from server.
if name_input_name is set to some value code will skip if block and store the value to variable $caption.
Upvotes: 1
Reputation:
From PHP manual – 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.
From PHP Manual – 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.
From PHP Manual – 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.
Upvotes: 8
Reputation: 1654
In these php functions empty(),isset() & is_null
are mainly used to test the variables.but each functions have different functionalists.
Credits : empty() , isset(), is_null()
Upvotes: -2
Reputation: 13822
isset()
will check if the variable is set, ie
<?php
echo isset($var); // false
$var = 'hello';
empty()
will check if the variable is empty, ie
<?php
$emptyString = '';
echo empty($emptyString); // true
is_null()
will check for NULL
which is different from empty, because it's set to NULL
not an empty string. (NULL might be a confusing concept)
Since your title is a string, I think you want to be using empty()
if (!isset($_REQUEST[$name_input_name]) || empty($_REQUEST[$name_input_name])) {
$file->error = 'Please Enter a Title';
return false;
}
Upvotes: 17
Reputation: 19
I use strlen to count the number of characters
if (strlen($_REQUEST['name_input_name']) < 1) {
$file->error = 'Please Enter a Title';
return false;
}
Upvotes: 1
Reputation: 225291
I think you meant to use isset
before you assigned it to something:
if(!isset($_REQUEST[$name_input_name]))
{
$file->error = 'Please Enter a Title';
return false;
}
$caption = $_REQUEST[$name_input_name];
Upvotes: 5