Difference between NULL, 0 ,False and ''? [duplicate] - php

This question already has answers here:
Null vs. False vs. 0 in PHP
(18 answers)
Closed 4 years ago.
What is the exact difference between null, 0, false, and an empty string in PHP? Is null similar to None in python?

Well, after massive comments and then poof deletion of them... may as well try to help you out.
null is the absence of value.
0 can be a numeric value, or a representation of a boolean FALSE, or a string. PHP doesn't really have variable typing, so depending on what you are checking for using ==0 or ===0 or ==false or ===false may be appropriate.
You may want to read over this - https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ as well as the PHP manual for isset() and empty()

Related

Checking if variable exist and if it has a certain value in one line [duplicate]

This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 months ago.
I have always thought that if I want to check if a variable exists and has a certain value I have to use two if conditions:
if(isset($x)){
if($x->age==5){}
}
But I realized its also possible to do it in one line this way:
if(isset($x) && ($x->age==5)){}
Can someone tell me why the second variation will not result in an error if $x is null. Given that $x is null and doesn't have the property age? Would it be trying to access a property that doesn't exist?
$x=null;
Because $x is null, isset($x) is false. Then, because of the logical operator "AND" (&&), the condition cannot be fully validated, so, the test is stopped here and ($x->age==5) is not executed.
For a shorter code, as of PHP 8.0.1, you can use the NullSafe Operator (?->)
if ($x?->age == 5) { }

php equations in statements with double = problems [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 6 years ago.
I've got some weird actions from my php script and finally figured out that this following statement makes problems. Because 0 is equal some string in php.
if(0 == "whats Up?")
{
echo 42;
}
With triple "=" it do what I expected. It is possible for you to give me a briefly answer what is the reason and idea behind this behavior of php? Why did they implement php like this?
I mean I know that 1 == "1" is true and 1 === "1" is not. This is also in python. I also learn from somewhere that 0 could be understandable as false but this example above has no explication for me. But I am sure that you know it.
Thank you in advance
That's because of Type Juggling. The second operand gets converted to an integer and 0 == 0 is true.
var_dump((int) "whats Up?"); // int(0)

What is different between isset() and !empty [duplicate]

This question already has answers here:
isset vs empty vs is_null
(14 answers)
What's the difference between 'isset()' and '!empty()' in PHP?
(7 answers)
Closed 8 years ago.
I want to know what is different between isset() and !empty?
I know that isset() tests if a variable is set and not null, while
empty() can return true when the variable is set to certain values.
But logically when isset() which doesn't empty (without using "empty()"), there is a variable in text or textarea whatever so we don't need empty().
isset method checks if a variable exists or not. On the other hand !empty knows that a variable exists but it needs to check its value.
The difference is quite small, but significant enough to not make mistakes with both statements. When you declare a variable like this:
<?php
foo = "";
?>
You will end up with different values depending on what you use. isset() will return true, as the variable foo was set to nothing. However !empty will return false, as the variable foo does not contain anything. Basically isset() only checks for NULL values, where !empty checks for for everything that is considered 0 (so NULL, 0, 0.0 etc. but also 0 as a string, for example).

Conditional in PHP [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.

Return a value if a PHP GET doesn't exist? [duplicate]

This question already has answers here:
Check if url contains parameters [duplicate]
(3 answers)
How to verify if $_GET exists?
(7 answers)
Closed 9 years ago.
I have lots of PHP statements along the lines of:
$getvalue = $_GET['valueiwant'];
In some scenarios not all variables are available. So, let's say 'valueiwant' doesn't exist in the URL string, how can I return a value based on the fact it doesn't exist?
For example if 'valueiwant' can't be found set $getvalue to -1
Currently it appears the value defaults to 0 and I need to be equal less than 0 if it doesn't exist.
Any ideas?
thanks
I always use
$getvalue=isset($_GET['valueiwant'])?$_GET['valueiwant']:-1;
Use of the isset() function checks if the offset exists, and returns a boolean value indicating it's existence.
This means that you can structure an if statement around the output.

Categories