2

I want to place an if statement into a variable in order to reduce the lines of code... for example I want to do this:

$empty = if(empty($_POST['username']) && empty($_POST['password']))
if($empty){ // echo message; 
}
elseif(!$empty){ ///echo message; 
}

Can something like the above be produced? Or am I making things too complex?

4 Answers 4

7

Just omit the if:

$empty = empty($_POST['username']) && empty($_POST['password']);
Sign up to request clarification or add additional context in comments.

Comments

2

You could use the ternary operator

$message = ( empty($_POST['username']) && empty($_POST['password']) ) ? 'empty': 'not empty';
echo $message;

1 Comment

No, the ternary operator is basically a shorthand for if/else - think of it like (if) ? true : false;
0

Try this

$empty = (empty($_POST['username']) && empty($_POST['password'])) ? 0 : 1;

if($empty){ // Not empty message here...; 

}
else{ ///Empty message here....; 

}

Comments

0

Use of ternary operator:

echo empty($_POST['username']) && empty($_POST['password']) ? 'empty message' : 'Not empty message';

Rather than getting value of $empty and printing it, you can directly use echo and ternary operator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.