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;
}
Related
This question already has answers here:
Using if(!empty) with multiple variables not in an array
(15 answers)
Closed 3 years ago.
With these variables:
$var1='a';
$var2='b';
How can I check if they are both empty at once without the need of doing this:
if(empty($var1) && empty($var2)){
}
or
if($var1=='' && $var==''){
}
I mean something like:
if(($var1 && $var)==''){
}
Depending on your scale requirements and how large the data you'll be storing in these vars is, you could just do a straight concatenation:
if($var1 . $var2 == '') {
// blank strings in both
}
Alternatively, using empty():
if(empty($var1 . $var2)) {
// blank strings in both
}
Repl.it
How about a utility function:
function is_any_empty() {
$args = func_get_args();
foreach($args as $arg) {
if(empty($arg)) return true;
}
return false;
}
var_dump(is_any_empty("")); // bool(true)
var_dump(is_any_empty("ds", "")); // bool(true)
var_dump(is_any_empty("ds", "dd")); // bool(false)
I mean, that's kinda how you do it. But if you want to ensure that they both are not empty, you'd probably want your if statement to look more like this:
if(!empty($var1) && !empty($var2)) {
// Do stuff! They both exist!
}
I'd prefer an empty check because if $var1 and $var2 haven't been defined, you'll get notices all over your error logs.
Hope that helps!
This question already has answers here:
shortened php if else block
(2 answers)
Closed 8 years ago.
Is it possible to make this in 1 line ?
Here is my code
if(!is_null($id)){
return TRUE;
}
else{
return FALSE;
}
Thank's for the help :)
The is_null() already returns a boolean type, so you can simply write:
return !is_null($id);
Try this
return (!is_null($id)) ? true : false;
Yes you can:
return (!is_null($id)) ? true : false;
Or just remove breaks:
if (!is_null($id)){ return TRUE; } else { return FALSE; }
Yeah:
return is_null($id) ? FALSE : TRUE
which reads as:
[return] [the statement] ? [if is true, do this] : [otherwise, do this]
Use ternary operator.
return (!is_null($id)) ? TRUE : FALSE;
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.
This question already has answers here:
Return always return false
(3 answers)
Closed 9 years ago.
I'm having some problems with a Return statement in PHP. The thing is that, no matter what happend inside my function I always get a false value out of the function. I really think that is because of the Return statement because I try to use it in other functions and I don't get a diferent value.
public function valid_token ()
{
if (!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
{
$this->errors[] = "Formulario incorrecto";
}
return count($this->errors)? 0 : 1;
}
Out of this function I always get a false value (0). The same happends when i call:
public function valid_data ()
{
if (empty($this->sectorName) || empty($this->sectorInfo) || empty($this->sectorCat))
{
$this->errors [] = "Datos incorrectos";
}
return count($this->errors)? 0 : 1;
}
Of course, I call both functions when I have already sent the form and have set the token.
Because you are just counting not checking,Try this
return count($this->errors) > 0 ? 0 : 1;
More simply,
return !$this->errors;
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)