Cant figure out operators in php statement - php

I need to edit a function, but I cant figure out what the ? and the : false mean in the return statement. I think the : is an OR but the ? I dont know.
public function hasPermission($name)
{
return $this->getVjGuardADUser() ? $this->getVjGuardADUser()->hasPermission($name) : false;
}
Anyone that can clear this up for me?

It is PHP's Ternary Operator. It's like a shorthand for if/else expressions.
Your expanded code could look like so:
public function hasPermission($name)
{
if ($this->getVjGuardADUser()) {
return $this->getVjGuardADUser()->hasPermission($name)
} else {
return false;
}
}
Some sample-code from php.net:
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
// The above is identical to this if/else statement
if (empty($_POST['action'])) {
$action = 'default';
} else {
$action = $_POST['action'];
}

It's the ternary operator, a simple oneliner if then construct.

This is the ternary operator. It's documented here.
It's a short form for:
public function hasPermission($name) {
if ($this->getVjGuardADUser()) {
return $this->getVjGuardADUser()->hasPermission($name)
} else {}
return false;
}
}
I recommend the more verbose style for conditional statements for better readability, though.

It's called the ternary operator.
variable = predicate ? /* predicate is true */ : /* predicate is false */;
In your code, it's a shorthand form of the following:
if($this->getVjGuardADUser())
return $this->getVjGuardADUser()->hasPermission($name);
else
return false;

It's a ternaire expression.
You can replace this by:
if ($this->getVjGuardADUser())
return $this->getVjGuardADUser()->hasPermission($name);
return false;

It's a ternaire operator. Read more about it here:
http://www.lizjamieson.co.uk/2007/08/20/short-if-statement-in-php/
http://www.scottklarr.com/topic/76/ternary-php-short-hand-ifelse-statement/

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.

Converting a PHP if/else statement to use ternary format

I want to convert this if/else statement to ternary format:
function session_active()
{
if ($_SESSION['p_logged_in']) {
return true;
}
else {
return false;
};
}
I tried:
function session_active()
{
($_SESSION['p_logged_in'] ? true : false);
}
but it always returns false.
I am looking at the examples at http://davidwalsh.name/php-ternary-examples and this seems correct as far as I can see from the examples. Why does it always return false?
You may try to simple return the $_SESSION['p_logged_in'] value :-
function session_active()
{
return (bool)$_SESSION['p_logged_in'];
}
php isnt ruby, you have to return that value from the ternary.
to elaborate in more detail...
function session_active()
{
return ($_SESSION['p_logged_in'] ? true : false);
}
Try this:
function session_active() {
return (isset($_SESSION['p_logged_in'])) ? true : false;
}
to correct your logic add return in front of your statment
to simplify it do: return (bool)$_SESSION['p_logged_in'];
There is a difference between $_SESSION['p_logged_in'] === true vs $_SESSION['p_logged_in'] != null, by returning $_SESSION['p_logged_in'] could in affect be more than what it is testing for.

Ternary operator shorthand to use subject of expression in true/false clause rather than repeating

Say I have a long(ish) variable, $row['data']['manybullets']['bullets']['bullet'][0], and want to test whether it's set using the ternary operator:
$bulletx =
isset($row['data']['property']['bullets']['bullet'][0]) // condition
? $row['data']['property']['bullets']['bullet'][0] // true
: 'empty'; // false
Is there anyway for me to reference the subject of the expression rather than repeating it. E.g.
$bulletx =
isset($row['data']['property']['bullets']['bullet'][0]) // condition
? SUBJECT // true
: 'empty'; // false
Curious.
PHP supports foo ?: bar but unfortunately this won't work because of the isset() in your condition.
So unfortunately there is no really good way to do this in a shorter way. Besides using another language of course (e.g. foo.get(..., 'empty') in python)
However, if the default value being evaluated in any case is not a problem (e.g. because it's just a static value anyway) you can use a function:
function ifsetor(&$value, $default) {
return isset($value) ? $value : $default;
}
Because of the reference argument this will not throw an E_NOTICE in case of an undefined value.
You can do it like this:
$bulletx = ($r=$row['data']['property']['bullets']['bullet'][0]) ? $r : 'empty';
See working demo
Not really, without triggering an E_NOTICE warning, but if you decide to ignore those you could achieve it like this.
$bulletx =
$row['data']['property']['bullets']['bullet'][0] // true
?: 'empty'; // false
No built-in way, but you can write a wrapper for isset that checks the array keys.
function array_isset(&$array /* ... */) {
$a = $array;
if (! is_array($a)) {
return false;
}
for ($i = 1; $i < func_num_args(); $i++) {
$k = func_get_arg($i);
if (isset($a[$k])) {
$a = $a[$k];
} else {
return false;
}
}
return $a;
}
$bulletx = array_isset($row, 'data', 'property', 'bullets', 'bullet', 0) ?: 'empty';
I like this way, as it keeps the same API as isset() and can make use of the ?: short cut.

boolean equivalent to NULL in 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))

PHP conditional return statement?

Can someone tell me what the condition is in this php statement?
return $node->type == 'article' ? mymodule_page_article($node) : mymodule_page_story($node);
I'm sorry if this is not the place to ask such a simple question but I'm finding it difficult to look up specific code structure (especially when I don't know the name of it).
This is a ternary operator.
It's equivalent to
if( $node->type == 'article' ) {
return mymodule_page_article($node);
} else {
return mymodule_page_story($node);
}
What it does is: if the stuff before the ? is true, return the result of the expression in the first clause (the stuff between ? and :). If it's false, then it returns the result of the second clause (the stuff after the :).
This is the ternary operator ?: and can be expanded as follows:
if ($node->type == 'article') {
return mymodule_page_article($node);
} else {
return mymodule_page_story($node);
}
This is equivalent to:
if($node->type == 'article')
{
return mymodule_page_article($node);
}
else
{
return mymodule_page_story($node);
}
This is called the ternary operator. See the section on it here for more information: http://www.php.net/operators.comparison
this is a ternary expression.
the condition is $node->type == 'article' and if it's true it does mymodule_page_article($node) else mymodule_page_story($node)
If type of node is equal to 'article' do mymodule_page_article($node), if it isn't equal then do mymodule_page_story($node)
Use Ternary operator
return isset($node->type == 'article') ? mymodule_page_article($node) : mymodule_page_story($node)

Categories