Advanced Search

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,420
    Thanks
    37
    Thanked 84 Times in 83 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 keyboard1333; 06-02-2012 at 10:16 PM.
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,585
    Thanks
    71
    Thanked 80 Times in 78 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,420
    Thanks
    37
    Thanked 84 Times in 83 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?
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,585
    Thanks
    71
    Thanked 80 Times in 78 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,134
    Thanks
    61
    Thanked 438 Times in 427 Posts
    Blog Entries
    7

    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).
    Adrian ~ facebook | gist/github

    ['66.215.156.37','208.75.149.97'] // ip,ip array!
    "Take that sticker *off* your hat; you look stupid" --Wil Wheaton

  6. #6
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,420
    Thanks
    37
    Thanked 84 Times in 83 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 {}
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    11,792
    Thanks
    227
    Thanked 657 Times in 645 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>| Deutsch | italiano | español | português | català | un peu de français | Ninasoma Kiswahili | 日本語の学生でした。| درست العربية

  8. #8
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,420
    Thanks
    37
    Thanked 84 Times in 83 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']; 
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    11,792
    Thanks
    227
    Thanked 657 Times in 645 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>| Deutsch | italiano | español | português | català | un peu de français | Ninasoma Kiswahili | 日本語の学生でした。| درست العربية

  10. #10
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,420
    Thanks
    37
    Thanked 84 Times in 83 Posts
    Blog Entries
    3

    Default

    Thanks for all the info!
    keebs - keyboard1333 [at] gmail [dot] com
    Anime Views Forums

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
  •