-
Articles
-
PHP
-
What you may not know about IF statements, ternary operators explained.
What you may not know about IF statements, ternary operators explained.
- Posted: 12th November 2009
- Created by: Robert
- Found in: PHP
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;