View Full Version : Resolved echoing strings and variables
keyboard
06-01-2012, 11:45 PM
echo "Welcome $_SESSION['username']";
echo 'Welcome' . $_SESSION['username'];
echo 'Welcome', $_SESSION['username'];
Is there any difference in these three methods and which of them is quickest?
Thanks, Keyboard1333
james438
06-02-2012, 03:52 AM
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.
echo "Welcome {$_SESSION['username']}";
The following will also work, but as I recall it is not quite proper.
echo "Welcome $_SESSION[username]";
You can read more about it here (http://us3.php.net/types.string).
keyboard
06-02-2012, 04:15 AM
Hmmmm, interesting...
So how come -
<?php
session_start();
$hey = "Example";
echo "Welcome $hey";
?>
works, but not when it's a session variable?
james438
06-02-2012, 04:49 AM
I can't give you a simple answer at the moment, but I would start reading here (http://us3.php.net/types.string#language.types.string.parsing) and contine for about 2 - 3 pages.
No worries, almost all of it is examples.
So how come -
<?php
session_start();
$hey = "Example";
echo "Welcome $hey";
?>
works, but not when it's a session variable?
do you mean
<?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).
keyboard
06-02-2012, 05:47 AM
No, I mean how come this works
<?php
session_start();
$hey = "Example";
echo "Welcome $hey";
?>
But this doesn't
<?php
session_start();
$_SESSION['username'] = "Example";
echo "Welcome $_SESSION['username']";
?>
unless you rap the session variable in {}
djr33
06-02-2012, 10:18 AM
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)
keyboard
06-02-2012, 11:38 AM
Hmmm, thanks for that Djr33!
Is there ever any time you should do this -
echo 'Welcome', $_SESSION['username'];
djr33
06-02-2012, 12:04 PM
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.
keyboard
06-02-2012, 10:16 PM
Thanks for all the info!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.