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?PHP Code:echo 'Welcome', $_SESSION['username'];
Thanks, Keyboard1333
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?PHP Code:echo 'Welcome', $_SESSION['username'];
Thanks, Keyboard1333
Last edited by keyboard; 06-02-2012 at 10:16 PM.
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.
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.Code:echo "Welcome $_SESSION[username]";
To choose the lesser of two evils is still to choose evil. My personal site
Hmmmm, interesting...
So how come -
works, but not when it's a session variable?Code:<?php session_start(); $hey = "Example"; echo "Welcome $hey"; ?>
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
No, I mean how come this works
But this doesn'tCode:<?php session_start(); $hey = "Example"; echo "Welcome $hey"; ?>
unless you rap the session variable in {}Code:<?php session_start(); $_SESSION['username'] = "Example"; echo "Welcome $_SESSION['username']"; ?>
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
Hmmm, thanks for that Djr33!
Is there ever any time you should do this -
PHP Code:echo 'Welcome', $_SESSION['username'];
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
Bookmarks