PHP is a easy language to learn and having some short hand codes which can make your code less and more beautiful and easy to understand . today i am going to discuss some method by which you can decrease your code lines with same effect .

Print statement : If you want to just echo a php variable inside the html code or anywhere the most people use

<?php echo $myvariable; ?>

but alternatively we can print the same variable with

<?=$myvariable?>

the following line will produce a same result and taking less time to write . It can help you to write quick code .

minimize Braces : Sometime you don’t need to add braces if your statement has only one directive . It can save a lot of time . i.e

for($i;$i<=10;$i++)
{
echo $i;
}
if($i>$j)
{
echo $i."is greater than ".$j;
}
foreach($array as $key=>$value)
{
echo $key.' has value '.$value;
}
while($array=$command->fetch_row())
{
echo $array['value'];
}

as we can see the following code has only one directive so we can write the same code as given below

for($i;$i<=10;$i++)
echo $i;
if($i>$j)
echo $i."is greater than ".$j;
foreach($array as $key=>$value)
echo $key.' has value '.$value;
while($array=$command->fetch_row())
echo $array['value'];

Ternary operator : The short code i love to use always . It saves a lot of time and always easy to ready inside the html tag .

if ($action == 'save') {  
echo 'save';  
} else {  
echo 'cancel';  
}

with Ternary operator the same code can be written as

echo $action == 'save'? 'save' : 'cancel';

Easy to write and can be written in just one line .

with Ternary operator we can set a value in variable , we can print values , we can return values and many more . Here is a example

$type=($action=='save') ? 'Save' :'Cancel';
$action=($action=='save' || $action=='update')? 'save' :'cancel';
return $type='check' ? true: false;

Alternative syntext : Php offers some alternative syntax for some of control statements . like for , if, while , in this the opening braces replaced with colon(:) and closing braces with endif; , endforeach; , endfor; , endswitch respectively. this can be very useful if you are using html code inside the php code .I.e

<?php if($data=='user') : ?>
<h2> this is for user</h2>
<?php elseif($data=='admin'): ?>
<h2> this is for admin</h2>
<?php endif; ?>

same for other control statement this can be use.

Incremental Operators: In loops like for loop, foreach loop and while loop we need to increment a counter variable so that we can get the no for each cycle . and we need to increment on each cycle

$int = $int + 1; // or alternatively: $int++;  
$int = $int - 1; // or alternatively: $int--;

Array shorthand PHP 5.4 introduced an array shorthand

$a = [1, 2, 3, 4];
$b = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
http://codingbin.com/wp-content/uploads/2015/12/PHP_Logo-copy.jpghttp://codingbin.com/wp-content/uploads/2015/12/PHP_Logo-copy-150x150.jpgdevidphpphp,shortcodes,shorthand codePHP is a easy language to learn and having some short hand codes which can make your code less and more beautiful and easy to understand . today i am going to discuss some method by which you can decrease your code lines with same effect . Print statement...coding, php, wordpress, yii, cakephp, and  jquery

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request