Unexpected PHP behaviour in isset and empty - php

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.

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.

How To resolve !empty function issue in php 5.4

I used !empty function multiple places.
But It not woring on php 5.4.
It giving error can not use return type context.
So I create a function for checking null values in php.
function not_empty($value) {
if(trim($value)==null or trim($value) == '0' or trim($value)=="0"){
return false;
}
else{
return true;
}
}
So Can I use This Instead of !empty in my project.
The following things are considered to be 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; (a variable declared, but without a value)
so no way you can consider your statement to be anywhere close to !empty in php
What you are doing is equivalent only to first condition i.e., empty string.
and also null too. But not false and 0.
I hope u got it

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)) {
//...
}

How do I check if a $_GET parameter exists but has no value?

I want to check if the app parameter exists in the URL, but has no value.
Example:
my_url.php?app
I tried isset() and empty(), but don’t work. I’ve seen it done before and I forgot how.
Empty is correct. You want to use both is set and empty together
if(isset($_GET['app']) && !empty($_GET['app'])){
echo "App = ".$_GET['app'];
} else {
echo "App is empty";
}
empty should be working (if(empty($_GET[var]))...) as it checks the following:
The following things are considered to be 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; (a variable declared, but without a value)
Here are your alternatives:
is_null - Finds whether a variable is NULL
if(is_null($_GET[var])) ...
defined - Checks whether a given named constant exists
if(defined($_GET[var])) ...
if( isset($_GET['app']) && $_GET['app'] == "")
{
}
You can simply check that by array_key_exists('param', $_GET);.
Imagine this is your URL: http://example.com/file.php?param. It has the param query parameter, but it has not value. So its value would be null actually.
array_key_exists('param', $_GET); returns true if param exists; returns false if it doesn't exist at all.

PHP/MYSQL: check if variable is empty

I want to prevent an empty value from going into a MySQL database
I'm using following but for some reason it is letting the empty values get through...
if (trim($var)= '' || !isset($var)){
header("Location:page.php?er=novariablel");
}
else {
...insert into database
}
Note, there is a bunch of complicated stuff that sets the value of var which is why I want to have both the ='' and the !isset because either might be the case.
Am I missing something with the or statement, i.e. it evaluates to false if both are true. Or what am I doing wrong?
You're lacking an = for your Equal Comparison Operator inside your if statement. Try:
if (trim($var) == '' || !isset($var)){
Try:
if (!isset($var) || empty(trim($var))){
empty() is a better way to check to see if a variable has no value. Just keep in mind that the following will return true:
The following things are considered to be 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)
Try:
$var = trim($var);
if (!empty($var)){
//not empty
}
Try
if (strlen(trim($var)))==0
You should also have put a constraint in your database table attribute that it will not accept NULL values. Then your entry would have not been made in the database.
#John this is great advice.
empty(trim($var))
I've been programming for 6 years and I never thought of trimming the variable before checking to see if it's empty.

Categories