Abishek
Abishek

Reputation: 11691

Syntax to check isset using comparison operators

Just like how we do in other languages to check for a condition in one line. Is it possible to do it in PHP?

in java

boolean val = (a == b) ? true : false;

similarly is it possible in PHP.

If yes, the can this be done for the isset keyword?

Upvotes: 1

Views: 723

Answers (2)

James P.
James P.

Reputation: 19617

Yes, it's possible to use the ternary operator in PHP. Replacing (a == b) with isset(expression) (returns boolean value) should do the trick. Just make sure that the = operator doesn't take precedence. It may not be necessary but I'd wrap the ternary statement in between curved brackets.

Upvotes: 0

cdhowie
cdhowie

Reputation: 169113

Yes, absolutely. PHP supports the ?: ternary operator as well. For example:

$foo = isset($_POST['foo']) ? $_POST['foo'] : '';

Upvotes: 2

Related Questions