Ternary Operator in PHP

Ternary Operator in PHP

The ternary operator is similar to an if/else statement. Here is an example for if/else statement:

if (empty($_POST[’submit’])) {

$action = ‘default’;

} else {

$action = $_POST[’submit’];

}

We can use the above example with ternary operator which will produce the same result using less space. It makes use of ? and : just like if and else.

$action = (empty($_POST[’submit’])) ? ‘default’ : $_POST[’submit’];

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.