473,414 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

single quotes, double quotes and "undefined index"

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
15 4435
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
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
..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
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
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
..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
..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
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
..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
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
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
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
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
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: NotGiven | last post by:
I have display error ON and error reporting to E_ALL. I have a form that opens fine. When you submit it, all the fields that have nothing in them, for example an un-selected radio button, throw...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
1
by: Pavils Jurjans | last post by:
Hello, I am building custom hashtable class, and thinking about value retrieval issues. The thing is, that sometimes the hashtable value may contain value null. If someone is reading this value...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
9
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined...
5
by: Nathan Sokalski | last post by:
I have an ASP.NET application which is giving the following JavaScript error: 'theForm' is undefined However, when I do a View Source one of the <scriptelements is as follows: <script...
3
by: car6006 | last post by:
Why am I getting the "undefined" in my txt box, I have tried everything! I am new here so I truly hope you are able to assist me... Thanks <html> <head> <title>Random Proverbs</title>...
5
by: Pseudonyme | last post by:
Dear All : Ever had an httpd error_log bigger than the httpd access log ? We are using Linux-Apache-Fedora-Httpd 2006 configuration. The PHP lines code that lead too tons of errors are : ...
4
by: mariaz | last post by:
Hello, I am getting a Notice error on my website that says: Notice: Undefined index: _created_formatted in C:\EasyPHP1-8\www\eP\components\com_jambook\jxtemplate.php(125) : regexp code on line 1 ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.