1

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?

5 Answers 5

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for "premature optimization". But this are not only syntax issues, because of the several context switches between php and inline-html.
I've noticed that my question might be a duplicate, I just re-frased the question differently and did not find it. Mayhaps it should be deleted.
2

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.

Comments

2

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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.