ECHO or concatenation with single quote, for performance?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • L. Berger

    ECHO or concatenation with single quote, for performance?

    Hi,

    I am working on an HTML template which has a lot of html tags, with
    PHP data shown in the middle of these tags -- you know, the usual.
    Currently, I have HTML as is, and many many "echo $variable"
    statements mixed in as PHP code.

    My question: should I leave it like this, with ECHO statements
    embedded within the tags,

    Or

    Should I concatenate the HTML within single quotes (single quotes are
    supposed to be faster in PHP than double quotes) and the display data
    as variables, and then ECHO that one concatenated string?

    Any experiences or pointers would be great!

    Thanks
    LB

  • Michael Fesser

    #2
    Re: ECHO or concatenation with single quote, for performance?

    ..oO(L. Berger)
    >I am working on an HTML template which has a lot of html tags, with
    >PHP data shown in the middle of these tags -- you know, the usual.
    >Currently, I have HTML as is, and many many "echo $variable"
    >statements mixed in as PHP code.
    >
    >My question: should I leave it like this, with ECHO statements
    >embedded within the tags,
    >
    >Or
    >
    >Should I concatenate the HTML within single quotes (single quotes are
    >supposed to be faster in PHP than double quotes) and the display data
    >as variables, and then ECHO that one concatenated string?
    The fastest would be something like

    echo 'some text ', $aVar, ' some more text ', $anotherVar;

    No double-quoted string, no concatenation, just pure output. But I don't
    consider that much of an issue. You should use what seems to be the most
    appropriate to you, mainly in order to keep the code readable and
    maintainable. For example instead of the code above I would usually use

    print "some text $aVar some more text $anotherVar";

    or even

    printf('some text %s some more text %s',
    $aVar,
    $anotherVar
    );

    Definitely slower than the echo statement, but much more flexible and
    more readable (at least for me), especially when there are a lot of
    variables or statements required in the string.

    Micha

    Comment

    • L. Berger

      #3
      Re: ECHO or concatenation with single quote, for performance?

      The fastest would be something like
      echo 'some text ', $aVar, ' some more text ', $anotherVar;
      >

      Thanks, I had no idea echo could work with simple commas! Some testing
      with microtime (just back of the envelope, nothing scientific) reveals
      that it is indeed the fastest. Here's what I had:

      OPTION 1: echo 'something and more' . $i . $i+100 . 'something else
      and then some';
      OPTION 2: echo 'something and more' , $i , $i+100 , 'something else
      and then some';
      OPTION 3: something and more' <?php echo $i; ?><?php echo $i+100; ?
      >something else and then some
      Option 2 outperforms the others. 1 and 2 are almost the same, but 2 is
      usually just a little bit faster, especially for more variables.
      Option 3, which calls echo many times is markedly slower.

      Hope this helps someone!

      Comment

      Working...