Pages

Thursday, 18 April 2013

how write shorthand if else condition in php

If else condition is the first syntax that a programmer learn.
Generally we use like below condition:
if(condition)
{
   do some task
}
else
{
   do some task.
}

but you can use a short hand way, that will reduce your line of codes.

(condition) ? task1  :  task2

ie. if condition is ok the it will do task1 otherwise it will do task2

example:
<?php
$signal = '0';
$signal == '1' ? $message = 'I am right' : $message = 'I am wrong';
echo $message;
?>

Just copy and paste this code to a file and run in server. you will understand it.

Change  $signal variable, then it will do second task.









No comments:

Post a Comment