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
Switch to threaded view of this topic Create a new topic Submit Reply
Using count() as an array index [message #178410] Fri, 15 June 2012 18:51 Go to next message
Martin Leese is currently offline  Martin Leese
Messages: 23
Registered: June 2012
Karma: 0
Junior Member
I am using PHP version 4.2.3. This is old,
but I cannot upgrade.

I came across a strange limitation which is
puzzling me. The following test program:

<?php
// This PHP file tests the use of count() as an array index
$anArray[1] = "This is element one";
$anArray[2] = "This is element two";
$anArray[3] = "This is the last element";
//
echo "$anArray[count($anArray)]\n";
?>

produces the error message:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting ']' in CountTest.php on line 7

Why is using count() as an array index not
allowed?

This is no big deal as the work around is
trivial; just replace the last line with:

$lastIndex = count($anArray);
echo "$anArray[$lastIndex]\n";

However, it is puzzling.

Many thanks,
Martin
--
Regards,
Martin Leese
E-mail: please(at)see(dot)Web(dot)for(dot)e-mail(dot)INVALID
Web: http://members.tripod.com/martin_leese/
Re: Using count() as an array index [message #178411 is a reply to message #178410] Fri, 15 June 2012 19:04 Go to previous messageGo to next message
Gregor Kofler is currently offline  Gregor Kofler
Messages: 69
Registered: September 2010
Karma: 0
Member
Am 2012-06-15 20:51, Martin Leese meinte:
> I am using PHP version 4.2.3. This is old,
> but I cannot upgrade.
>
> I came across a strange limitation which is
> puzzling me. The following test program:
>
> <?php
> // This PHP file tests the use of count() as an array index
> $anArray[1] = "This is element one";
> $anArray[2] = "This is element two";
> $anArray[3] = "This is the last element";
> //
> echo "$anArray[count($anArray)]\n";
> ?>
>
> produces the error message:
> Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE,
> expecting ']' in CountTest.php on line 7
>
> Why is using count() as an array index not
> allowed?

That's not what the error message says.


> This is no big deal as the work around is
> trivial; just replace the last line with:
>
> $lastIndex = count($anArray);
> echo "$anArray[$lastIndex]\n";

Which produces a notice "Undefined offset: ..."

> However, it is puzzling.

No. Read about "interpolation" in PHP. (As a hint: PHP can't isolate the
function name.)

Gregor


--
http://vxweb.net
Re: Using count() as an array index [message #178415 is a reply to message #178410] Fri, 15 June 2012 21:38 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Fri, 15 Jun 2012 12:51:59 -0600, Martin Leese wrote:

> I am using PHP version 4.2.3. This is old, but I cannot upgrade.
>
> I came across a strange limitation which is puzzling me. The following
> test program:
>
> <?php
> // This PHP file tests the use of count() as an array index
> $anArray[1] = "This is element one";
> $anArray[2] = "This is element two";
> $anArray[3] = "This is the last element";
> //
> echo "$anArray[count($anArray)]\n";
> ?>
>
> produces the error message:
> Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE,
> expecting ']' in CountTest.php on line 7

> Why is using count() as an array index not allowed?

What's not allowed is using a function call as part of a variable
translation inside a string.

> This is no big deal as the work around is trivial; just replace the last
> line with:
>
> $lastIndex = count($anArray);
> echo "$anArray[$lastIndex]\n";

Note that by default, array indexes start at 0.

<?php
$arr = array("first","middle","last");
$len = count($arr);
echo "element {$len} is {$arr[$len]}\n";
?>

Generates output:

============
PHP Notice: Undefined offset: 3 in /home/denis/programming/php/
arrcountfail.php on line 4
element 3 is
============

Rgds

Denis McMahon
Re: Using count() as an array index [message #178416 is a reply to message #178410] Fri, 15 June 2012 21:44 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
Martin Leese wrote:
> I am using PHP version 4.2.3. This is old,
> but I cannot upgrade.
>
> I came across a strange limitation which is
> puzzling me. The following test program:
>
> <?php
> // This PHP file tests the use of count() as an array index
> $anArray[1] = "This is element one";
> $anArray[2] = "This is element two";
> $anArray[3] = "This is the last element";
> //
> echo "$anArray[count($anArray)]\n";
> ?>
>
> produces the error message:
> Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE,
> expecting ']' in CountTest.php on line 7
>
> Why is using count() as an array index not
> allowed?
>
> This is no big deal as the work around is
> trivial; just replace the last line with:
>
> $lastIndex = count($anArray);
> echo "$anArray[$lastIndex]\n";
>
> However, it is puzzling.

You can not use a function call within a quoted string.

You should use:

echo $anArray[count($array)] . "\n";

[Note: in "standard" arrays (indexing starts at 0), count($some_array)
will be an undefined index.]

--
*****************************
Chuck Anderson • Boulder, CO
http://cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
Re: Using count() as an array index [message #178420 is a reply to message #178416] Sat, 16 June 2012 17:56 Go to previous messageGo to next message
Martin Leese is currently offline  Martin Leese
Messages: 23
Registered: June 2012
Karma: 0
Junior Member
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.

--
Regards,
Martin Leese
E-mail: please(at)see(dot)Web(dot)for(dot)e-mail(dot)INVALID
Web: http://members.tripod.com/martin_leese/
Re: Using count() as an array index [message #178422 is a reply to message #178420] Sat, 16 June 2012 18:44 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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).

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

You need to get used to this idea; it's used throughout PHP.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Using count() as an array index [message #178425 is a reply to message #178422] Sat, 16 June 2012 21:43 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 6/16/2012 11:44 AM, Jerry Stuckle wrote:
> 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).
>
> 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).
>
> You need to get used to this idea; it's used throughout PHP.
>
Oh how I have come to love those curly braces.. :)
Re: Using count() as an array index [message #178429 is a reply to message #178422] Sun, 17 June 2012 13:57 Go to previous messageGo to next message
Martin Leese is currently offline  Martin Leese
Messages: 23
Registered: June 2012
Karma: 0
Junior Member
Jerry Stuckle wrote:
....
> 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).
>
> You need to get used to this idea; it's used throughout PHP.

The test code I posted was taken from my
application in which I deliberately start my
array with [1]. This is because the array
holds numbered sections of an FAQ, and it
makes more sense to have Section 1 in array
element [1], etc.

--
Regards,
Martin Leese
E-mail: please(at)see(dot)Web(dot)for(dot)e-mail(dot)INVALID
Web: http://members.tripod.com/martin_leese/
Re: Using count() as an array index [message #178430 is a reply to message #178429] Sun, 17 June 2012 14:27 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/17/2012 9:57 AM, Martin Leese wrote:
> Jerry Stuckle wrote:
> ...
>> 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).
>>
>> You need to get used to this idea; it's used throughout PHP.
>
> The test code I posted was taken from my
> application in which I deliberately start my
> array with [1]. This is because the array
> holds numbered sections of an FAQ, and it
> makes more sense to have Section 1 in array
> element [1], etc.
>

Actually, I think it's a bad way to do things. What happens if you want
to add a new section - or delete one? You'll have to change your code,
which should never be the case.

A better way is to hold the information in a database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Using count() as an array index [message #178434 is a reply to message #178422] Mon, 18 June 2012 07:46 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
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.

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


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Using count() as an array index [message #178436 is a reply to message #178434] Mon, 18 June 2012 10:32 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
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.



--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Re: Using count() as an array index [message #178437 is a reply to message #178436] Mon, 18 June 2012 11:24 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
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.
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 next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
Re: Using count() as an array index [message #178440 is a reply to message #178430] Mon, 18 June 2012 14:46 Go to previous messageGo to next message
Martin Leese is currently offline  Martin Leese
Messages: 23
Registered: June 2012
Karma: 0
Junior Member
Jerry Stuckle wrote:
> On 6/17/2012 9:57 AM, Martin Leese wrote:
>> Jerry Stuckle wrote:
>> ...
>>> 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).
>>>
>>> You need to get used to this idea; it's used throughout PHP.
>>
>> The test code I posted was taken from my
>> application in which I deliberately start my
>> array with [1]. This is because the array
>> holds numbered sections of an FAQ, and it
>> makes more sense to have Section 1 in array
>> element [1], etc.
>
> Actually, I think it's a bad way to do things. What happens if you want
> to add a new section - or delete one? You'll have to change your code,
> which should never be the case.
>
> A better way is to hold the information in a database.

No, I will not need to change my code. I
am using an array as a replacement for a
database. Here is a snippet of my file
faqData.php to give you the idea:

// Set data to identify Web page, and set prev and next anchors
// Note that the first set must have index explicitly set to [1]
$tempIndex = 1;
$dataHref[$tempIndex] = "Ambisonic/faq_section01.html";
$dataAnchorText[$tempIndex] = "1. Where can I get this FAQ?";
$dataCompleteText[$tempIndex] = "1. <a href=\"#SECTION1\">Where can
I get this FAQ?</a>";
$dataSplitText[$tempIndex] = "1. <a rel=\"section\"
href=\"$dataHref[$tempIndex]\">Where can I get this FAQ?</a>";

$tempIndex++;
$dataHref[$tempIndex] = "Ambisonic/faq_section02.html";
$dataAnchorText[$tempIndex] = "2. Corrections to the FAQ";
$dataCompleteText[$tempIndex] = "2. <a
href=\"#SECTION2\">Corrections to the FAQ</a>";
$dataSplitText[$tempIndex] = "2. <a rel=\"section\"
href=\"$dataHref[$tempIndex]\">Corrections to the FAQ</a>";

--
Regards,
Martin Leese
E-mail: please(at)see(dot)Web(dot)for(dot)e-mail(dot)INVALID
Web: http://members.tripod.com/martin_leese/
Re: Using count() as an array index [message #178442 is a reply to message #178440] Mon, 18 June 2012 17:38 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/18/2012 10:46 AM, Martin Leese wrote:
> Jerry Stuckle wrote:
>> On 6/17/2012 9:57 AM, Martin Leese wrote:
>>> Jerry Stuckle wrote:
>>> ...
>>>> 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).
>>>>
>>>> You need to get used to this idea; it's used throughout PHP.
>>>
>>> The test code I posted was taken from my
>>> application in which I deliberately start my
>>> array with [1]. This is because the array
>>> holds numbered sections of an FAQ, and it
>>> makes more sense to have Section 1 in array
>>> element [1], etc.
>>
>> Actually, I think it's a bad way to do things. What happens if you
>> want to add a new section - or delete one? You'll have to change your
>> code, which should never be the case.
>>
>> A better way is to hold the information in a database.
>
> No, I will not need to change my code. I
> am using an array as a replacement for a
> database. Here is a snippet of my file
> faqData.php to give you the idea:
>
> // Set data to identify Web page, and set prev and next anchors
> // Note that the first set must have index explicitly set to [1]
> $tempIndex = 1;
> $dataHref[$tempIndex] = "Ambisonic/faq_section01.html";
> $dataAnchorText[$tempIndex] = "1. Where can I get this FAQ?";
> $dataCompleteText[$tempIndex] = "1. <a href=\"#SECTION1\">Where can I
> get this FAQ?</a>";
> $dataSplitText[$tempIndex] = "1. <a rel=\"section\"
> href=\"$dataHref[$tempIndex]\">Where can I get this FAQ?</a>";
>
> $tempIndex++;
> $dataHref[$tempIndex] = "Ambisonic/faq_section02.html";
> $dataAnchorText[$tempIndex] = "2. Corrections to the FAQ";
> $dataCompleteText[$tempIndex] = "2. <a href=\"#SECTION2\">Corrections to
> the FAQ</a>";
> $dataSplitText[$tempIndex] = "2. <a rel=\"section\"
> href=\"$dataHref[$tempIndex]\">Corrections to the FAQ</a>";
>

Yes, which means if you need to change your FAQs you need to change this
code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Using count() as an array index [message #178444 is a reply to message #178436] Mon, 18 June 2012 17:43 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Norman Peelman, 18.06.2012 12:32:

> On 06/18/2012 03:46 AM, Arno Welzel wrote:
>> Jerry Stuckle, 16.06.2012 20:44:
[...]
>> "{$anArray[count($anArray)]}\n";
[...]
>> 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

It is.

The form

"text {...something...} text"

is generally the same as

'text' . something . 'text'


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

So:

echo 'This is the result: '.$myclass->result;

Is longer than

echo "This is the result: {$myclass->result}";

And why do i have to escape '/'? Seems have missed something ;-)



--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Using count() as an array index [message #178456 is a reply to message #178444] Tue, 19 June 2012 07:31 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 18.06.2012 19:43, schrieb Arno Welzel:
> Norman Peelman, 18.06.2012 12:32:
>
>> On 06/18/2012 03:46 AM, Arno Welzel wrote:
>>> Jerry Stuckle, 16.06.2012 20:44:
> [...]
>>> "{$anArray[count($anArray)]}\n";
> [...]
>>> 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
>
> It is.
>
> The form
>
> "text {...something...} text"
>
> is generally the same as
>
> 'text' . something . 'text'
>
>
>> 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.
>
> So:
>
> echo 'This is the result: '.$myclass->result;
>
> Is longer than
>
> echo "This is the result: {$myclass->result}";
>
> And why do i have to escape '/'? Seems have missed something ;-)

php > $c = new stdClass;
php > $c->result = 'NONE';
php > echo "this is the result: $c->result";
this is the result: NONE
php >

What are you arguing about? The braces have their uses, but you should know when they
are needed.

Maybe we should start an "obfuscated PHP contest" to mark the limits of code readability.

/Str.
Re: Using count() as an array index [message #178458 is a reply to message #178422] Tue, 19 June 2012 11:24 Go to previous messageGo to next message
Christoph Becker is currently offline  Christoph Becker
Messages: 91
Registered: June 2012
Karma: 0
Member
Jerry Stuckle wrote:
>
> 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.

AFAIK this feature is avaible not until PHP5, but the OP asked about PHP
4.2.3.

KR,
Christoph M. Becker
Re: Using count() as an array index [message #178459 is a reply to message #178458] Tue, 19 June 2012 11:39 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/19/2012 7:24 AM, Christoph Becker wrote:
> Jerry Stuckle wrote:
>>
>> 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.
>
> AFAIK this feature is avaible not until PHP5, but the OP asked about PHP
> 4.2.3.
>
> KR,
> Christoph M. Becker
>

It was available well before PHP5.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Using count() as an array index [message #178461 is a reply to message #178459] Tue, 19 June 2012 13:04 Go to previous messageGo to next message
Christoph Becker is currently offline  Christoph Becker
Messages: 91
Registered: June 2012
Karma: 0
Member
Jerry Stuckle wrote:

> It was available well before PHP5.

Thanks for pointing this out!

KR,
Christoph M. Becker
Re: Using count() as an array index [message #178464 is a reply to message #178442] Tue, 19 June 2012 15:03 Go to previous messageGo to next message
Martin Leese is currently offline  Martin Leese
Messages: 23
Registered: June 2012
Karma: 0
Junior Member
Jerry Stuckle wrote:
> On 6/18/2012 10:46 AM, Martin Leese wrote:
>> Jerry Stuckle wrote:
....
>>> A better way is to hold the information in a database.
>>
>> No, I will not need to change my code. I
>> am using an array as a replacement for a
>> database. Here is a snippet of my file
>> faqData.php to give you the idea:
>>
>> // Set data to identify Web page, and set prev and next anchors
>> // Note that the first set must have index explicitly set to [1]
>> $tempIndex = 1;
>> $dataHref[$tempIndex] = "Ambisonic/faq_section01.html";
>> $dataAnchorText[$tempIndex] = "1. Where can I get this FAQ?";
>> $dataCompleteText[$tempIndex] = "1. <a href=\"#SECTION1\">Where can I
>> get this FAQ?</a>";
>> $dataSplitText[$tempIndex] = "1. <a rel=\"section\"
>> href=\"$dataHref[$tempIndex]\">Where can I get this FAQ?</a>";
>>
>> $tempIndex++;
>> $dataHref[$tempIndex] = "Ambisonic/faq_section02.html";
>> $dataAnchorText[$tempIndex] = "2. Corrections to the FAQ";
>> $dataCompleteText[$tempIndex] = "2. <a href=\"#SECTION2\">Corrections to
>> the FAQ</a>";
>> $dataSplitText[$tempIndex] = "2. <a rel=\"section\"
>> href=\"$dataHref[$tempIndex]\">Corrections to the FAQ</a>";
>
> Yes, which means if you need to change your FAQs you need to change this
> code.

This code is my database. Yes, if I change
the FAQ I will need to change the database.

--
Regards,
Martin Leese
E-mail: please(at)see(dot)Web(dot)for(dot)e-mail(dot)INVALID
Web: http://members.tripod.com/martin_leese/
Re: Using count() as an array index [message #178465 is a reply to message #178458] Tue, 19 June 2012 15:14 Go to previous messageGo to next message
Martin Leese is currently offline  Martin Leese
Messages: 23
Registered: June 2012
Karma: 0
Junior Member
Christoph Becker wrote:
> Jerry Stuckle wrote:
>>
>> 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.
>
> AFAIK this feature is avaible not until PHP5, but the OP asked about PHP
> 4.2.3.

This works in 4.2.3, although I have used
Chuck's suggestion as it looks more readable
to my eyes.

--
Regards,
Martin Leese
E-mail: please(at)see(dot)Web(dot)for(dot)e-mail(dot)INVALID
Web: http://members.tripod.com/martin_leese/
Re: Using count() as an array index [message #178466 is a reply to message #178464] Tue, 19 June 2012 15:27 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/19/2012 11:03 AM, Martin Leese wrote:
> Jerry Stuckle wrote:
>> On 6/18/2012 10:46 AM, Martin Leese wrote:
>>> Jerry Stuckle wrote:
> ...
>>>> A better way is to hold the information in a database.
>>>
>>> No, I will not need to change my code. I
>>> am using an array as a replacement for a
>>> database. Here is a snippet of my file
>>> faqData.php to give you the idea:
>>>
>>> // Set data to identify Web page, and set prev and next anchors
>>> // Note that the first set must have index explicitly set to [1]
>>> $tempIndex = 1;
>>> $dataHref[$tempIndex] = "Ambisonic/faq_section01.html";
>>> $dataAnchorText[$tempIndex] = "1. Where can I get this FAQ?";
>>> $dataCompleteText[$tempIndex] = "1. <a href=\"#SECTION1\">Where can I
>>> get this FAQ?</a>";
>>> $dataSplitText[$tempIndex] = "1. <a rel=\"section\"
>>> href=\"$dataHref[$tempIndex]\">Where can I get this FAQ?</a>";
>>>
>>> $tempIndex++;
>>> $dataHref[$tempIndex] = "Ambisonic/faq_section02.html";
>>> $dataAnchorText[$tempIndex] = "2. Corrections to the FAQ";
>>> $dataCompleteText[$tempIndex] = "2. <a href=\"#SECTION2\">Corrections to
>>> the FAQ</a>";
>>> $dataSplitText[$tempIndex] = "2. <a rel=\"section\"
>>> href=\"$dataHref[$tempIndex]\">Corrections to the FAQ</a>";
>>
>> Yes, which means if you need to change your FAQs you need to change
>> this code.
>
> This code is my database. Yes, if I change
> the FAQ I will need to change the database.
>

This is not a database. It is a PHP script.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Wed Nov 27 22:37:31 GMT 2024

Total time taken to generate the page: 0.02940 seconds