Re: Using count() as an array index [message #178437 is a reply to message #178436] |
Mon, 18 June 2012 11:24 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
Norman Peelman wrote:
> On 06/18/2012 03:46 AM, Arno Welzel wrote:
>> Jerry Stuckle, 16.06.2012 20:44:
>>
>>> On 6/16/2012 1:56 PM, Martin Leese wrote:
>>>> Chuck Anderson wrote:
>>>>
>>>> > You can not use a function call within a quoted string.
>>>> >
>>>> > You should use:
>>>> >
>>>> > echo $anArray[count($array)] . "\n";
>>>>
>>>> Thank you. I actually understood this
>>>> explanation.
>>>>
>>>
>>> Actually, you can easily use a function call within a quoted string, but
>>> since it is not a simple variable, you need to use curly braces, i.e.
>>>
>>> <?php
>>> $anArray[1] = "This is element one";
>>> $anArray[2] = "This is element two";
>>> $anArray[3] = "This is the last element";
>>> echo "{$anArray[count($anArray)]}\n";
>>> ?>
>>>
>>> prints "This is the last element" (without the quotes, of course).
>>
>> Please compare:
>>
>> $anArray[count($array)] . "\n";
>>
>> "{$anArray[count($anArray)]}\n";
>>
>> You see the difference? Your suggestion is longer (even whitespaces
>> which can me omitted) and totally unreadable for people you are not
>> familiar with this.
>>
>
> People that aren't familiar with this are mostly people that simply
> jump in to PHP without reading much documentation. Which is possible but
> people should end up reading at one of the PHP mirrors at some point. In
> this case:
>
> http://us.php.net/manual/en/language.types.string.php
>
> ...which explains all the different ways and reasons why.
>
>>> Be aware, though - the typical array in PHP starts counting at 0, not 1.
>>> So an array of 3 elements would be numbered 0, 1 and 2. In this case,
>>> an element with an index of 3 (count($anArray)) is not set (and will
>>> give a warning if you try to use it).
>>
>> ACK.
>>
>>> You need to get used to this idea; it's used throughout PHP.
>>
>> Just because something is possible does not mean you have to use it ;-)
>> Curly braces should only be used for code blocks and not to replace
>> string concatenation.
>>
>
> It's a little more than possible... it was designed this way. It's not
> a replacement for string concatenation, it is used (in this context) for
> variable expansion within strings. If you don't want to use curly brace
> syntax, then do it the long way, it's your choice. Me personally, I find
> curly brace syntax much more readable than alot of '.'s and '/' escapes
> like alot of people use.
>
>
>
being an old C hacker I simply use (s)printf, lots of %s's and stick all
the variables in a parameter list.
the nice thing about that is that - especially if using POST or GET
variables you can counter PHPs execrable lack of string typing using
'%d' and KNOW that whatever malformed crap is in the variables you will
get a number or nothing at all.
--
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.
|
|
|