Facebook Opensources HipHop PHP Compiler

February 3, 2010 · Posted in PHP · Comment 

Facebook earlier announced that they are releasing their PHP compiler to open-source. I love it when a company uses open source software like PHP to build their platform, and then they pass back to the community, and everyone benefits.

A friend’s (an ex PHP developer) comment the other day was that PHP isn’t a real programming language because it does not compile, he uses C# for his CMS. But I suppose the mechanism of HipHop confirms what he was saying, as the compiler essentially converts your PHP code or script into C++.

Most exciting is the performance benefits of the compiled PHP code vs un-compiled PHP script. Facebook will be streaming a live tech talk regarding HipHop for PHP this evening at 7:30PM Pacific. HipHop Logo

Short PHP IF-Else Statements (Shorthand IF)

November 4, 2009 · Posted in PHP · 1 Comment 

Funny thing is I have been working in PHP for years, but only with the prevalence of CMS’s have I required short code for PHP, I have always just typed things out in full. These shortened PHP conditional statements are called ternary operators, and I find them most useful when popping code into a CMS code block, otherwise I generally code stuff in full for legibility.

If you look at the PHP manual they give you the following alternative syntax which is really not that compact:

<?php
## all examples start with $x having a value of 3 ##
if ($x == 3):
echo "X is equal to 3";
else:
echo "X is not equal to 3";
endif;
?>

And that if and elseif … endif structure is fine, but what about one line alternative code for a PHP if statement:

<?php if ($x == 3): echo "X is equal to 3"; else: echo "X is not equal to 3"; endif; ?>

But still this is not that efficient, the following two versions give nice compact code the first for just printing the value:

<?php echo ($x == 3)? "X is equal to 3" : "X is not equal to 3"; ?>

and the second allocates the string value to $y, dependent on the short if statement, and then prints:

<?php $y = ($x == 3)? "X is equal to 3" : "X is not equal to 3" ; ?>
<?php echo $y ; ?>

And of course if all you want to do is print one thing’s value dependant on its not being blank or zero:

<?php echo ($z)? $z : "" ; ?>

Nice short if statement, have I missed anything?

The PHP manual does have a couple of points if you scroll down on this page. The most notable is “avoid “stacking” ternary expressions”; if you want your code to be readable dont nest ternary conditional statements.