FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Using count() as an array index
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Using count() as an array index [message #178438 is a reply to message #178434] Mon, 18 June 2012 12:43 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 6/18/2012 3: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.
>

Yup, I used an extra pair of braces. Just because YOU don't like it
doesn't mean it's a bad idea.

>> 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.
>
>

And just because YOU don't like it doesn't mean no one should use it.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: runkit
Next Topic: PHP mysql_excape but need to search for those items
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 01:20:59 GMT 2024

Total time taken to generate the page: 0.04443 seconds