开发者

Syntax to check isset using comparison operators

开发者 https://www.devze.com 2023-04-01 09:32 出处:网络
Just like how we do in other languages to check for a condition in one line. Is it possible to do it in PHP?开发者_开发问答

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?


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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消