Results 1 to 10 of 10

Thread: echoing strings and variables

  1. #1
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,509
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default echoing strings and variables

    PHP Code:
    echo "Welcome $_SESSION['username']"
    PHP Code:
    echo 'Welcome' $_SESSION['username']; 
    PHP Code:
    echo 'Welcome'$_SESSION['username']; 
    Is there any difference in these three methods and which of them is quickest?

    Thanks, Keyboard1333
    Last edited by keyboard; 06-02-2012 at 10:16 PM.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,689
    Thanks
    82
    Thanked 89 Times in 87 Posts

    Default

    This is not really an answer, but your first example should have quotes in the $_SESSION variable. Otherwise you will get an error message.

    It should read as the following instead.

    Code:
    echo "Welcome {$_SESSION['username']}";
    The following will also work, but as I recall it is not quite proper.
    Code:
    echo "Welcome $_SESSION[username]";
    You can read more about it here.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,509
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Hmmmm, interesting...

    So how come -

    Code:
    <?php
    session_start();
    $hey = "Example";
    echo "Welcome $hey";  
    ?>
    works, but not when it's a session variable?

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,689
    Thanks
    82
    Thanked 89 Times in 87 Posts

    Default

    I can't give you a simple answer at the moment, but I would start reading here and contine for about 2 - 3 pages.

    No worries, almost all of it is examples.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by keyboard1333 View Post
    So how come -
    Code:
    <?php
    session_start();
    $hey = "Example";
    echo "Welcome $hey";  
    ?>
    works, but not when it's a session variable?
    do you mean
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['hey'] = 'Example';
    echo 
    "Welcome, {$_SESSION['hey']}";
    ? should work. works for me. (In fact, it'll work even if you don't start a session first - though, of course, it wouldn't work on subsequent pages).

  6. #6
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,509
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    No, I mean how come this works

    Code:
    <?php
    session_start();
    $hey = "Example";
    echo "Welcome $hey";  
    ?>
    But this doesn't

    Code:
    <?php
    session_start();
    $_SESSION['username'] = "Example";
    echo "Welcome $_SESSION['username']";  
    ?>
    unless you rap the session variable in {}

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Because any array value simply doesn't work in double quotes the way a simple variable does:

    "$yes"
    "$array['no']"

    That's how PHP is designed.

    Regardless, good code shouldn't rely on variables in double quotes anyway. It's cleaner, faster and better for reasons like this (if you want to use arrays, or functions, etc.) to leave the quotes and concatenate instead:

    "something".$variable."something"
    "something".$array['entry']."something"
    "something".myfunction('value')."something"

    And beyond that, it's actually better to avoid the double quotes entirely because single quotes are faster (they don't make PHP search for variables, etc.):

    'something'.$variable.'something'
    'something'.$array['entry'].'something'
    'something'.myfunction('value').'something'


    The only time it is good practice to use double quotes is when you need a special character like a new line or tab:
    "\n"
    "\t"

    (But '\n' is interpreted literally as a slash and an N.)


    So a line of code might look something like this:

    echo 'The value of my variable is: ' . $variable . '.' . "\n";
    text + variable's value + text (period) + line break (in double quotes)
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,509
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Hmmm, thanks for that Djr33!

    Is there ever any time you should do this -

    PHP Code:
    echo 'Welcome'$_SESSION['username']; 

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    The comma and the period are functionally equivalent with echo. Echo can either take a single argument (eg, $variable, or 'Hello World', or 'Hello '.$variable), or it can take multiple arguments (eg, 'Hello ',$variable).

    The difference is that multiple arguments are never joined, except that they are both output sequentially, so it appears that they are joined.

    Technically, using commas is better for processing.

    But... this only works with echo (and print, I think). So you can't do this in other cases. For example, you can't do:

    $variable = 'something', 'something else';

    So personally I ignore the comma option for consistency. The quotes issues discussed above are consistent across all uses, regardless of whether it's output via echo or saving to a variable or arguments in a function or anything else.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,509
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Thanks for all the info!

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
  •