[thelist] single quotes and arrays

Nadeem Hosenbokus nadeem at nadeemh.com
Sun Apr 24 06:57:51 CDT 2016


I'm glad that this is active again, etc, etc. Onto the problem:

In the first instance, an array key is string so $_GET[pageid] should
actually be $_GET['pageid']. $_GET is an array, "pageid" is a key which is
treated as a string.

In the second instance, you get a fatal error because of the general syntax.
Direct references to variables work inside double quotes:

	$a = 0;
	$b = 'Label';
	echo "<option value="$a">$b</option>";

But PHP operations do *not* work inside double quote (at least not without
curly brackets but I won't go into that). Your particular code is asking PHP
to reference a value inside an array.

If you iterate through your array, you should be able to do this:

	$output	=	'';
	foreach($row as $id=>$title){
		$output	.=	"<option value=\"$id\">$title</option>";
	echo $output;

Personally I don't like using escaped double quotes, so the other way is
like this:


Note that the \n has to be in double quotes.

Hope this helps a bit.

Nadeem Hosenbokus
(230) 5766 9169

-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Garth Hagerman
Sent: 24 April 2016 04:52
To: thelist at lists.evolt.org
Subject: [thelist] single quotes and arrays

Hi everybody-
Since The List is alive again (hooray!), here's a query.
I've been making the same boo-boo over and over for years. I have a rote
mechanical understanding of what to do about it, but I do not understand the
conceptual background.

In PHP, I pass info to another page using a form. The page receiving the
info has a line like this:
$pageid = $_GET[pageid];

This works, so there doesn't seem to be anything wrong, so I do it over and
over and over. But it logs something like this into the error_log:
"PHP Notice:  Use of undefined constant pageid - assumed 'pageid' ". 

Since this happens a lot, the error_log gets to be a very large file.

So, I understand how to stop getting that notice: put single quotes around
But why? pageid isn't a string, it's the name of an array element. If I do
the same single quote thing with other arrays, something like: 
echo("<option value=\"$row['id']\">$row['title']</option>\n");
 it's a fatal error and the whole page explodes.

I've tried to find a reasonable explanation online, but haven't found
anything which makes the little "eureka!" light over my head light up.

  Thanks in advance

                           * * * * * * * * * * * * * * * *
                               my online portfolio



* * Please support the community that supports you.  * *

For unsubscribe and other options, including the Tip Harvester and archives
of thelist go to: http://lists.evolt.org Workers of the Web, evolt ! 

More information about the thelist mailing list