If else and return [duplicate] - php

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;

Related

function returns true false or maybe [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a function that returns true or false based on its algorithm and works fine.
function abc(){
if(somecondition){
return true;
}else{
return false;
}
}
and later on in my code I do
if(abc()){
//code if the function returns true
}else{
//code if the function returns false
}
and this works flawlessy.
Now I want to handle a situation where the function should return a "maybe" status so that later on I perform an additional check. So my pseudo code becomes:
function abc(){
if(somecondition){
return true;
}elseif(someothercondition){
return 'maybe';
}else{
return false;
}
and later on in my code I do
if(abc()){
//code if the function returns true
}elseif(abc()=='maybe'){
//code if the function returns maybe
}else{
//code if the function returns false
}
Is this going to work? Is this the correct way to handle this?. I am asking since the maybe condition is a very remote situation in my code that is hard to replicate and this makes it hard for me to test directly
It's a cleaner approach to return nullable bool, than mixed.
So your implementation would be
function abc(): ?bool
{
if (somecondition) {
return true;
} elseif (someothercondition) {
return null;
} else {
return false;
}
}
Where the null value stands for "maybe".
You'll also need to use strict comparison (===). In loose comparison (==) null would equal to false.
$abc = abc();
if ($abc === false) {
// no
} elseif ($abc === null) {
// maybe
}
The condition after return evaluates to either true or false, so there's no need for the if/else.
function abc(){
/*if(somecondition){
return true;
}else{
return false;
}*/
return somecondition;
}
or
function abc(){
/*if(somecondition){
return true;
}elseif(someothercondition){
return 'maybe';
}else{
return false;
}*/
return someothercondition ? 'maybe' : somecondition;
}
the condition the more specific should be the first to evaluate abc()=='maybe'.
so change this
if(abc()){
//code if the function returns true
}elseif(abc()=='maybe'){
//code if the function returns maybe
}else{
//code if the function returns false
}
to that to be more specific
if(abc()==='maybe'){
//code if the function returns maybe
}elseif(abc()){
//code if the function returns true
}else{
//code if the function returns false
}
The accepted answer is the correct answer to my question but the solution I finally implemented is taking input from a comment by #nicohaase. I splitted in two the function so that the code becomes:
function abc(){
if(somecondition){
return true;
}else{
return false;
}
}
function def(){
if(someothercondition){
return true;
}else{
return false;
}
}
then I do:
if(abc()){
//do something
}else{
//this is false, check other conditions
if(def()){
//do something else
}else{
//execute code if all return false
}
}
I have moved the "maybe" outside the function so the functions return a boolean as expected and the code is cleaner to read.

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.

Return statement in PHP functions [duplicate]

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;

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