By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,808 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

single quotes, double quotes and "undefined index"

P: n/a
I am trying to write clean code but keep having trouble deciding
when to quote an array index and when not to.

sometimes when I quote an array index inside of double quotes I
get an error about enased whitespace (to my best memory)

AT other times I get an undefined index notice as below:

Notice: Undefined index: last_reminder_id in...

the 2nd line (which wraps to the 3rd in this posting
is the one listed in the error message:

$sql = "SELECT * from notes
where recall_date '0' and recall_date <= '$now' and id >
'$_SESSION[last_reminder_id]'";

what is the correct syntax and/or can anyone point me at a
reference that will explain this ?

bill
Jul 13 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
On Jul 13, 9:56 am, bill <nob...@spamcop.netwrote:
I am trying to write clean code but keep having trouble deciding
when to quote an array index and when not to.

sometimes when I quote an array index inside of double quotes I
get an error about enased whitespace (to my best memory)

AT other times I get an undefined index notice as below:

Notice: Undefined index: last_reminder_id in...

the 2nd line (which wraps to the 3rd in this posting
is the one listed in the error message:

$sql = "SELECT * from notes
where recall_date '0' and recall_date <= '$now' and id >
'$_SESSION[last_reminder_id]'";

what is the correct syntax and/or can anyone point me at a
reference that will explain this ?

bill
If you have an array called $foo with a key called "bar" the correct
syntax is like this:

$foo['bar'];

or

$foo["bar"];

or

$key = 'bar';
$foo[$key];

Jul 13 '07 #2

P: n/a
On 13 Jul, 14:56, bill <nob...@spamcop.netwrote:
I am trying to write clean code but keep having trouble deciding
when to quote an array index and when not to.

sometimes when I quote an array index inside of double quotes I
get an error about enased whitespace (to my best memory)

AT other times I get an undefined index notice as below:

Notice: Undefined index: last_reminder_id in...

the 2nd line (which wraps to the 3rd in this posting
is the one listed in the error message:

$sql = "SELECT * from notes
where recall_date '0' and recall_date <= '$now' and id >
'$_SESSION[last_reminder_id]'";

what is the correct syntax and/or can anyone point me at a
reference that will explain this ?

bill
http://www.php.net/manual/en/

Jul 13 '07 #3

P: n/a
..oO(bill)
>I am trying to write clean code but keep having trouble deciding
when to quote an array index and when not to.
Literal array indexs always have to be quoted, unless the entire array
is embedded in double-quoted string without using curly syntax:

print $foo['bar'];

print "hello $foo[bar]";

print "hello {$foo['bar']}";

All correct.
>sometimes when I quote an array index inside of double quotes I
get an error about enased whitespace (to my best memory)

AT other times I get an undefined index notice as below:

Notice: Undefined index: last_reminder_id in...

the 2nd line (which wraps to the 3rd in this posting
is the one listed in the error message:

$sql = "SELECT * from notes
where recall_date '0' and recall_date <= '$now' and id >
'$_SESSION[last_reminder_id]'";
Syntax is correct. I'm just wondering why the values for the ID and date
fields are quoted. If a value is numeric, don't quote it. So the parser
might be right and the index is just not defined?
>what is the correct syntax and/or can anyone point me at a
reference that will explain this ?
All possible syntax variants are explained in the manual (see the
examples).

Array do's and don'ts
http://www.php.net/manual/en/languag...es.array.donts

Micha
Jul 13 '07 #4

P: n/a
Michael Fesser wrote:
.oO(bill)
>I am trying to write clean code but keep having trouble deciding
when to quote an array index and when not to.

Literal array indexs always have to be quoted, unless the entire array
is embedded in double-quoted string without using curly syntax:

print $foo['bar'];

print "hello $foo[bar]";

print "hello {$foo['bar']}";

All correct.
>sometimes when I quote an array index inside of double quotes I
get an error about enased whitespace (to my best memory)

AT other times I get an undefined index notice as below:

Notice: Undefined index: last_reminder_id in...

the 2nd line (which wraps to the 3rd in this posting
is the one listed in the error message:

$sql = "SELECT * from notes
where recall_date '0' and recall_date <= '$now' and id >
'$_SESSION[last_reminder_id]'";

Syntax is correct. I'm just wondering why the values for the ID and date
fields are quoted. If a value is numeric, don't quote it. So the parser
might be right and the index is just not defined?
I hate that: The index *is* undefined the first time through !

I sure do appreciate the other pointers and will study them.
>
>what is the correct syntax and/or can anyone point me at a
reference that will explain this ?

All possible syntax variants are explained in the manual (see the
examples).

Array do's and don'ts
http://www.php.net/manual/en/languag...es.array.donts

Micha
Jul 13 '07 #5

P: n/a
Michael Fesser schrieb:
.oO(bill)
>I am trying to write clean code but keep having trouble deciding
when to quote an array index and when not to.

Literal array indexs always have to be quoted, unless the entire array
is embedded in double-quoted string without using curly syntax:

print $foo['bar'];

print "hello $foo[bar]";

print "hello {$foo['bar']}";

All correct.
Additionnally, if you want to be safe, you can just concatenate the string:
$sql = "SELECT * FROM foo WHERE bar='".$_SESSION['bar']."'";
Jul 13 '07 #6

P: n/a
..oO(Markus)
>Additionnally, if you want to be safe, you can just concatenate the string:
$sql = "SELECT * FROM foo WHERE bar='".$_SESSION['bar']."'";
Sure, but IMHO this kind of defeats the purpose of double-quoted strings
in PHP. When I see something like

print "text ".$var." more text ".$anotherVar." and so on";

or even worse

print "<a href=\"".$url."\">".$text."</a>";

then I'm getting...what's that in English - goose bumps (de: Gänsehaut)?
It's just terrible and hurts the eye of an experienced programmer. Not
to mention that jumping in and out of string parsing and mixing it with
escaping and different quote signs is very error-prone.

Micha
Jul 13 '07 #7

P: n/a
..oO(bill)
>Michael Fesser wrote:
>Syntax is correct. I'm just wondering why the values for the ID and date
fields are quoted. If a value is numeric, don't quote it. So the parser
might be right and the index is just not defined?

I hate that: The index *is* undefined the first time through !
If you know that an index will be undefined on the first run, then you
have to take care of that in your scripts and use a default value if
necessary. isset() exists.

Micha
Jul 13 '07 #8

P: n/a
Michael Fesser wrote:
print $foo['bar'];
print "hello $foo[bar]";
print "hello {$foo['bar']}";

All correct.
Correct, yes. But the middle example is unwise.

define('bar', 'baz');

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 24 days, 18:03.]

demiblog 0.2.0 Released
http://tobyinkster.co.uk/blog/2007/0...emiblog-0.2.0/
Jul 15 '07 #9

P: n/a
..oO(Toby A Inkster)
>Michael Fesser wrote:
>print $foo['bar'];
print "hello $foo[bar]";
print "hello {$foo['bar']}";

All correct.

Correct, yes. But the middle example is unwise.

define('bar', 'baz');
Didn't test it, but according to the manual it should work:

| // The following is okay as it's inside a string. Constants are not
| // looked for within strings so no E_NOTICE error here
| print "Hello $arr[fruit]"; // Hello apple

Micha
Jul 15 '07 #10

P: n/a
Michael Fesser wrote:
Didn't test it, but according to the manual it should work
It will work, yes, but it's unwise, as you'd be skating on very thin ice.

echo $foo[bar];
echo "{$foo[bar]}";

will both use the constant 'bar' instead of the string 'bar' if it exists.
It's very easy to slip up in this area, so safer to always quote strings
used as array indices.

PHP is known to change its syntax from time to time, and may well become
less forgiving in the future with regard to something like:

echo "$foo[bar]";

and I'd imagine such a bug may be very tricky to track down.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 24 days, 23:34.]

demiblog 0.2.0 Released
http://tobyinkster.co.uk/blog/2007/0...emiblog-0.2.0/
Jul 15 '07 #11

P: n/a
Michael Fesser schrieb:
.oO(Markus)
>Additionnally, if you want to be safe, you can just concatenate the string:
$sql = "SELECT * FROM foo WHERE bar='".$_SESSION['bar']."'";

Sure, but IMHO this kind of defeats the purpose of double-quoted strings
in PHP. When I see something like

print "text ".$var." more text ".$anotherVar." and so on";

or even worse

print "<a href=\"".$url."\">".$text."</a>";

then I'm getting...what's that in English - goose bumps (de: Gänsehaut)?
It's just terrible and hurts the eye of an experienced programmer. Not
to mention that jumping in and out of string parsing and mixing it with
escaping and different quote signs is very error-prone.
I partially agree. Anyway I see nothing Gänsehaut-prone about
concatenating single-quoted strings, such as

print 'text '.$var.' more text '.$arr['foo'].' '.$obj->bar.'st time';

It is IMO a good habit if somebody does not want to run into the
questions discussed above, and there seem to be even performance reasons
for it:
http://www.php.net/manual/de/languag...ring.php#74627

But I admit, the above $sql example with double quotes originates in my
early insecureness about if double quotes would work inside an SQL
query, so I got used to double-quote those and use single quotes inside.
Jul 16 '07 #12

P: n/a
Markus wrote:
print 'text '.$var.' more text '.$arr['foo'].' '.$obj->bar.'st time';
printf( 'text %s more text %s %d time',
$var, $arr['foo'], ordinal($obj->bar));

function ordinal ($number, $lang='en')
{
$number = (int)$number;

if ($lang=='en')
{
if ($number==11) return '11th';
if ($number==12) return '12th';
if ($number==13) return '13th';

switch ($number % 10)
{
case 1: return "{$number}st";
case 2: return "{$number}nd";
case 3: return "{$number}rd";
default: return "{$number}th";
}
}
else
return $number;
}

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 25 days, 17:56.]

demiblog 0.2.0 Released
http://tobyinkster.co.uk/blog/2007/0...emiblog-0.2.0/
Jul 16 '07 #13

P: n/a
Toby A Inkster wrote:
printf( 'text %s more text %s %d time',
$var, $arr['foo'], ordinal($obj->bar));
Oops -- my finger slipped.

s/d time/s time/;

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 25 days, 20:03.]

demiblog 0.2.0 Released
http://tobyinkster.co.uk/blog/2007/0...emiblog-0.2.0/
Jul 16 '07 #14

P: n/a
On Mon, 16 Jul 2007 10:01:50 +0200, Markus wrote:
I partially agree. Anyway I see nothing Gänsehaut-prone about
concatenating single-quoted strings, such as

print 'text '.$var.' more text '.$arr['foo'].' '.$obj->bar.'st time';

It is IMO a good habit if somebody does not want to run into the
questions discussed above, and there seem to be even performance reasons
for it:
Assuming it is slower to use variables within strings/double quotes* -
it comes down to the argument of developer time/cost versus server/CPU
cycles cost.

Personally I find double quotes with embedded variables to be MUCH more
readable and therefore a 0.1 second difference over a million prints is
negligible (particularly in comparison to the delay while waiting for the
DB servers to respond).

* The next comment after the one you linked suggests that it's faster to
do string interpolation than concatenation with either single or double
quotes:
http://www.php.net/manual/en/languag...ring.php#74257
Cheers,
Andy
Jul 17 '07 #15

P: n/a
..oO(Andy Jeffries)
>On Mon, 16 Jul 2007 10:01:50 +0200, Markus wrote:
>>
print 'text '.$var.' more text '.$arr['foo'].' '.$obj->bar.'st time';

It is IMO a good habit if somebody does not want to run into the
questions discussed above, and there seem to be even performance reasons
for it:

Assuming it is slower to use variables within strings/double quotes* -
it comes down to the argument of developer time/cost versus server/CPU
cycles cost.
ACK

The typical bottlenecks in a script are usually IO operations, database
stuff or bad algorithms (bubblesort vs. quicksort for example). Printing
out data shouldn't be an issue at all in most cases.
>Personally I find double quotes with embedded variables to be MUCH more
readable and therefore a 0.1 second difference over a million prints is
negligible (particularly in comparison to the delay while waiting for the
DB servers to respond).
Yep. Additionally I often prefer printf/sprintf when there are a lot of
variables or expressions to embed. It's even slower than a plain print
or echo statement, but much more flexible and readable.

Micha
Jul 17 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.