PHP - What you may not know about IF statements, ternary operators explained.

Facebook

Publish on Facebook

Twitter

Search

Tag Search

Recently Added

Most Popular

Member Options

12
Nov

Traditional IF statements can take up more lines than you would like to. Especially when its only a small condition. Ternary operators take three arguements condition, true, false. This can be contructed on one line as opposed to the tranditional way which can take 4 lines (to look nice to read).

(condition) ? true : false;

A traditional statement may look like this:

if(is_numeric($_GET['id'])){  $id = $_GET['id'];}else{  $id = 0;}

The same statement as a ternary operator:

$id = is_numeric($_GET['id'])? $_GET['id'] : 0;