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


Groups > comp.lang.php > #6335

Re: Using count() as an array index

From Gregor Kofler <usenet@gregorkofler.com>
Newsgroups comp.lang.php
Subject Re: Using count() as an array index
Date 2012-06-15 21:04 +0200
Organization A noiseless patient Spider
Message-ID <jrg10a$qib$1@dont-email.me> (permalink)
References <jrg08j$ldn$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web