How to differentiate between 0 and an empty variable? - php

Lets say I have a variable $a that holds an integer between 0 to 10. $a needs to be not empty/not null to run a code. It works when the integer is between 1 to 10, but when it is 0, it treats $a as empty.
$a = 5;
if (!empty($a))
{ echo "not empty"; }
else
{ echo "empty";}
This this case it echoes "not empty".
$a = 0;
if (!empty($a))
{ echo "not empty"; }
else
{ echo "empty";}
But in this case it echoes "empty".
Is there a way to differentiate between 0 and an empty variable?

check for null an a string length of 0
if(null === $a || strlen($) === 0) {
}

Instead of empty you could use isset: http://fr2.php.net/isset
if (isset($a))
{ echo "not empty"; }
else
{ echo "empty";}

You can use the strict equality operator === to test for zero:
$a = 0;
$isZero = $a === 0; // $isZero is true

There is a difference between EMPTY and NULL. For $a = 0, $a is EMPTY but not NULL. So you can use isset($a) to check if $a is NULL or not.
if (isset($a)) {
echo 'not null';
} else {
echo 'is null';
}

Related

Assign variable in if condition

I am trying to use a text variable value in an if condition parathesis.
Using PHP
<?php
$a = 1;
$b = 2;
$condition = '$a < $b';
if($condition)
{ echo "Condition is TRUE"; } else
{ echo "Condition is FALSE"; }
?>
Expected Answer:
Condition is TRUE

how works or operator in if statements and print

I have two questions
1) how can i make php to check getnext() function,if it exists or what value it has ???
$a = 1;
if($a == 1 or getnext()== 1){
echo "yeap"; //this works
}
2)i want to write condition -- if $a or $b is equal to 1 , print the variable name that has the value of 1.
Is it possible to do in php ??can i do it this way???
if($a ==1 or $b==1){
print($a or $b);
}
thanks in advance:)
You could for example do as follows :
if($a ==1 || $b==1){
print (($a == 1)? $a : $b);
}
For your first question,
your getnext() function must be return some valid integer value then only you can compare it with integer 1.
for second,
You should write
if($a == 1 && $b == 1){
echo 'both are 1';
}
else if($a == 1){
echo '$a is 1';
}
else if($b == 1){
echo '$b is 1';
}else{
// both are not 1
}
also see this links,
logical operators or vs || (double pipe) in php
PHP: return value from function and echo it directly?
You write condition? You use if and else, if no else in if you put return. Goodluck

If condition and empty variable

I have this code:
If(!isset($a) || empty($a))
{
// code to run when $a not set or empty;
}
Elseif ($a==0)
{
//code to run when $a is equal 0
}
Else
{
//code to run in all other scenarios
}
The issue is that when $a is equal 0 then empty($a) is true and the first code runs. I need the second one to run. How do I do it?
if (isset($a) && $a == 0)
{
//code to run when $a is equal 0
}
elseif (empty($a))
{
// code to run when $a not set or empty;
}
else
{
//code to run in all other scenarios
}
Try this:
if((!isset($a) || empty($a)) && $a !== 0)
{
// code runs when $a not set or empty and $a is not 0;
}
elseif ($a === 0)
{
//code runs when $a is equal 0
}
else
{
//code runs in all other scenarios
}
Update:
Changed to typesafe comparison.
replace this and try
If(!isset($a) || $a=='')
{
// code to run when $a not set or empty;
}
Elseif ($a==0)
{
//code to run when $a is equal 0
}
Else
{
//code to run in all other scenarios
}
I found the solution:
if (!isset($a) || (empty($a) && $a!==0))
{
//run code if $a is not set or is empty
}
elseif ($a===0)
{
//run code if $a is 0;
}
else
{
//all other scenarios
}
Empty function returns false when 0 (0 as an integer).
So your code should be
If(!isset($a))
{
// code to run when $a not set or empty;
}
Elseif ($a==0)
{
//code to run when $a is equal 0
}
Else
{
//code to run in all other scenarios
}

IF condition: Passing first assigned variable as a parameter of a function on second condition

I want to do something like this:
if( $a = 'something' && $b = substr( $a, 2 ) )
{
//do something
}
I mean, on an if condition, evaluate two conditions, and the second one passing the first assigned $a as a parameter to the second condition function substr().
It is just an example, so I don't want answers to this functionality, just generic answers.
The above code throws 'Undefined' $a, since $a is not still assigned.
I could do the next:
if( $a = 'something')
{
if( $b = substr( $a, 2 ) )
//do something
}
}
but this will make my code bigger.
Is there any way to achieve something like the first example?
Edit:
I don't want to compare. Just assign and ensure that $a and $b are not null, false, ...
Your only problem is the wrong precedence of the && and = operators. This works just fine:
if (($a = 'something') && $b = substr($a, 2))
This way, $a is undefined:
if ($a = 'something' && $b = substr($a, 2))
But if you give the = operator priority:
if (($a = 'something') && $b = substr($a, 2))
It will be set.
Moreover, you can simply write:
if( $b = substr( $a = 'something', 2 ) )
This question intrigued me along with #moonwave99 answer, so I did some testing with his last answer.
if( $b = substr( $a = NULL, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = FALSE, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = 0, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = TRUE, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = 233, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
PASS
if( $b = substr( $a = "SOMETHING", 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
PASS
The only way to get it to fail was to pass the Boolean TRUE. But if you are expecting string values, it should fail all Boolean values, zero and NULL and evaluate to true on ints, floats, and string values. (Haven't tested with array, but I suspect it would fail for any non-primitive types). Interesting question.
Use isset() for that.Also keep in mind use == or === for comparison operations since = is assignment operator
if( (isset($a) && $a == 'something') && (isset($b) && $b == substr( $a, 2 )) )
{
//do something
}

PHP loose typing in while loop comparison

Given the following code snippet:
$i= 11;
function get_num() {
global $i;
return (--$i >= 0) ? $i : false;
}
while($num = get_num()) {
echo "Number: $num\n";
}
Results in the following output:
Number: 10
Number: 9
Number: 8
Number: 7
Number: 6
Number: 5
Number: 4
Number: 3
Number: 2
Number: 1
However, I also want it to output Number: 0 - but the while loop evaluates 0 as being false, so the loop never gets to that point. How do I get the loop to terminate only on an explicit false?
while( ($num = get_num()) !== false ) {
extra = forces type check as well.
<?php
$i= 11;
function get_num() {
global $i;
return (--$i >= 0) ? $i : false;
}
while(($num = get_num())!==false) {
echo "Number: $num\n";
}
?>
You have to do a comparison that compares the types, and not only the values -- which means using the === or !== operators, instead of == or !=.
So, you could use something like :
while(($num = get_num()) !== false) {
echo "Number: $num\n";
}
With this, 0 will not be considered as the same thing as false.
As reference : Comparison Operators (quoting)
$a == $b : Equal : TRUE if
$a is equal to $b. $a === $b
: Identical : TRUE if $a is
equal to $b, and they are of the
same type.
I noticed that you are using global. Some devs scream about using global when not necessary. Also, you can leave out the parentheses if you write false!== before your $num declaration (no big deal, just a note).
Option 1
function get_num() {
static $i=11; // effectively only declare the value once
return (--$i >= 0) ? $i : false;
}
while(false!==$num=get_num()){
echo "Number: $num\n"; // $i is NOT available outside the scope of the function.
}
Option 2
function get_num($i) {
return (--$i >= 0) ? $i : false;
}
$num=11; // declare in global scope
while(false!==$num=get_num($num)){ // pass it to function as an argument
echo "Number: $num\n";
}

Categories