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
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()
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];
PHP empty()
vs is_null()
vs isset()
:
+-------+-------+-------+-------+-------+--------------+
| "" | "foo" | NULL | FALSE | 0 | undefined |
+-----------+-------+-------+-------+-------+-------+--------------+
| empty() | TRUE | FALSE | TRUE | TRUE | TRUE | TRUE |
| is_null() | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE (ERROR) |
| isset() | 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.)
If you want to check if the value is non-blank text or any number including zero, it gets trickier:
if (!empty($v) || (isset($v) && ($v === 0 || $v === '0'))) {
// $v is non-blank text, true, 0 or '0'
// $v is NOT an empty string, null, false or undefined
}
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>
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;
}
Not so easy to get the difference between isset()
, is_null()
and empty()
. It is more subtle than 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.