0

I am currently asking myself question about the <?php ?> tags

Some people use them like this:

<?php 
    $value = "2";
    echo "test".$value."tested";
?>

And some people do it like this:

<?php $value = 2; ?>
test<?php echo $value ?>tested

Is there any big difference I haven't stumbled upon yet?

I made some perfomance tests and the biggest difference was 17% (which may be normal fluctuations)

3
  • Your second example isn't even valid. Commented Nov 24, 2014 at 10:20
  • It's all about code separation and code reading, nothing to do with performance. Commented Nov 24, 2014 at 10:22
  • @Daan thanks i corrected that :D, Royal Bg so just readabilty? Commented Nov 24, 2014 at 10:24

2 Answers 2

2

The role of the PHP tags <?php and ?> are to tell PHP which of the code it should parse. This is useful because you are then able to mix HTML and PHP, as you've already discovered.

An additional open tag also exists: <?=, this tag is a shortcut for <?php echo and is useful in your example, where all you are doing is outputting the value from a variable. It uses the same closing tag: ?>

There is an interesting thread about PHP tags and performance for you, right here: Opening/closing tags & performance?

Read more about the PHP tags on php.net: http://php.net/manual/en/language.basic-syntax.phptags.php

Sign up to request clarification or add additional context in comments.

Comments

1

From a performance point of view: Don't try to improve performance with such bagatelles. If you use short tags or not, if you render your output inside a PHP block or you use multiple, if you use one space more or less: It doesn't matter. You will or won't save a few ms, but it's more important to think about the drawbacks.

The real deal is to improve performance where it's profitable. That is in the most cases:

  • unperformant database interaction
  • unnecessary iterations
  • non-existing caching concepts
  • misconfigured webservers

The PHP-Community seems to go a little bit insanse on alleged performance improvements lately and ask questions like: "Can I use a space here (OMG PERFORMANCE)?" or "Is it a performance killer to instantiate many objects?" - okay the first one is a bit exaggerated, but I've seen the second one a couple of times.


In your example both scripts produce the same output and I would just for that example prefer the first one because it's simply more readable. If you're processing templates for example, where you occasionally need to substitute small snippets of PHP in large amounts of HTML, method two would be prefered.


Generally one should care about their code and it's readability and then about optimization and performance. You should never ever trade performance for readability/code quality (luckily most of the times these two go hand in hand), especially not in irrelevant cases such as shorter syntaxes.

3 Comments

I'm not about every little tiny bit of performance. I'm working with php since some years and generaly know what points of code makes the difference mostly. I was just curious about this tag. Some of my friends had a BIG discussion about it and i wanted to know whats behind it, so i just asked
Thats no offense on you. Really. I wanted to post a few thoughts on performance in case someone stumbles upon
I didn't see that as offense don't worry

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.