25 January 2007

cleaner php code with define

When using echo in PHP, i try to use single quotes (’) ... as that makes thing faster… but when i want a newline or tab, then i have to break out and use double quotes “\n” ... really looks messy… define to the rescue…

This is a very simple trick to make code more readable, and provides no speed implications… or does it?

Starting code … this is a trivial example just to show the concept …

.
echo "<ul>\n";
echo "\t<li>one</li>\n";
echo "\t<li>two</li>\n";
echo "\t<li>three</li>\n";
echo "<ul>\n\n";

1st improvement: As indicated before double quotes is slower, especially if you are not putting in variables, i pretty much always use single quotes…

.
echo '<ul>' . "\n";
echo "\t" . '<li>one</li>' . "\n";
echo "\t" . '<li>two</li>' . "\n";
echo "\t" . '<li>three</li>' . "\n";
echo '<ul>' . "\n\n";

2nd improvement: multiple echos slower than storing as variable then just printing that …

.
$list  = '<ul>' . "\n";
$list .= "\t" . '<li>one</li>' . "\n";
$list .= "\t" . '<li>two</li>' . "\n";
$list .= "\t" . '<li>three</li>' . "\n";
$list .= '<ul>' . "\n\n";
echo $list;

3rd improvement: doesn’t that really look ugly … well define to the rescue … i basically have a “constants.php” that has all my defines, that i #include so that i always have access to them … here is the last iteration of the above code with define inline…

.
define("t","\t");
define("n","\n");
$list  = '<ul>' . n;
$list .= t . '<li>one</li>' . n;
$list .= t . '<li>two</li>' . n;
$list .= t . '<li>three</li>' . n;
$list .= '<ul>' . n.n;
echo $list;

This may not look like much … but it saves some typing, and i think it’s much cleaner, plus with #include you can change things one place for certain constants… obviously TABS and NEWLINES probably won’t change but HTML snippets could…

Someone might say why not use local variables…

.
$t = "\t";
$n = "\n";
$list  = '<ul>' . $n;
$list .= $t . '<li>one</li>' .$n;
$list .= $t . '<li>two</li>' . $n;
$list .= $t . '<li>three</li>' . $n;
$list .= '<ul>' . $n.$n;
echo $list;

One drawback of that is that variables are not accessible inside functions… where as constants are GLOBAL ...

Here is a snippet of my “constants.php” file …

"constants.php"
define("t","\t");
define("n","\n");
define("br","<br />");
define("brc",'<br clear="all" />');
define("sp","&nbsp;");
define("a","&amp;");
define("vpipe",'<span class="vp"> | </span>');
define("r_arr",'&raquo;');
define("l_arr",'&laquo;');
define("req",' <span class="req">*</span>');
 

comment

what they saidwho said it

Well, the simplest tricks are often the best :)

2007-01-29
Seyora

hey, this is a good intro to define, but i have a suggestion, instead of writing all these defines to help write code in php, why not change how you write the code. let the html stay html and separate the php. php is very handy because it can be placed anywhere in the code so for your example:

<ul>
   <li><?= result ?></li>
   <li><?= result ?></li>
   <li><?= result ?></li>
   <li><?= result ?></li>
   <li><?= result ?></li>
</ul>

this sill allow the html to format itself without the need to you to go in and add tabs and newlines

2007-02-13
david b

Great tip david b … but sometimes i need to loop through a table to determine how many list items there are…

I think it’s great to intersperse php inside html but it can get tedious sometimes. I do a mixture of both, and sometimes it’s faster for parser to not have to interpret a number of variables inside of a template.

2007-02-13
DannyB

Im glad you said that, thats exactly what i use this bit of code for, its excellent in flowing through a table.

<ul>
<?
//sql to get result
$foreach($result as $value){
?> <li><?= $value?></li>
<? } ?>
</ul>

that will loop through your result which is pulled from your database and still display the html separate from the php =)

2007-02-14
david b

Different ways and different views … I do it like this …


//HTML TEMPLATE
<ul>
%%LIST%%
</ul>

//PHP
$list = '';
$foreach($result as $value)
{
  $list .= t.'<li>'.$value.'</li>';
}
$html = 
  str_replace('%%LIST%%',$list,$html);
echo $html;
2007-02-14
DannyB

In my case I use this code:

.
$list= ‘<ul>’.
‘<li>one</li>’.
‘<li>two</li>’.
‘<li>three</li>’.
‘<ul>’;

why? because if you are using single quotes is because you are exaggerating the optimization, and in this case you don’t want to send the \t or \n codes to the user, indentation is a PHP server code matter, not an HTML client matter, if you don’t send that codes you’ll have optimized 9 characters and that optimization is even better than the use of single quotes, let’s say another exaggeration. Well, who knows, may be this is inside a for/while loop and we earn some time.

2007-08-06
poirot

poirot: my example may not give it due justice, but it does support the technique. I do however have a sick obsession with code that looks pretty too ;) …

2007-08-06
DannyB


note: you can only submit after you hit preview


nuff-respec is a weblog written by daniel bulli a senior web programmer in boston, ma.
more >

contact | resume | profile

recently :::

diversions :::

Using Flash And Staying Standards Compliant
Anyone who has ever worked with Flash on the web has likely come across the fact that embedding flash into a web page is usually no walk in the park...
A Design is Finished when...
This is probably the hardest part of designing for me.... 23 Pro designers weigh in with their opinions ...
JungleCrazy.com
Find all the Amazon.com products that are discounted by at least 70%
IETester
IETester is a free WebBrowser that allows you to have the rendering and javascript engines of IE8 beta 1, IE7 IE 6 and IE5.5 on Vista and XP, as well as the installed IE in the same process.
you still want more »