Mattis
Mattis

Reputation: 5096

PHP coding convention and efficiency

Consider

<a href="<?php echo $url;?>"><?php echo $name;?></a>

and compare with

<?php echo "<a href=\"{$url}\">{$name}</a>";?>

then consider hundreds of these in different variations on the same page.

Does one or the other convention affect performance in any way or is it just a matter of preference?

Upvotes: 1

Views: 147

Answers (5)

George Cummins
George Cummins

Reputation: 28906

This question has been addressed quite well before: Opening/closing tags & performance?

In brief, here is the answer:

3 simple rules for you to get it right:

 - No syntax issue can affect performance. Data manipulation does.
 - Speak of performance only backed with results of profiling.
 - Premature optimization is the root of all evil

Upvotes: 4

Jack
Jack

Reputation: 1376

Some conventions can possibly effect performance if they are calling different functions or if they take up more space (so the script physically takes up more space in memory) - but this is completely negligible and just a matter of preference. If you compare it to something like Smarty or another Templating system - that on the other hand will effect performance.

Upvotes: 0

fvox
fvox

Reputation: 1087

The single quotes (') is more faster than double quotes ("), because the parser don't try to find variables in the string.
So, I think that the first script is the best way.

Upvotes: 2

Jeroen
Jeroen

Reputation: 13257

The first example will be slightly faster because using PHP to echo content adds a tiny latency over just displaying text using raw html. However, this difference is not noticeable at all and I would advise you to use choose whichever you prefer.

Upvotes: 1

KingCrunch
KingCrunch

Reputation: 131811

It may affect the performance, but that should never be the point to influence how you write good and readable code. The difference is negligible.

Just my 2 cent: The first one is much more readable, if I have huge parts of html and I just want to integrate some variables. The second one is more readable, if its only a single line or something.

Upvotes: 2

Related Questions