SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    canada
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    best format for echo?

    I'm new to php and have bene wondering about echo()

    I was initially using echo('var ='.$var);
    then i was told i shouldn't use the brackets
    should i use quotation marks(ie ") or parenthesis (ie ')? I prefer parenthesis becasue I don't have to escape html attributes , but which is better
    echo '<table width="200px">';

    OR

    echo "<table width=\"200px\">';

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Bucharest
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If all of them work, I should say it's a matter of what you choose. I for one use quotation marks: "", and no paranthesis.

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It really comes down to a matter preference.

    Most of the time I use single quotes, and all of the time I use no parentheses.

    This is why.

    Double quotes tells the PHP parser to go through the string looking for any variables/values it needs to parse. Single quotes uses less CPU cycles. (this is what I have been told, so this is what I do when I don't need any variables parsed.)

    echo "$foo = 'is the only way to do it'"; (parsed by PHP)

    echo '$foo = "is the only way to do it"'; (no parsed by PHP)

    I don't use the parentheses because if I don't there is one less set of things I have to match on the other end. It makes writing code go faster (two less key strokes per echo) and less possible syntax errors, without the parentheses.
    John

  4. #4
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What PHP John has written is correct, apart from the parantheses.

    You DONT need to use parantheses for echo

    echo() is not actually a function (it is a language construct) so you are not required to use parentheses with it. In fact, if you want to pass more than one parameter to echo, you must not enclose the parameters within parentheses. It is not possible to use echo() in a variable function context.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •