[thelist] single quotes and arrays
Nadeem Hosenbokus
nadeem at nadeemh.com
Sun Apr 24 22:24:05 CDT 2016
I may have misunderstood what you intended (apologies if so) but this code:
title = 'daniel'
$_GET[ title ]
does not resolve to $_GET['daniel'].
Its usage would be like this:
$title = 'daniel';
$_GET['title'] = $title;
echo $_GET['title'];
would print out "daniel".
echo $_GET['daniel'] would result in some kind of error because the key isn't set in the array.
Nadeem Hosenbokus
(230) 5766 9169
www.nadeemh.com
http://mu.linkedin.com/in/nadeemhosenbokus
-----Original Message-----
From: thelist-bounces at lists.evolt.org [mailto:thelist-bounces at lists.evolt.org] On Behalf Of Volkan Özçelik
Sent: 24 April 2016 06:08
To: thelist at lists.evolt.org
Subject: Re: [thelist] single quotes and arrays
My guesstimate is:
In `$pageid = $_GET[pageid];` you are doing a direct assignment and hence the PHP interpreter can get clever enough to "catch" what you meant.
In ` ""<option value=\"$row['id']\">$row['title']</option>\n" ` however, it is string interpolation; so it’s an additional level of indirection, and PHP interpreter is not ready to infer what you meant and erring out.
As a best practice you should always be using quoted attributes as not using them may be mixed with a variable with the same name. (
e.g. `
title = 'daniel'
$_GET[ title ]
`
will resolve to $_GET['daniel'] which probably will be undefined.
Just my 2c.
Would love to hear other PHP experts’ ideas on that though.
HTH.
Volkan.
On Sat, Apr 23, 2016 at 5:52 PM Garth Hagerman <hagerman at mcn.org> wrote:
> 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 pageid.
> 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
> Garth
>
>
> * * * * * * * * * * * * * * * *
> my online portfolio
>
> http://garthhagerman.com/
>
>
> --
>
> * * Please support the community that supports you. * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester and
> archives of thelist go to: http://lists.evolt.org Workers of the Web,
> evolt !
>
--
* * Please support the community that supports you. * * http://evolt.org/help_support_evolt/
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