Page 1 of 2

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

Posted: Wed Jul 12, 2006 12:28 am
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?

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

Posted: Wed Jul 12, 2006 1:25 am
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?

Posted: Wed Jul 12, 2006 1:49 am
by jamiel

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

Posted: Wed Jul 12, 2006 10:12 am
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>";

Posted: Wed Jul 12, 2006 10:17 am
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>'; 
?>

Posted: Wed Jul 12, 2006 10:26 am
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>';

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

Posted: Wed Jul 12, 2006 10:34 am
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.

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

Posted: Wed Jul 12, 2006 10:42 am
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.

Posted: Wed Jul 12, 2006 11:01 am
by Luke
I stand corrected.

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

Posted: Wed Jul 12, 2006 11:30 am
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.

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

Posted: Wed Jul 12, 2006 2:06 pm
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']. ******

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

Posted: Wed Jul 12, 2006 2:17 pm
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.

Posted: Wed Jul 12, 2006 2:31 pm
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.

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

Posted: Wed Jul 12, 2006 2:43 pm
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 ' .

Posted: Wed Jul 12, 2006 3:00 pm
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.