boolean equivalent to NULL in PHP - php

is this the right syntax to check if a value is equivalent to NULL or not in PHP:
if($AppointmentNo==NULL)
?
I have tried it but it seems that it is not working. Any suggestion for another syntax?
Thanks

Use is_null():
if(is_null($AppointmentNo)) {
// do stuff
}
Alternatively, you could also use strict comparison (===):
if($AppointmentNo === NULL) {
An example:
$foo = NULL;
if(is_null($foo)) {
echo 'NULL';
} else {
echo 'NOT NULL';
}
Output:
NULL

There is a function called is_null.
is_null($AppointmentNo)

Use is_null().
if (is_null($AppointmentNo))

Typically you would use is_null().
if (is_null($AppountmentNo))

Related

difficulty to understand a PHP syntax

Suppose we have this method in a class :
public static function example($s, $f = false)
{
(static::$i['s'] === null or $f) and static::$i['s'] = $s;
return new static;
}
would you please give me any hint what is the meaning of this line of code?
(static::$i['s'] === null or $f) and static::$i['s'] = $s;
is it a conditional statement? or something like a ternary operator?
Thanks
They are trying to be clever by using the short circuiting that happens when dealing with logical operators like this. This is what they are doing:
if ((static::$info['syntax'] === null) || $force) {
static::$info['syntax'] = $syntax;
}
If you want to see how this short circuiting works with the &&/and operator:
$a = 'something';
false && $a = 'else';
echo $a;
// output: something
Since the first part is false, it never even runs the statement on the other side since this logical operation already evaluates to false.

Check if an object is null (Shortest way)

I try to check if an object is null.
In Groovy I can check:
System.out.println(object?.object2?.property)
In other words
if(object != null){
if(object.object2 != null){
System.out.println(object.object2.property);
}
}
Now I want the same in PHP
How can I write the same in the shortest way?
object->object2->property
object2 can be null
if I try to get the property of object2 I get a NPE
Thanks for help.
in version 8 of PHP you can use Nullsafe operator as follow:
$prop = object?->object2?->property;
you can do:
if( ! $object)
if($object !== null)
If you're using PHP7 you can also do null coalesce operator
$object1 ?? $object2
in your case
if($object && $object->object2)
You can use the isset function over the entire object
if (isset($object->object2->property))
echo 'It exists!';
else
echo 'It does not exist!';
Hi We have a function is_null() or isset()
if(isset(object, object->object2)){
echo(object.object2.property);
}

PHP - Is there a way to get a variable value if it is set?

I always find myself writing something like:
if(isset($var))
{
DoSomethingWith($var);
}
else
{
DoSomethingWith(null);
}
or
if(isset($arr["key"]))
{
DoSomethingWith($arr["key"];
}
else
{
DoSomethingWith(null);
}
My question is exacly this:
Is there a way to write a get_var_if_set() function so that you can simply write...
DoSomethingWith(get_var_if_set($var));
/// and
DoSomethingWith(get_var_if_set($arr["key"]));
....WITHOUT notifying if $var doesn't exists or that $arr doesn't have a set value for "key"?
I guess it should be something like this:
function get_var_if_set($a)
{
return (isset($a) ? $a : null);
}
But that doesn't work because calling get_var_if_set() with unset variables will always generate a Notice, so it might need a little magic.
Thanks all folks.
Edit
A user who deleted his answer suggested to pass variables by reference, as PHP will pass null if the $variable doesn't exist.
So that would be perfect, take a look at these solutions (which might probably be equivalent):
function get_var_if_set(&$a) { return (isset($a) ? $a : null); }
function get_var_if_set(&$a) { return $a ?? null; } // Only in PHP 7.0+ (Coalescing operator)
Note: Coalescing operator suggested by Koray Küpe
The problem as you can see is that they initialize the passed variables somehow in the return statement.
We don't want that.
If you use PHP 7.0+ you can use null coalescing operator.
return $a ?? null;
Note: The operator checkes whether the variable exist and not empty. So, if the variable is already set but empty, it will return the null variable already.
The problem is not the var itself, but the key of the array key in this:
DoSomethingWith(get_var_if_set($arr["key"]))
So the only solution is to check for the array and the key you're looking for.
function get_var_if_set($a, $key = null)
{
if(is_array($a) && array_key_exists($key, $array)) {
return $a[$key];
} else {
return (isset($a) ? $a : null);
}
}

PHP get ? without setting variable to anything

If I were to call:
www.*.com?hello
if($_GET["hello"]){
}
It will always return false because ?hello variable needs to be set as something...
Is there a way to get the ? part without setting the variable to anythign before hand?
You can check if variable is set like this:
if(isset($_GET["hello"])){
}
sometimes key_exists() is better because if $_GET["hello"] == null you can get false
if (key_exists("hello", $_GET)) {
}
$_GET["hello"] is falsy, check if it's set at all
if (isset($_GET["hello"])) {
//do stuff
}
Use array_key_exists:
if (array_key_exists("hello", $_GET)) {
}
Please read this for a difference between isset and array_key_exists.
The usual way is to check it like:
if(isset($_GET["hello"]) && $_GET["hello"] != ""){
//code
}
if(!empty($_GET["hello"]))
{
}
Instead of checking for both isset and $_GET != "".

PHP what is wrong with the syntax of this code?

I'm building some simple validation rules in php and my IDE (phped) is complaining about the syntax.
Can anyone tell me what is wrong with the following?
function notBlank($str) {
(strlen($str) == 0) ? return false : return true;
}
phped complains of 'unexpected return'
Any advice appreciated.
Thanks.
write it like this:
function notBlank($str){
return strlen($str) != 0;
}
Write it like this:
function notBlank($str) {
return ( strlen($str) == 0 ? false : true );
}
You cant use return within ternary operators. If you want to keep that syntax you have to do something like this:
function notBlank($str = '') {
$var = (strlen($str) == 0) ? false : true;
return $var;
}
Nevertheless do notice that the default way of doing things is more legible:
function notBlank($str = '') {
if(strlen($str) == 0)
return false;
else
return true;
}
Hope it helps!
GSto's answer seems the best here, though you might also like to check out php's empty function:
http://www.php.net/empty
strlen() returns 0 when the string is empty, and in PHP 0==false. So really, it's unnecessary to wrap strlen() in a function. If you want to insist on a boolean answer then cast it. ie:
(bool) strlen($string);
So instead of your function, which is assumably called in an if block, you'd just have
if(strlen($string)) //etc.

Categories