PHP conditional return statement? - php

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)

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.

PHP - how to use a 'break;' in a ternary operator

I have the following function. It compares one value to each value in an array.
function catExists($id) {
$cats = getCats();
foreach ($cats as $cat) {
if ($cat['id'] == $id) {
return true;break;
} else {
return false;
}
}
}
I'm trying to shorten the whole thing by using ternary operators.
function catExists($id) {
foreach (getCats() as $cat) return ($cat['id'] == $id) ? true : false;
}
The problem I have is that I can't use break; when the condition turns to true. i.e The returned value will keep reverting back to false unless the true condition is at the end of the array.
Is their a way that this can be achieved on a single line?
Thanks
That's not what ternary operators are meant to do. Keep it simple (KISS). You don't need the break statement at all since return ends the function execution and returns the program control back to the main program.
I'd write it this way:
function catExists($id) {
foreach (getCats() as $cat) {
if ($cat['id'] == $id)
return true;
}
return false; // 'return true' never happened, so return false
}
If you really want to make it one line, you could use array_column() in conjunction with array_search() like so:
function catExists($id) {
return array_search($id, array_column(getCats(), 'id')) !== 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.

PHP return syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the PHP ? : operator called and what does it do?
Can someone please tell me what this 'return' php code means / does:
return ($status=='SUCCESS' && $blocked=='YES') ? $reason : false;
I'm familiar with the regular return $variable type of statements in php, but I don't get what the specific brackets ( ) and ? question marks and the ": false" does.
(this is the return statement at the end of a php function)
It's a ternary statement. It's basically a shorthand notation for if/else.
In your example it would read like: If $status is equal to "success" and $blocked is equal to "Yes" return $reason, else, return false;
That's a ternary, or conditional operator, it's the same as if you had:
if($status=='SUCCESS' && $blocked=='YES'){
return $reason;}
else{
return false;
}
It means the same as this:
if($status == 'SUCCESS' && $blocked == 'YES')
{
return $reason;
}
else
{
return false;
}

Cant figure out operators in php statement

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/

Categories