Is there any difference between {strlen($var) == 0} and {empty($var)} - php

considering that the variable only can be String fetched from an HTML Form (input-text, textarea).

Yes, there is a difference between strlen($str)==0 and empty($str). empty returns true if the value is "0". See the PHP type comparison tables.

$var = 0;
strlen( $var ); // 1, coerced to true
empty($var) // true, it's considered "empty", these are the empty ones:
"" (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)

Here is some note I've discovered:
empty(), requires a variable and only a variable, so I think it has a problem when it's dealing with an object value fetched from the magic __get() method.

Related

Why does empty() behave differently for variable and array offset?

I have snippet of code which behaviour I do not understand. TRUE value is giving different result when passed to empty() in different copies of variable.
var_dump($this->controller->type['Company']['is_active']); // bool(true)
property $controller is object with $type property which is multi array. value of is_active is bool(true)
What should be the result of empty()?
var_dump(!empty($this->controller->type['Company']['is_active'])); //false
ok, let's create a copy
$temp = $this->controller->type['Company']['is_active'];
var_dump(!empty($temp)); //true
hmmm different result?
var_dump($this->controller->type['Company']['is_active'] === $temp);//true
what about casting?
var_dump(!empty((int)$this->controller->type['Company']['is_active'])); //true
Could someone explain this behaviour please?
Please refere php manual, http://php.net/manual/en/function.empty.php
Returns FALSE if var exists and has a non-empty, non-zero value.
Otherwise returns TRUE.
Note that empty works with pass by variable not with pass by value.
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)

What is difference between if($a) and if($a==1)?

function foo($a)
{
if($a) {return "a";}
else if($a==2) {return "b";}
else {return "c";}
}
-----------------------------------------
function foo2($a)
{
if($a==1){return "a";}
if($a==2){return "b";}
if($a==3){return "c";}
}
when i passed value any number frim 1,2 or 3 ? it will return 1. when i pass value in function foo2 it will return value as value passed .
But why is difference coming ?
When you do something like if ($a), consider that PHP is a weak typed language. To understand what is evaluated in if ($a), see the conversion rules for booleans.
Quoting from the manual:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
All the others are considered TRUE, including -1 (remarkable!).
On the other hand, when you do if ($somevar == 1), then $somevar == 1 is already a boolean, and conversion rules don't apply.
if($a) any non-zero value is true.
if($a==1) only 1 value is true.
If $a is true, it will not return 1, and that is the difference.
if($a) means the same as: "if a is true, do this"
In PHP (and most other languages), the value of 0 means false and any other value is 'true'

setting variables using logcial OR

$db_hased_pw = $result["password"] || false;
I understand the usage of this line of code, but WHEN will it evaluate as false?
will $db_hased_pw equal false only when $result["password"] is undefined?
or is $db_hased_password be set to false if $result["password"] is unset, or false, or zero, or null?
It will evaluate to false when $result["password"] is a "falsy value". The page on empty describes these values.
These would be equivalent:
$db_hased_pw = !!$result["password"];
$db_hased_pw = (bool) $result["password"];
If $result["password"] can indeed be undefined, you should be using:
$db_hased_pw = !empty($result["password"]);
to avoid a notice.
From PHP documentation:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
You can read more on this page.

should i use empty() php function

which is better?
if (!empty($val)) { // do something }
and
if ($val) { // do something }
when i test it with PHP 5, all cases produce the same results. what about PHP 4, or have any idea which way is better?
You should use the empty() construct when you are not sure if the variable even exists. If the variable is expected to be set, use if ($var) instead.
empty() is the equivalent of !isset($var) || $var == false. It returns true if the variable is:
"" (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)
Read the manual:
empty() is the opposite of (boolean) var, except that no warning is
generated when the variable is not set.

(isset($someValue) && ($someValue != '')) vs. !empty($someValue) - are they the same?

Are the below two statements similar?
if (isset($someValue) && $someValue != '')
and
if(!empty($someValue]))
No, they are not the same. Yes, they are similar. The first one checks if the 'someValue' is set and not equal to null plus whether it is not empty string. The second one is true if $_GET['act'] is not set or is on the list of variables treated as empty and listed on documentation page.
The following example from the same documentation page should fill any gaps:
$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';
}
Kind of similar except 0 value.
According to http://us3.php.net/empty
$var = "0";
(!empty($var)) === false;
(isset($var) && $var!='') === true;
As you write it, they're actually almost the opposite, but I'll just assume you mean isset($_GET[someValue]) and !empty.
In that case no, they're not the same. In the first line you are checking for an empty string, but for example a zero value would pass. However, a zero value counts as empty.
Similar but not the same. Empty will return true if $_GET['act'] is equal to any of the following:
"" (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)
Where as the first one only checks to see if the variable has been created and isn't equal to an empty string.

Categories