$_POST['variable'], $_GET['variable']...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

$_POST['variable'], $_GET['variable']...

Post by SidewinderX »

First off what do you refer to these things as so in my future posts i dont have to refer to them as 'variable things'? $_POST['variable'], $_GET['variable'], $row['variable'] ect...

secondly, i downladed a code ive been modifying...below is an excerpt of the code

Code: Select all

if ($row[nk_link] != ""){
	$nk_author = "<a href=\"$row[nk_link]\" target=\"_blank\">$row[nk_author]</a>";
	}
	else{
	$nk_author = $row[nk_author];
	}
	
	if ($row[bb_link] != ""){
	$bb_author = "<a href=\"$row[bb_link]\" target=\"_blank\">$row[bb_author]</a>";
	}
	else{
	$bb_author = $row[bb_author];
	}
	
	if ($row[t_url] != ""){
	$download = "<a href=\"$row[t_url]\" target=\"_blank\">DOWNLOAD</a>";
	}
	else{
	$download = "Download N/A";
	}
	
	if ($row[t_views] != ""){
	$views = $row[t_views];
	}
	else{
	$views = "0";
	}
If you notice all the variable things are listed with outsingle quotes $row[t_views]. Is there any difference? Every time i use those variable things i always use the '' $row['t_views'], should i change them?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: $_POST['variable'], $_GET['variable']...

Post by Luke »

SidewinderX wrote:First off what do you refer to these things as so in my future posts i dont have to refer to them as 'variable things'? $_POST['variable'], $_GET['variable'], $row['variable'] ect...
uhh... arrays?
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: $_POST['variable'], $_GET['variable']...

Post by Luke »

ALWAYS use quotes. you have to. also... you can't insert array values into a string without concatenation:

Code: Select all

"<a href=\"$row[t_url]\" target=\"_blank\">DOWNLOAD</a>";  //ERROR
This is the correct way:

Code: Select all

"<a href=\"" . $row['t_url'] . "\" target=\"_blank\">DOWNLOAD</a>";
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The manual will tel you that quotes are optional. The thing to remember is that if you don't use quotes, PHP will assume that the index you chose should have been quoted or that the value you entered as an index is a constant. Clean coding tells you to wrap your array indeces in quotes.

I would also recommend using single quotes instead of double quotes. I am still looking for the benchmarks, but I read on the PHP web site that double quotes are unusually slow when it comes to parsing. Benchmarks showed that double quotes take almost four times as long to parse as opposed to double quotes.

So....

Code: Select all

<?php
// instead of this:
echo "<a href=\"$row[t_url]\" target=\"_blank\">DOWNLOAD</a>"; 

//use this:
echo '<a href="' . $row['t_url'] . '" target="_blank">DOWNLOAD</a>'; 
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

plus, then you don't have to escape the double quotes in your html with a backslash. Also, I have heard it's faster to concatenate rather than pop variables directly into your strings so:

Code: Select all

// Instead of this:
echo '<a href="$variable">Something</a>';
// Do this
echo '<a href="' . $varibale . '">Something</a>';
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Re: $_POST['variable'], $_GET['variable']...

Post by GM »

The Ninja Space Goat wrote:also... you can't insert array values into a string without concatenation:

Code: Select all

"<a href="$row[t_url]" target="_blank">DOWNLOAD</a>";  //ERROR
This is the correct way:

Code: Select all

"<a href="" . $row['t_url'] . "" target="_blank">DOWNLOAD</a>";
Not entirely true, you can put the values in curly brackets:

Code: Select all

"<a href="{$row['t_url']}" target="_blank">DOWNLOAD</a>";
In my view this makes it more difficult to read the code though. It is just an alternative way.

Also, I remember reading somewhere (may even have been here) that the difference between single and double quotes is negligable when it comes to performance. I've got no hard evidence to back this up, so it could be wrong.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Re: $_POST['variable'], $_GET['variable']...

Post by Grim... »

The Ninja Space Goat wrote:ALWAYS use quotes. you have to. also... you can't insert array values into a string without concatenation
Yes you can. Try it.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I stand corrected.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: $_POST['variable'], $_GET['variable']...

Post by RobertGonzalez »

GM wrote:Also, I remember reading somewhere (may even have been here) that the difference between single and double quotes is negligable when it comes to performance. I've got no hard evidence to back this up, so it could be wrong.
I found the benchmarks I was talking about. They are in the user comments section of the PHP manual page on echo.

There is also an image (32K) that shows printed results.
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Re: $_POST['variable'], $_GET['variable']...

Post by julian_lp »

GM wrote: Not entirely true, you can put the values in curly brackets:

Code: Select all

"<a href="{$row['t_url']}" target="_blank">DOWNLOAD</a>";
Please don't ever do that. It's really annoying.

Always use ' instead of ", and concatenate the output as needed

Code: Select all

'<a href="'.$row['t_url']. ******
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: $_POST['variable'], $_GET['variable']...

Post by Christopher »

julian_lp wrote:Please don't ever do that. It's really annoying.
Last time I checked "because some programmer finds it really annoying" was not a reason for choosing a programming practice -- if it was no one would be able to do anything.
julian_lp wrote:Always use ' instead of ", and concatenate the output as needed
Both are valid syntax in PHP and can be used as approprite and according to programmer preference.
(#10850)
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

Well, what i find easiest to do (in normal programs):

Code: Select all

$string = "<a href=\"" . $array['value'] . "\">ect.";
But atm i seem to be creating alot of programs that i need to edit certain strings via an admin panel.
So i use something like:

Code: Select all

$string = myfunc("<a href=\"%s\">ect.", $array['value']);
and my 'myfunc' is basicly an alias to sprintf BUT, it checks md5(the 1st argument) in a mysql database and if the value in the DB is not the same as the 1st arg, it uses the DB value.

quite good if you want easy translate software, but slow... very slow.
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Re: $_POST['variable'], $_GET['variable']...

Post by julian_lp »

arborint wrote:
julian_lp wrote:Please don't ever do that. It's really annoying.
Last time I checked "because some programmer finds it really annoying" was not a reason for choosing a programming practice -- if it was no one would be able to do anything.
You're absolutely right. But there seems to be some kind of agreed related to this topic. I mean, take 100 programmers and ask them about wich option they preffer, and you'll see that using

"/"{

is annoying.

having said that, everybody is free to choose any option

arborint wrote:
julian_lp wrote:Always use ' instead of ", and concatenate the output as needed
Both are valid syntax in PHP and can be used as approprite and according to programmer preference.
I've read somewhere that is considerably faster the ' than the ", beacause the parser doesn't try to find anything between ' .
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Single and double quotes are both perfrectly acceptable syntactically. One performs faster than the other, but both are usable in PHP without issue.
Post Reply