PHP syntax question: What does the question mark and the colon mean?

综合编程 2018-02-08 阅读原文

Possible Duplicate:

quick php syntax question

return $add_review ? FALSE : $arg;

What do question mark and colon mean?

Thanks

This is the PHP ternary operator
(also known as a conditional operator) - if first operand evaluates true, evaluate as second operand, else evaluate as third operand.

Think of it as an "if" statement you can use in expressions. Can be very useful in making concise assignments that depend on some condition, e.g.

$param = isset($_GET['param']) ? $_GET['param'] : 'default';

There's also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise. For example:

$result = $x ?: 'default';

It is worth mentioning that the above code when using i.e. $_GET or $_POST variable will throw undefined index notice and to prevent that we need to use a longer version, with isset
or a null coalescing operator
which is introduced in PHP7:

$param = $_GET['param'] ?? 'default';
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!

您可能感兴趣的

Getting Hired Dear reader, Those that follow me know that I am coming out of a week and a ha...
How can I find the largest item in a table with ph... I am trying to return the highest term taxonomy id of a post page or a taxonomy ...
Sergey Zhuk: Asynchronous PHP: Why? In a post to his site Sergey Zhuk shares his opinion on asynchronous PHP and...
关于PHP SESSION反序列化 0x00 环境PHP:PHP Version 5.5.35 System: Linux version 2.6.32-504.23.4.el...
Exakat 1.3.1 review – Exakat Exakat 1.3.1 review Exakat 1.3.1 embarks on a new version of the underly...