Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6334 > unrolled thread

Using count() as an array index

Started byMartin Leese <please@see.Web.for.e-mail.INVALID>
First post2012-06-15 12:51 -0600
Last post2012-06-19 09:14 -0600
Articles 20 on this page of 23 — 11 participants

Back to article view | Back to comp.lang.php


Contents

  Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-15 12:51 -0600
    Re: Using count() as an array index Gregor Kofler <usenet@gregorkofler.com> - 2012-06-15 21:04 +0200
    Re: Using count() as an array index Denis McMahon <denismfmcmahon@gmail.com> - 2012-06-15 21:38 +0000
    Re: Using count() as an array index Chuck Anderson <cycletourist@invalid.invalid> - 2012-06-15 15:44 -0600
      Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-16 11:56 -0600
        Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-16 14:44 -0400
          Re: Using count() as an array index Scott Johnson <noonehome@chalupasworld.com> - 2012-06-16 14:43 -0700
          Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-17 07:57 -0600
            Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-17 10:27 -0400
              Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-18 08:46 -0600
                Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-18 13:38 -0400
                  Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-19 09:03 -0600
                    Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-19 11:27 -0400
          Re: Using count() as an array index Arno Welzel <usenet@arnowelzel.de> - 2012-06-18 09:46 +0200
            Re: Using count() as an array index Norman Peelman <npeelmandog@cfl.rr.com> - 2012-06-18 06:32 -0400
              Re: Using count() as an array index The Natural Philosopher <tnp@invalid.invalid> - 2012-06-18 12:24 +0100
              Re: Using count() as an array index Arno Welzel <usenet@arnowelzel.de> - 2012-06-18 19:43 +0200
                Re: Using count() as an array index "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-06-19 09:31 +0200
            Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-18 08:43 -0400
          Re: Using count() as an array index Christoph Becker <cmbecker69@gmx.de> - 2012-06-19 13:24 +0200
            Re: Using count() as an array index Jerry Stuckle <jstucklex@attglobal.net> - 2012-06-19 07:39 -0400
              Re: Using count() as an array index Christoph Becker <cmbecker69@gmx.de> - 2012-06-19 15:04 +0200
            Re: Using count() as an array index Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-06-19 09:14 -0600

Page 1 of 2  [1] 2  Next page →


#6334 — Using count() as an array index

FromMartin Leese <please@see.Web.for.e-mail.INVALID>
Date2012-06-15 12:51 -0600
SubjectUsing count() as an array index
Message-ID<jrg08j$ldn$1@dont-email.me>
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@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

[toc] | [next] | [standalone]


#6335

FromGregor Kofler <usenet@gregorkofler.com>
Date2012-06-15 21:04 +0200
Message-ID<jrg10a$qib$1@dont-email.me>
In reply to#6334
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

[toc] | [prev] | [next] | [standalone]


#6339

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-06-15 21:38 +0000
Message-ID<jrga17$dtt$1@dont-email.me>
In reply to#6334
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

[toc] | [prev] | [next] | [standalone]


#6340

FromChuck Anderson <cycletourist@invalid.invalid>
Date2012-06-15 15:44 -0600
Message-ID<jrgab8$n9j$1@dont-email.me>
In reply to#6334
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
*****************************

[toc] | [prev] | [next] | [standalone]


#6344

FromMartin Leese <please@see.Web.for.e-mail.INVALID>
Date2012-06-16 11:56 -0600
Message-ID<jrihcn$7ht$1@dont-email.me>
In reply to#6340
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@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

[toc] | [prev] | [next] | [standalone]


#6346

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-06-16 14:44 -0400
Message-ID<jrik64$ovm$1@dont-email.me>
In reply to#6344
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6349

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-06-16 14:43 -0700
Message-ID<jrium4$p45$1@dont-email.me>
In reply to#6346
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.. :)

[toc] | [prev] | [next] | [standalone]


#6353

FromMartin Leese <please@see.Web.for.e-mail.INVALID>
Date2012-06-17 07:57 -0600
Message-ID<jrknos$cg2$1@dont-email.me>
In reply to#6346
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@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

[toc] | [prev] | [next] | [standalone]


#6354

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-06-17 10:27 -0400
Message-ID<jrkpgd$m1c$1@dont-email.me>
In reply to#6353
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6364

FromMartin Leese <please@see.Web.for.e-mail.INVALID>
Date2012-06-18 08:46 -0600
Message-ID<jrnf04$8hg$1@dont-email.me>
In reply to#6354
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@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

[toc] | [prev] | [next] | [standalone]


#6366

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-06-18 13:38 -0400
Message-ID<jrnp3m$9tr$1@dont-email.me>
In reply to#6364
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6388

FromMartin Leese <please@see.Web.for.e-mail.INVALID>
Date2012-06-19 09:03 -0600
Message-ID<jrq4cp$90d$1@dont-email.me>
In reply to#6366
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@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

[toc] | [prev] | [next] | [standalone]


#6390

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-06-19 11:27 -0400
Message-ID<jrq5op$ivu$1@dont-email.me>
In reply to#6388
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6358

FromArno Welzel <usenet@arnowelzel.de>
Date2012-06-18 09:46 +0200
Message-ID<4FDEDCC7.4010002@arnowelzel.de>
In reply to#6346
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

[toc] | [prev] | [next] | [standalone]


#6360

FromNorman Peelman <npeelmandog@cfl.rr.com>
Date2012-06-18 06:32 -0400
Message-ID<jrn04n$dt2$1@dont-email.me>
In reply to#6358
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?-

[toc] | [prev] | [next] | [standalone]


#6361

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2012-06-18 12:24 +0100
Message-ID<jrn364$e77$3@news.albasani.net>
In reply to#6360
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.

[toc] | [prev] | [next] | [standalone]


#6368

FromArno Welzel <usenet@arnowelzel.de>
Date2012-06-18 19:43 +0200
Message-ID<4FDF68DC.4050707@arnowelzel.de>
In reply to#6360
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

[toc] | [prev] | [next] | [standalone]


#6380

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-06-19 09:31 +0200
Message-ID<a4ao60FbetU1@mid.uni-berlin.de>
In reply to#6368
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.


[toc] | [prev] | [next] | [standalone]


#6362

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-06-18 08:43 -0400
Message-ID<jrn7pg$q1j$1@dont-email.me>
In reply to#6358
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6382

FromChristoph Becker <cmbecker69@gmx.de>
Date2012-06-19 13:24 +0200
Message-ID<jrpngj$1vu$1@speranza.aioe.org>
In reply to#6346
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

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.php


csiph-web