Using count() as an array index

  • Follow


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/
0
Reply please32 (61) 6/15/2012 6:51:59 PM

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
0
Reply usenet2181 (202) 6/15/2012 7:04:42 PM


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
0
Reply denismfmcmahon (354) 6/15/2012 9:38:47 PM

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
*****************************
0
Reply cycletourist (65) 6/15/2012 9:44:00 PM

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/
0
Reply please32 (61) 6/16/2012 5:56:39 PM

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
==================
0
Reply jstucklex (14363) 6/16/2012 6:44:14 PM

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.. :)
0
Reply noonehome (135) 6/16/2012 9:43:09 PM

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/
0
Reply please32 (61) 6/17/2012 1:57:45 PM

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
==================
0
Reply jstucklex (14363) 6/17/2012 2:27:18 PM

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
0
Reply usenet3064 (194) 6/18/2012 7:46:15 AM

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?-
0
Reply npeelmandog (163) 6/18/2012 10:32:54 AM

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.
0
Reply tnp (2255) 6/18/2012 11:24:52 AM

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
==================
0
Reply jstucklex (14363) 6/18/2012 12:43:21 PM

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/
0
Reply please32 (61) 6/18/2012 2:46:25 PM

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
==================
0
Reply jstucklex (14363) 6/18/2012 5:38:55 PM

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
0
Reply usenet3064 (194) 6/18/2012 5:43:56 PM

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.



0
Reply sorry_no_mail_here (506) 6/19/2012 7:31:12 AM

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

0
Reply cmbecker69 (141) 6/19/2012 11:24:34 AM

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@attglobal.net
==================
0
Reply jstucklex (14363) 6/19/2012 11:39:07 AM

Jerry Stuckle wrote:

> It was available well before PHP5.

Thanks for pointing this out!

KR,
Christoph M. Becker
0
Reply cmbecker69 (141) 6/19/2012 1:04:05 PM

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/
0
Reply please32 (61) 6/19/2012 3:03:53 PM

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@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/
0
Reply please32 (61) 6/19/2012 3:14:49 PM

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
==================
0
Reply jstucklex (14363) 6/19/2012 3:27:14 PM

22 Replies
15 Views

(page loaded in 0.171 seconds)


Reply: