Share it

Bookmark and Share

Translate

Tuesday, October 11, 2011

0 Overview of PHP shorthand

Your code will mostly become shorter by learning what all the different functions do and finding a nice one that does what you want, or combining a few of them to achieve the desired effect.

  accepted
Here are some of the shorthand operators used in PHP.
//If $y > 10, $x will say 'foo', else it'll say 'bar'
$x = ($y > 10) ? 'foo' : 'bar';
//Short way of saying <? print $foo;?>, 
useful in HTML templates
<?=$foo;?>
//Shorthand way of doing the for loop, 
useful in html templates
for ($x=1; $x < 100; $x++):
   //Do something
end for;
//Shorthand way of the foreach loop
foreach ($array as $key=>$value):
   //Do something;
endforeach;
//Another Shorthand way of If/else:
if ($x > 10):
    doX();
    doY();
    doZ();
else:
    doA();
    doB();
endif;
//You can also do an if statement without 
any brackets or colons if you only need to
//execute one statement after your if:
if ($x = 100)
   doX();
$x = 1000;

0 comments:

Post a Comment

Thanks for your valuable Comment

 

TechnoTipworld- Tips,Tricks,Technology Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates