5 Least Used PHP Syntax Jewels, Adopt One!

Here is a list of keywords that you don’t often see in code, don’t let these PHP gems become extinct, start using them in forums, books and your own code.

Long Script Tags

I bet you’ve seen these three forms of short tags: <% %>, <?   ?> and <?php ?>, while people refer to the latter as the long tags there is actually a longer tag similar to javascript’s

<script language="php">
// all your php code can go here
</script>

Don’t worry, your code is still parsed by the server so no one will see it.

Even Shorter Comment Tags

PHP has double back slashes // for single lines and a backslash and asterisk for multiple line comments, but it also has the pound symbol for single lines.

# This is a comment

End Tags For Flow Of Control

I learned about these when I started using CodeIgniter, they are great for when you have PHP embedded into HTML. Take a look at how clean they can make your code look, and understandable if you are working with a designer who might not know PHP.

<html>
<head>
</head>
<body>
<?php
foreach($rows as $user):
?>
<li><?php echo $user['name']?></li>
<?php
endforeach;
?>
</body>
</html>

There is also endif, endwhile and more here.

Dynamic Variable Names

PHP is a dynamic language, but I didn’t know it could be this dynamic! You can use strings and variables to make variables. To see what I mean take a look at and run this code if you don’t believe me.

$part1='user_';
$part2='name';

${$part1.$part2}='DragonFly';

echo $user_name;

This is pretty cool but I’ll tell you thing, if you adopt this syntax I just hope I never have to debug your code!

While Loop That Looks Like For

This involves incrementing your counter variable in a while loop when you evaluate it. Be careful though, this syntax will increment your counter variable EVERY time it gets compared.

While Loop That Looks Like For Loop

$i=0;
while($i++<10)
{
// do your thing here
}

Those where all the "jewels" I could think of, but I bet you know of more, let us know of them in the comments and tell us which of these you are going to start using.