$request problem 
March 7th, 2006, 05:05 AM
| | | |
Can anybody see a problem with this? Am I being stupid?
if (!isset($_REQUEST["input"]) || "" == $_REQUEST["input"])
{
throw new Exception("need query");
}
else
{
echo "<p>Search words: $_REQUEST[input]";
}
/*
* split input into array of query words
*/
$queries = array_map(mysql_real_escape_string, explode(" ",
$_REQUEST["input"]));
This works on one server, running PHP5, but not another. Both apache.
I've checked php.ini but cannot see why this seemingly simple bit of
code should fail in one instance.
In the one where it fails to work $queries is set to "" where I would
expect it to be set to an array of the values of $_REQUEST["input"],
split at each space.
No errors are reported even with error_reporting(E_ALL);
$_REQUEST is definitely set as the echo line prints out the input as
expected. | 
March 7th, 2006, 05:15 AM
| | | | re: $request problem
Joe Blow wrote:[color=blue]
> Can anybody see a problem with this? Am I being stupid?
>
> if (!isset($_REQUEST["input"]) || "" == $_REQUEST["input"])
> {
> throw new Exception("need query");
> }
> else
> {
> echo "<p>Search words: $_REQUEST[input]";
> }
> /*
> * split input into array of query words
> */
> $queries = array_map(mysql_real_escape_string, explode(" ",
> $_REQUEST["input"]));
>
>
> This works on one server, running PHP5, but not another. Both apache.
> I've checked php.ini but cannot see why this seemingly simple bit of
> code should fail in one instance.
>
> In the one where it fails to work $queries is set to "" where I would
> expect it to be set to an array of the values of $_REQUEST["input"],
> split at each space.
>
> No errors are reported even with error_reporting(E_ALL);
>
> $_REQUEST is definitely set as the echo line prints out the input as
> expected.
>[/color]
Maybe your problem is:
echo "<p>Search words: $_REQUEST['input']";
^ ^
P.S. I prefer to use single quotes around constants like this.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. jstucklex@attglobal.net
================== | 
March 11th, 2006, 01:15 AM
| | | | re: $request problem
"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
news:15edndHF8fD3jpDZ4p2dnA@comcast.com...[color=blue]
> Joe Blow wrote:[color=green]
> > Can anybody see a problem with this? Am I being stupid?
> >
> > if (!isset($_REQUEST["input"]) || "" == $_REQUEST["input"])
> > {
> > throw new Exception("need query");
> > }
> > else
> > {
> > echo "<p>Search words: $_REQUEST[input]";
> > }
> > /*
> > * split input into array of query words
> > */
> > $queries = array_map(mysql_real_escape_string, explode(" ",
> > $_REQUEST["input"]));
> >
> >
> > This works on one server, running PHP5, but not another. Both apache.
> > I've checked php.ini but cannot see why this seemingly simple bit of
> > code should fail in one instance.
> >
> > In the one where it fails to work $queries is set to "" where I would
> > expect it to be set to an array of the values of $_REQUEST["input"],
> > split at each space.
> >
> > No errors are reported even with error_reporting(E_ALL);
> >
> > $_REQUEST is definitely set as the echo line prints out the input as
> > expected.
> >[/color]
>
> Maybe your problem is:
>
> echo "<p>Search words: $_REQUEST['input']";
> ^ ^
>
> P.S. I prefer to use single quotes around constants like this.
>[/color]
No,
echo "<p>Search words: $_REQUEST[input]";
is (one of) the correct ways. Single quotes aren't needed/don't work when
the variable is enclosed in double quotes. Unless you enclose the variable
in curly braces:
echo "<p>Search words: {$_REQUEST['input']}";
will work properly too...
Norm | 
March 11th, 2006, 01:35 AM
| | | | re: $request problem
Not quotes a string index generates an E_NOTICE or something, i forget
which exactly, normally error reporting isn't set to show it. PHP
tries to resolve it as a define()'d constant, then treats it as a
quoted string. Not quoting the string index is generally bad form.
Single quotes provide a small performance gain as php doesn't try to
interpret the string.
For the question at hand:
I'm fairly sure that the callback has to be quoted, when using array
map, that may be the problem. Why are you individually escaping each
'word' of the input? Wouldn't it be easier to just apply it to the
entire $_REQUEST['input'] string?
$queries = array_map( 'mysql_real_escape_string', explode(" ",
$_REQUEST["input"])); | 
March 11th, 2006, 05:15 AM
| | | | re: $request problem
Norman Peelman wrote:[color=blue]
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
> news:15edndHF8fD3jpDZ4p2dnA@comcast.com...
>[color=green]
>>Joe Blow wrote:
>>[color=darkred]
>>>Can anybody see a problem with this? Am I being stupid?
>>>
>>>if (!isset($_REQUEST["input"]) || "" == $_REQUEST["input"])
>>> {
>>>throw new Exception("need query");
>>> }
>>> else
>>> {
>>> echo "<p>Search words: $_REQUEST[input]";
>>> }
>>>/*
>>>* split input into array of query words
>>>*/
>>> $queries = array_map(mysql_real_escape_string, explode(" ",
>>>$_REQUEST["input"]));
>>>
>>>
>>>This works on one server, running PHP5, but not another. Both apache.
>>>I've checked php.ini but cannot see why this seemingly simple bit of
>>>code should fail in one instance.
>>>
>>>In the one where it fails to work $queries is set to "" where I would
>>>expect it to be set to an array of the values of $_REQUEST["input"],
>>>split at each space.
>>>
>>>No errors are reported even with error_reporting(E_ALL);
>>>
>>>$_REQUEST is definitely set as the echo line prints out the input as
>>>expected.
>>>[/color]
>>
>>Maybe your problem is:
>>
>> echo "<p>Search words: $_REQUEST['input']";
>> ^ ^
>>
>>P.S. I prefer to use single quotes around constants like this.
>>[/color]
>
>
> No,
>
> echo "<p>Search words: $_REQUEST[input]";
>
> is (one of) the correct ways. Single quotes aren't needed/don't work when
> the variable is enclosed in double quotes. Unless you enclose the variable
> in curly braces:
>
> echo "<p>Search words: {$_REQUEST['input']}";
>
> will work properly too...
>
> Norm
>
>[/color]
Yes, if you turn on all errors you will get an E_NOTICE because the
string is not quoted.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. jstucklex@attglobal.net
================== | 
March 11th, 2006, 02:25 PM
| | | | re: $request problem
Jerry Stuckle in news:WvadnQQMO-tzxI_ZRVn-gw@comcast.com wrote:
[color=blue]
> Norman Peelman wrote:
>[color=green]
>> echo "<p>Search words: $_REQUEST[input]";
>>
>> is (one of) the correct ways. Single quotes aren't needed/don't work when
>> the variable is enclosed in double quotes. Unless you enclose the
>> variable in curly braces:
>>
>> echo "<p>Search words: {$_REQUEST['input']}";
>>
>> will work properly too...[/color]
>
> Yes, if you turn on all errors you will get an E_NOTICE because the
> string is not quoted.[/color]
No E_NOTICE at all. http://www.php.net/manual/en/languag...arsing.complex
There's no clear example stating this, but:
echo "$array[key]";
Is EXACTLY the same as:
echo "{$array['key']}";
With no notices being generated (I know since I always debug using E_ALL).
It's probably faster too, since I guess { "activates" a more complex parser.
Of course, that's the same as:
echo $array['key'];
--
ColdShine
"Experience is a hard teacher: she gives the test first, the lesson
afterwards." - Vernon Sanders law | 
March 11th, 2006, 07:45 PM
| | | | re: $request problem
ColdShine wrote:
[color=blue]
> There's no clear example stating this, but:
>
> echo "$array[key]";
>
> Is EXACTLY the same as:
>
> echo "{$array['key']}";[/color]
No, Jerry is right, they are not the same.
$array[key] is an array with a constant called key as index,
$array['key'] is an array with the string 'key' as index. You will see
the difference as soon as you assign a value to the constant key. :-)
When using $array[key] PHP will try to find the constant key, but
intelligently will assume you actually meant 'key' when it figures out
that the constant is undefined. Here is the error from the log:
[Sat Mar 11 19:29:40 2006] [error] [client 127.0.0.1] PHP Notice: Use
of undefined constant key - assumed 'key' in C:\\webroot\\test.php on
line 4
The PHP manual says on this topic:
Note: Enabling E_NOTICE during development has some benefits. For
debugging purposes: NOTICE messages will warn you about possible bugs in
your code. For example, use of unassigned values is warned. It is
extremely useful to find typos and to save time for debugging. NOTICE
messages will warn you about bad style. For example, $arr[item] is
better to be written as $arr['item'] since PHP tries to treat "item" as
constant. If it is not a constant, PHP assumes it is a string index for
the array.
However, strangely enough this warning showed up in the log only the
first time I accessed the page! No more warnings on any subsequent
accesses to the page or any other page with that kind of error!
I just checked the config, ignore_repeated_errors and
ignore_repeated_source are set to off. I also confirmed these settings
with phpinfo(). However, PHP reacts as if both are set to on! Did I just
find a bug?
That would explain why you get no warnings! You probably got your first
and only warning a long time ago if you always program like that. ;-)
P.S.: I use PHP 4.4.2 which is the latest released version 4 available.
Can anyone confirm that erroneous logging behaviour of this PHP version? | 
March 12th, 2006, 04:35 AM
| | | | re: $request problem
"Anonymous" <anonymous@nowhere.invalid> wrote in message
news:441326E4.66903A5B@nowhere.invalid...[color=blue]
> ColdShine wrote:
>[color=green]
> > There's no clear example stating this, but:
> >
> > echo "$array[key]";
> >
> > Is EXACTLY the same as:
> >
> > echo "{$array['key']}";[/color]
>
> No, Jerry is right, they are not the same.
>
> $array[key] is an array with a constant called key as index,
> $array['key'] is an array with the string 'key' as index. You will see
> the difference as soon as you assign a value to the constant key. :-)
>
> When using $array[key] PHP will try to find the constant key, but
> intelligently will assume you actually meant 'key' when it figures out
> that the constant is undefined. Here is the error from the log:
>
> [Sat Mar 11 19:29:40 2006] [error] [client 127.0.0.1] PHP Notice: Use
> of undefined constant key - assumed 'key' in C:\\webroot\\test.php on
> line 4
>
> The PHP manual says on this topic:
>
>
> Note: Enabling E_NOTICE during development has some benefits. For
> debugging purposes: NOTICE messages will warn you about possible bugs in
> your code. For example, use of unassigned values is warned. It is
> extremely useful to find typos and to save time for debugging. NOTICE
> messages will warn you about bad style. For example, $arr[item] is
> better to be written as $arr['item'] since PHP tries to treat "item" as
> constant. If it is not a constant, PHP assumes it is a string index for
> the array.
>
>
>
> However, strangely enough this warning showed up in the log only the
> first time I accessed the page! No more warnings on any subsequent
> accesses to the page or any other page with that kind of error!
>
> I just checked the config, ignore_repeated_errors and
> ignore_repeated_source are set to off. I also confirmed these settings
> with phpinfo(). However, PHP reacts as if both are set to on! Did I just
> find a bug?
>
> That would explain why you get no warnings! You probably got your first
> and only warning a long time ago if you always program like that. ;-)
>
> P.S.: I use PHP 4.4.2 which is the latest released version 4 available.
> Can anyone confirm that erroneous logging behaviour of this PHP version?[/color]
Consider this small piece of code:
---
<?php
error_reporting(E_ALL);
define('one','I am the defined one');
$str['one'] = 'I am the one!';
echo "<br>$str[one]";
echo '<br>'.one;
echo "<br>{$str['one']}";
?>
---
....output is exactly this:
I am the one!
I am the defined one!
I am the one!
.... no conflicts at all, no NOTICES, ERRORS, etc. The thing to wrap your
head around is that in a wierd technical way the associative index is
considered quoted as long as the entire variable is within double-quotes to
begin with. I personally prefer this way to multiple concatinations...
Norm | 
March 12th, 2006, 09:45 PM
| | | | re: $request problem
define('key', 'foobar', false);
$a = array('key'=>'value');
echo "$a[key]<br>";
echo "{$a[key]}<br>";
echo "{$a['key']}<br>";
output:
value
Errno: 8, Undefined index: foobar
value
It would seem constants are only expanded if they're in {}, and are
treated like strings otherwise. I wonder when this was changed, or if
its unintentional? | 
March 12th, 2006, 10:45 PM
| | | | re: $request problem
"Richard Levasseur" <richardlev@gmail.com> wrote in message
news:1142199015.476842.320290@i40g2000cwc.googlegr oups.com...[color=blue]
> define('key', 'foobar', false);
> $a = array('key'=>'value');
> echo "$a[key]<br>";
> echo "{$a[key]}<br>";
> echo "{$a['key']}<br>";
>
> output:
> value
> Errno: 8, Undefined index: foobar
> value
>
> It would seem constants are only expanded if they're in {}, and are
> treated like strings otherwise. I wonder when this was changed, or if
> its unintentional?
>[/color]
Interesting enough that in all actuality, you don't need to use curly
braces unless you are dealing with multi-dimensional arrays...
Norm
--
FREE Avatar hosting at www.easyavatar.com | 
March 12th, 2006, 11:05 PM
| | | | re: $request problem
Or back in the day when PHP gave E_NOTICE errors and you cared ;) | 
March 13th, 2006, 01:25 AM
| | | | re: $request problem
Anonymous in news:441326E4.66903A5B@nowhere.invalid wrote:
[color=blue]
> ColdShine wrote:
>[color=green]
>> There's no clear example stating this, but:
>>
>> echo "$array[key]";
>>
>> Is EXACTLY the same as:
>>
>> echo "{$array['key']}";[/color]
>
> No, Jerry is right, they are not the same.
>
> $array[key] is an array with a constant called key as index,
> $array['key'] is an array with the string 'key' as index.[/color]
Please read my post twice. I'm not talking about using $arr[key], but I'm
talking 'bout using "$arr[key]". If you can't understand the difference, you
should not be correcting me.
[color=blue]
> You will see the difference as soon as you assign a value to the
> constant key. :-)[/color]
No way. The constant called key is ONLY used when referring to $arr[key]
from OUTSIDE a doublequote'd string, or when using curly syntax.
[color=blue]
> When using $array[key] PHP will try to find the constant key, but
> intelligently will assume you actually meant 'key' when it figures out
> that the constant is undefined. Here is the error from the log:
>
> [Sat Mar 11 19:29:40 2006] [error] [client 127.0.0.1] PHP Notice: Use
> of undefined constant key - assumed 'key' in C:\\webroot\\test.php on
> line 4
>
> The PHP manual says on this topic:
>
>
> Note: Enabling E_NOTICE during development has some benefits. For
> debugging purposes: NOTICE messages will warn you about possible bugs in
> your code. For example, use of unassigned values is warned. It is
> extremely useful to find typos and to save time for debugging. NOTICE
> messages will warn you about bad style. For example, $arr[item] is
> better to be written as $arr['item'] since PHP tries to treat "item" as
> constant. If it is not a constant, PHP assumes it is a string index for
> the array.[/color]
Both you and the PHP manual are STILL talking about $arr[item] being used
OUTSIDE a complex (doublequote'd) string (or again, with curly syntax).
[color=blue]
> However, strangely enough this warning showed up in the log only the
> first time I accessed the page! No more warnings on any subsequent
> accesses to the page or any other page with that kind of error!
>
> I just checked the config, ignore_repeated_errors and
> ignore_repeated_source are set to off. I also confirmed these settings
> with phpinfo(). However, PHP reacts as if both are set to on! Did I just
> find a bug?
>
> That would explain why you get no warnings! You probably got your first
> and only warning a long time ago if you always program like that. ;-)[/color]
If you write $arr[key] you get an E_NOTICE unless there's a constant named
'key'.
If you write "$arr[key]" you get an E_NOTICE unless 'key' is a defined index
in $arr.
I don't think there's anything complex to understand here. And no, I didn't
turn on such an idiot-friendly option as ignoring
warnings/errors/whatsoever.
Here's a quick roundup, if you (or anyone f'wing this thread) still are
wondering WHAT is meant WHEN:
<?php
$arr = array('key' => 'foo');
define('key', 'another');
echo $arr[key]; // undefined index ('another')
echo $arr['key']; // foo
echo '$arr[key]'; // $arr[key]
echo "$arr[key]"; // foo
echo "$arr['key']"; // parse error
echo "{$arr[key]}"; // undefined index ('another')
echo "{$arr['key']}"; // foo
?>
This should rule out any misunderstandings.
Btw,
<?php
$arr = array('c' => 'Cheers', 'f' => 'F**k off');
define('c', 'f');
echo "$arr[c] :-)";
?>
--
ColdShine
"Experience is a hard teacher: she gives the test first, the lesson
afterwards." - Vernon Sanders law | 
March 14th, 2006, 01:25 PM
| | | | re: $request problem
ColdShine wrote:
<snip>
I liked this discussion, and it cleared a few thing up I never understood,
and thus simply avoided in all my code.
Your examples are a nice summary. :-)
I couldn't resist myself to give some comment.
[color=blue]
>
> Here's a quick roundup, if you (or anyone f'wing this thread) still are
> wondering WHAT is meant WHEN:
>
> <?php
> $arr = array('key' => 'foo');
> define('key', 'another');
>
> echo $arr[key]; // undefined index ('another')
> echo $arr['key']; // foo
> echo '$arr[key]'; // $arr[key]
> echo "$arr[key]"; // foo[/color]
Personally I think that it is a big mistake of PHP to let this construct
slip. :-/
Why try to be nice and 'correct' this internally to
$arr["key"] ??
Why not to defined key?
Sounds like a great source of bugs to me.
Why oh why not just produce an error?
These constructs are excactly why I walked away from Perl some years ago.
;-)
Another thing: Try to be nice to yourself and fellowprogrammers, and just
jump out of the string and concatenate what you need.
What's wrong with:
$result = "bla".$arr[key]." and of course: ".$arr["key"];
Why obfuscate code with complex parameters inside (double) quotes?
What is the point? Outsmart fellow programmers? If so: go Perl. ;-)
I think it is just unneccessary, and I never came across a situation where
it is actually needed...
Well, just my 2 cents.
And thanks for the examples.
I finally got it, but will surely never use it. :-)
Regards,
Erwin Moller
[color=blue]
> echo "$arr['key']"; // parse error
> echo "{$arr[key]}"; // undefined index ('another')
> echo "{$arr['key']}"; // foo
> ?>
>
> This should rule out any misunderstandings.
>
> Btw,
> <?php
> $arr = array('c' => 'Cheers', 'f' => 'F**k off');
> define('c', 'f');
>
> echo "$arr[c] :-)";
> ?>[/color] | 
March 14th, 2006, 02:15 PM
| | | | re: $request problem
ColdShine wrote:
[color=blue]
> Here's a quick roundup, if you (or anyone f'wing this thread) still are
> wondering WHAT is meant WHEN:
>
> <?php
> $arr = array('key' => 'foo');
> define('key', 'another');
>
> echo $arr[key]; // undefined index ('another')
> echo $arr['key']; // foo
> echo '$arr[key]'; // $arr[key][/color]
Ok, these results are obvious, IMHO.
[color=blue]
> echo "$arr[key]"; // foo
> echo "$arr['key']"; // parse error[/color]
But these two are not. For the first case: Where in the manual does it
state that "$arr[key]" should be evaluated like $arr["key"]? For the
second: Within double quotes variables should be evaluated, so why
doesn't $arr['key'] evaluate to foo?
[color=blue]
> echo "{$arr[key]}"; // undefined index ('another')
> echo "{$arr['key']}"; // foo
> ?>[/color]
Jep, these are obvious, too.
[color=blue]
>
> This should rule out any misunderstandings.[/color]
Yes, it did. But it also brought up two new questions on my part. :-) | 
March 14th, 2006, 10:05 PM
| | | | re: $request problem
Anonymous in news:4416CEBE.92F38532@nowhere.invalid wrote:
[color=blue]
> ColdShine wrote:
>[color=green]
>> Here's a quick roundup, if you (or anyone f'wing this thread) still are
>> wondering WHAT is meant WHEN:
>>
>> <?php
>> $arr = array('key' => 'foo');
>> define('key', 'another');
>>
>> echo $arr[key]; // undefined index ('another')
>> echo $arr['key']; // foo
>> echo '$arr[key]'; // $arr[key][/color]
>
> Ok, these results are obvious, IMHO.[/color]
I included those for the sake of completeness...
[color=blue][color=green]
>> echo "$arr[key]"; // foo
>> echo "$arr['key']"; // parse error[/color]
>
> But these two are not. For the first case: Where in the manual does it
> state that "$arr[key]" should be evaluated like $arr["key"]? For the
> second: Within double quotes variables should be evaluated, so why
> doesn't $arr['key'] evaluate to foo?[/color]
This answers your questions: http://www.php.net/manual/en/languag...ng.php#AEN3054
If you could not find this in the manual it's your fault, as it's just
where... you should expect it to be :)
[color=blue][color=green]
>> This should rule out any misunderstandings.[/color]
>
> Yes, it did. But it also brought up two new questions on my part. :-)[/color]
....and anyone else's who's not read the manual of course.
--
ColdShine
"Experience is a hard teacher: she gives the test first, the lesson
afterwards." - Vernon Sanders law | 
March 14th, 2006, 10:25 PM
| | | | re: $request problem
Erwin Moller in news:4416c095$0$11067$e4fe514c@news.xs4all.nl wrote:
[color=blue]
> I liked this discussion, and it cleared a few thing up I never understood,
> and thus simply avoided in all my code.
> Your examples are a nice summary. :-)
>
> Personally I think that it is a big mistake of PHP to let this construct
> slip. :-/
> Why try to be nice and 'correct' this internally to
> $arr["key"] ??[/color]
That's not a "correction", it's just... its behavior :)
[color=blue]
> Why not to defined key?
> Sounds like a great source of bugs to me.
>
> Why oh why not just produce an error?
>
> Another thing: Try to be nice to yourself and fellowprogrammers, and just
> jump out of the string and concatenate what you need.
>
> What's wrong with:
> $result = "bla".$arr[key]." and of course: ".$arr["key"];
>
> Why obfuscate code with complex parameters inside (double) quotes?
> What is the point? Outsmart fellow programmers? If so: go Perl. ;-)
>
> I think it is just unneccessary, and I never came across a situation where
> it is actually needed...
>
> Well, just my 2 cents.
> And thanks for the examples.
> I finally got it, but will surely never use it. :-)[/color]
You're welcome, but... why stick to not using a language feature?
Look at this multiple string concatenation:
$s = $a['first'] . '#' . $a['second'] . '(' . $a['third'] . ')';
Becomes:
$s = "$a[first]#$a[second]($a[third])";
I found the latter syntax to be much clearer to read. And I suspect it's
faster to execute too, since it probably saves some string concatenations
(thus being less memory-intensive).
And, as this syntax is clearly a short-hand to the more common "standard"
syntax, why should constants be preferred over string keys, with string keys
being much more common than constant keys?
You can find the official samples here, if you like: http://www.php.net/manual/en/languag...ng.php#AEN3054
If there's few people extensively using this syntax, it maybe due to the
page it's documented into being too lengthy...
--
ColdShine
"Experience is a hard teacher: she gives the test first, the lesson
afterwards." - Vernon Sanders law | 
March 15th, 2006, 09:25 AM
| | | | re: $request problem
ColdShine wrote:
[color=blue]
> Erwin Moller in news:4416c095$0$11067$e4fe514c@news.xs4all.nl wrote:
>[color=green]
>> I liked this discussion, and it cleared a few thing up I never
>> understood, and thus simply avoided in all my code.
>> Your examples are a nice summary. :-)
>>
>> Personally I think that it is a big mistake of PHP to let this construct
>> slip. :-/
>> Why try to be nice and 'correct' this internally to
>> $arr["key"] ??[/color]
>
> That's not a "correction", it's just... its behavior :)[/color]
Well... yes.
LOL. :-)
Reminds me of that old M$ joke:
Customer: "Dear Miscrosoft, I found again a bug in your software."
Microsoft: "No sir, sorry, that is not a bug, it is an undocumented
feature."
[color=blue][color=green]
>>
>> Why obfuscate code with complex parameters inside (double) quotes?
>> What is the point? Outsmart fellow programmers? If so: go Perl. ;-)
>>
>> I think it is just unneccessary, and I never came across a situation
>> where it is actually needed...
>>
>> Well, just my 2 cents.
>> And thanks for the examples.
>> I finally got it, but will surely never use it. :-)[/color]
>
> You're welcome, but... why stick to not using a language feature?
>
> Look at this multiple string concatenation:
> $s = $a['first'] . '#' . $a['second'] . '(' . $a['third'] . ')';
> Becomes:
> $s = "$a[first]#$a[second]($a[third])";
>
> I found the latter syntax to be much clearer to read. And I suspect it's
> faster to execute too, since it probably saves some string concatenations
> (thus being less memory-intensive).
> And, as this syntax is clearly a short-hand to the more common "standard"
> syntax, why should constants be preferred over string keys, with string
> keys being much more common than constant keys?[/color]
erm... I am such an oldfashioned guy that rather writes a few extra lines so
it is completely clear what happens instead of a 'feature-guy' who tries to
keep things short and rely on less-well-known behaviour.
When I see:
echo "$arr[key.$another['key2']]";
I need a toilet first, and then I must THINK what it means, whereas the
longer notation is always clear.
But you are right: It is probably a little faster and a little memoryhungry,
but I always prefer clarity over speed and performance.
But hey: That is just me: I am absolutely not claiming my way is better. :-)
Like I said: puzzle-constructs in code were the main reason I walked away
from Perl.
Of course lots of people write readable Perl, but most dont. :P
At least not for a mere mortal like me.
Anyway, thanks for your examples.
Regards,
Erwin
[color=blue]
>
> You can find the official samples here, if you like:
> http://www.php.net/manual/en/languag...ng.php#AEN3054
>
> If there's few people extensively using this syntax, it maybe due to the
> page it's documented into being too lengthy...
>[/color] | 
March 15th, 2006, 05:45 PM
| | | | re: $request problem
ColdShine wrote:
[color=blue][color=green][color=darkred]
> >> echo "$arr[key]"; // foo
> >> echo "$arr['key']"; // parse error[/color]
> >
> > But these two are not. For the first case: Where in the manual does it
> > state that "$arr[key]" should be evaluated like $arr["key"]? For the
> > second: Within double quotes variables should be evaluated, so why
> > doesn't $arr['key'] evaluate to foo?[/color]
>
> This answers your questions:
> http://www.php.net/manual/en/languag...ng.php#AEN3054
>
> If you could not find this in the manual it's your fault, as it's just
> where... you should expect it to be :)[/color]
*LOL*, I swear, that wasn't there the last time I read that section! ;-)
But seriously, was that always in there or is that some recent addition?
It might be some time ago when I last read about string handling,
because I never had any problems with that. However, I usually don't use
less known constructions like these that don't make immediate sense,
because I still want to be able to understand my own code in a year.
But the bottom line for this thread is: You were right, it *is*
documented behaviour.
Bye! | 
March 15th, 2006, 06:55 PM
| | | | re: $request problem
The note here: http://us2.php.net/manual/en/ref.errorfunc.php
still says
echo "$arr[foo]"
will give an E_NOTICE (it doesn't say if it means within the context of
a double quoted string or not, but past history would imply it does, as
I specifcally remember seeing E_NOTICE errors for this when I
originally began learning php. I guess it's context has changed, it'd
be nice if that was explicit considering its history), however then
link you gave, http://www.php.net/manual/en/languag...ng.php#AEN3054 ,
doesn't mention that, so i guess the $arr[foo] not giving an E_NOTICE
is an intended change and is a problem with the documentation. (or its
unintended and the documentation is wrong...haha)
Aside: That page still says using {} for string offsets is preferred,
however, I remember reading php meeting notes that {} was going to be
deprecated (removed in php 6) and [] was going to be the preferred
method. http://www.php.net/~derick/meeting-n...cleanup-for-vs
Manual contradictions can be fun! :)
Speed of executation: a friend of mine ran some tests comparing $arr,
$arr["index"], $arr[index], $arr[$index], $arr['index'], numerical
indices vs string indices, with and without {}. It was a few years ago
(php4) and was just an informal test to see what would happen. Results
may have changed, but anyways:
In general, using {} was faster than not using {}, ({$foo} vs $foo).
Numerical indices ($arr[0], not $arr['0'] or $arr["0"]) were faster
than string indices.
Single quotes and variables ($arr['index'] vs $arr[$index]) were about
the same.
Single quotes were faster than double quotes, ($arr['index'] vs
$arr["index"], echo 'foo' vs echo "foo", etc)
I would guess $var[index] is as fast as using single quotes; they're
both constant literals.
Of course, things may have changed since PHP5 since then.
Style of writing out strings: In general I prefer always using curly
braces, {}, around variables within strings to help distinguish them.
Wen I concatenate, i put a space on both sides of the dot operator,
starting the new line with the dot op if its a long line. Plus using
{} avoid the whole E_NOTICE discussion :p
Woo, time for my final. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|