Click to See Complete Forum and Search --> : Seperate vars from strings
LordShryku
10-07-2003, 03:04 AM
In this post (http://www.phpbuilder.com/board/showthread.php?s=&postid=10426727), someone asked me why I seperate my variables from my strings.
ex.
echo "Hello ".name;
//instead of
echo "Hello $name";
I've just always done this, and ran into some trouble working with $_GET and $_POST vars inside of the string. Got me thinking...I've been asked this quite a few times now. I'm wondering what everyone else does. Does anyone else see a need to do this, or think that it is the "best practice", as I've been preaching? Or am I just full of hot air?
Weedpacket
10-07-2003, 04:15 AM
I tend to separate them; I've noticed a measurable slowdown when PHP's interpolating. Plus, it makes syntax highlighting more informative and gives me convenient places to insert breaks in long lines.
jebster
10-07-2003, 07:24 AM
I do that to! Well I do this:
echo 'Hello ' . $name;
Seems neater and easier to read! :)
laserlight
10-07-2003, 09:47 AM
I think it came up in one of our best practices discussions.
Of course the answer is as Weedpacket says.
Moonglobe
10-07-2003, 10:37 AM
My editor highlights variables in "" strings, but i stilll do it as i always have. I didn't learn that way, but i was corrected early, and i got used to it.
jstarkey
10-07-2003, 11:43 AM
Originally posted by LordShryku
I've just always done this, and ran into some trouble working with $_GET and $_POST vars inside of the string.
echo "Hello {$_GET['var']}";
Should work, if that's what you were referring to.
LordShryku
10-07-2003, 12:12 PM
Yeah, I've seen that'll work, but what I was really referring to was
echo "Hello $_GET['name']";
Moonglobe
10-07-2003, 12:14 PM
Originally posted by LordShryku
Yeah, I've seen that'll work, but what I was really referring to was
echo "Hello $_GET['name']"; If your not using braces then you do not need the single quotes inside string. this is the only place this is valid. the above should be:<?php echo "Hello $_GET[name]"; ?>
jstarkey
10-07-2003, 12:21 PM
Originally posted by Moonglobe
this is the only place this is valid. the above should be:<?php echo "Hello $_GET[name]"; ?>
Only until 'name' becomes a reserved key name, though.
[afterthought] actually, I guess by the time you've echo'd it, you've already changed the value of the reserved key anyway. My bad.
stolzyboy
10-07-2003, 12:23 PM
Originally posted by Moonglobe
If your not using braces then you do not need the single quotes inside string. this is the only place this is valid. the above should be:<?php echo "Hello $_GET[name]"; ?>
yes, however PHP states it is better to use [''] rather than []
snippet from php.net
Note: Enabling E_NOTICE during development has some benefits. For debugging purposes: NOTICE messages will warn you about possibls bugs in your code. For example, use of unassigned values are warned. It is extremely useful to find typos and to save time for debugging. NOTICE messages will warn you about bad style. For example, $arr[item] is better to be written as $arr['item'] since PHP tries to treat "item" as constant. If it is not a constant, PHP assumes it is a string index for the array.
Moonglobe
10-07-2003, 12:26 PM
Yes, i know, and i always quote my indecies. Outside of strings. but thats all the time, becuase as i said before, i never interpolate. However, i do believe the manual specifically says you should omit the quotes of an index if you're interpolating.
stolzyboy
10-07-2003, 12:29 PM
Originally posted by Moonglobe
Yes, i know, and i always quote my indecies. Outside of strings. but thats all the time, becuase as i said before, i never interpolate. However, i do believe the manual specifically says you should omit the quotes of an index if you're interpolating.
yep, that's why i don't interpolate, and obviously if you don't use {}'s you have to lose the ''s or ya know what'll happen...
n00854180t
10-07-2003, 12:49 PM
I learned to do this:
<?php echo "Hello " . $world; ?>
Rather than the other way. I like it better because it is easier for me to read and understand.
LordShryku
10-07-2003, 01:58 PM
Yeah, that's the way I learned too. Wanted to get a "feeler" out to see what everyone else's take on it was though. Thanks for the input....
GilesGuthrie
10-07-2003, 09:11 PM
I do:
<?php echo "Hello ".$name; ?>
Although I have been breaking out of PHP, in large pages where there's a lot of HTML and not much PHP:
<?php
if($dogreet)
{
?>Hello <?php
echo $name;
}
?>
Which is faster to render, although can lead to some illegibility in the code. Helpfully though, in DWMX, each little PHP block has its own icon, and in the split screen view you can bounce from code section to code section by clicking the icons. I find this helps me to control my code and my layout.
It's working for me at the moment, but I will probably change it at some point!
rpanning
10-07-2003, 09:18 PM
I also seperate my variables from my strings. I want to pick up as much preformance as possible and it's easier to read.
stolzyboy
10-08-2003, 09:22 AM
Originally posted by GilesGuthrie
I do:
[PHP]Helpfully though, in DWMX, each little PHP block has its own icon, and in the split screen view you can bounce from code section to code section by clicking the icons. I find this helps me to control my code and my layout.
It's working for me at the moment, but I will probably change it at some point!
Or... if you use a colored text editor, the PHP will stand out from the HTML.
Like HomeSite, all the PHP has a light gray background whereas the HTML is white.
Merve
10-08-2003, 08:08 PM
Yup, I separate vars from strings. Concatenation is good.
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.