Function Evaluation inside a literal.

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

    Function Evaluation inside a literal.

    Is there a way to have a function inside a literal string evaluated
    similar to variable or array evaluation?

    something similar to this?

    $dt = "The order was sent ${date('F j, Y')}. Please make a note of it"

    or do I have to create a variable first or concatenate the string.

  • Janwillem Borleffs

    #2
    Re: Function Evaluation inside a literal.

    ImOk wrote:[color=blue]
    > or do I have to create a variable first or concatenate the string.
    >[/color]

    Besides creating a variable first, you have the following options:

    $dt = 'The order was sent ' . date('F j, Y') . '. Please make a note of
    it';

    $dt = sprintf('The order was sent %s. Please make a note of it', date('F j,
    Y'));


    JW


    Comment

    • lorento

      #3
      Re: Function Evaluation inside a literal.

      Hmm..instead use sprintf or concatenate the string its better you
      creating variable first.

      $date_order = date ("F j, Y");
      $dt = "The order was sent $date_order. Please make a note of it";

      The concatenate method is faster then other methods but its difficult
      to maintain.
      The sprintf() function is slowest among this methods.. its better use
      sprintf() if you want format complex string.

      Read this : http://www.zend.com/zend/art/mistake.php

      ---




      Janwillem Borleffs wrote:[color=blue]
      > ImOk wrote:[color=green]
      > > or do I have to create a variable first or concatenate the string.
      > >[/color]
      >
      > Besides creating a variable first, you have the following options:
      >
      > $dt = 'The order was sent ' . date('F j, Y') . '. Please make a note of
      > it';
      >
      > $dt = sprintf('The order was sent %s. Please make a note of it', date('F j,
      > Y'));
      >
      >
      > JW[/color]

      Comment

      • Janwillem Borleffs

        #4
        Re: Function Evaluation inside a literal.

        lorento wrote:[color=blue]
        > The sprintf() function is slowest among this methods.. its better use
        > sprintf() if you want format complex string.
        >[/color]

        True, although (s)printf is sometimes used in cases where readability is
        preferred before performance (per example, when dynamically constructing a
        complex SQL statement with a lot of variables) .


        JW


        Comment

        Working...