Array variable in echo statement [message #169782] |
Sun, 26 September 2010 18:24 |
MikeB
Messages: 65 Registered: September 2010
Karma: 0
|
Member |
|
|
I tried this and it didn't work as I was hoping:
foreach ($_COOKIE as $c) {
$ck = key($_COOKIE);
echo "<br /> Cookie: Key:\"{key($_COOKIE)}\" value:\"$c\"";
}
Now while I got this to work:
foreach ($_COOKIE as $ck => $c) {
echo "<br /> Cookie: Key:\"$ck\" value:\"$c\"";
}
I'd still like to know why the curly braces in the first example didn't
resolve the array function - I thought that was what they were supposed
to do?
Thanks, as usual.
|
|
|
Re: Array variable in echo statement [message #169783 is a reply to message #169782] |
Sun, 26 September 2010 18:41 |
Marious Barrier
Messages: 25 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
On 09/26/2010 02:24 PM, MikeB wrote:
>
> I tried this and it didn't work as I was hoping:
>
> foreach ($_COOKIE as $c) {
> $ck = key($_COOKIE);
> echo "<br /> Cookie: Key:\"{key($_COOKIE)}\" value:\"$c\"";
> }
>
> Now while I got this to work:
>
> foreach ($_COOKIE as $ck => $c) {
> echo "<br /> Cookie: Key:\"$ck\" value:\"$c\"";
> }
>
> I'd still like to know why the curly braces in the first example didn't
> resolve the array function - I thought that was what they were supposed
> to do
The curly braces are meant to wrap array variables, not functions.
|
|
|
Re: Array variable in echo statement [message #169784 is a reply to message #169783] |
Sun, 26 September 2010 18:59 |
MikeB
Messages: 65 Registered: September 2010
Karma: 0
|
Member |
|
|
Marious Barrier wrote:
> On 09/26/2010 02:24 PM, MikeB wrote:
>>
>> I tried this and it didn't work as I was hoping:
>>
>> foreach ($_COOKIE as $c) {
>> $ck = key($_COOKIE);
>> echo "<br /> Cookie: Key:\"{key($_COOKIE)}\" value:\"$c\"";
>> }
>>
>> Now while I got this to work:
>>
>> foreach ($_COOKIE as $ck => $c) {
>> echo "<br /> Cookie: Key:\"$ck\" value:\"$c\"";
>> }
>>
>> I'd still like to know why the curly braces in the first example didn't
>> resolve the array function - I thought that was what they were supposed
>> to do
>
> The curly braces are meant to wrap array variables, not functions.
Right. Thanks. So that mean that if I want the result of a function in
an output statement I should put it in a variable prior to the output
statement?
Like this:
$ck=key($_COOKIE);
echo "The key is $ck";
rather than:
echo "The key is key($_COOKIE)";
Which I know won't work and is why I thought the curly braces was meant
to resolve, but I guess I misread/misunderstood that part as well.
|
|
|
Re: Array variable in echo statement [message #169785 is a reply to message #169784] |
Sun, 26 September 2010 19:10 |
Marious Barrier
Messages: 25 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
On 09/26/2010 02:59 PM, MikeB wrote:
> Right. Thanks. So that mean that if I want the result of a function in
> an output statement I should put it in a variable prior to the output
> statement?
>
> Like this:
>
> $ck=key($_COOKIE);
> echo "The key is $ck";
>
> rather than:
>
> echo "The key is key($_COOKIE)";
>
> Which I know won't work and is why I thought the curly braces was meant
> to resolve, but I guess I misread/misunderstood that part as well.
All is correct.
|
|
|
Re: Array variable in echo statement [message #169786 is a reply to message #169784] |
Sun, 26 September 2010 19:22 |
Captain Paralytic
Messages: 204 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Sep 26, 7:59 pm, MikeB <mpbr...@gmail.com> wrote:
> Marious Barrier wrote:
>> On 09/26/2010 02:24 PM, MikeB wrote:
>
>>> I tried this and it didn't work as I was hoping:
>
>>> foreach ($_COOKIE as $c) {
>>> $ck = key($_COOKIE);
>>> echo "<br /> Cookie: Key:\"{key($_COOKIE)}\" value:\"$c\"";
>>> }
>
>>> Now while I got this to work:
>
>>> foreach ($_COOKIE as $ck => $c) {
>>> echo "<br /> Cookie: Key:\"$ck\" value:\"$c\"";
>>> }
>
>>> I'd still like to know why the curly braces in the first example didn't
>>> resolve the array function - I thought that was what they were supposed
>>> to do
>
>> The curly braces are meant to wrap array variables, not functions.
>
> Right. Thanks. So that mean that if I want the result of a function in
> an output statement I should put it in a variable prior to the output
> statement?
>
> Like this:
>
> $ck=key($_COOKIE);
> echo "The key is $ck";
>
> rather than:
>
> echo "The key is key($_COOKIE)";
>
> Which I know won't work and is why I thought the curly braces was meant
> to resolve, but I guess I misread/misunderstood that part as well.
Or:
echo 'The key is '.key($_COOKIE);
|
|
|
Re: Array variable in echo statement [message #169787 is a reply to message #169784] |
Sun, 26 September 2010 20:46 |
Michael Fesser
Messages: 215 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
.oO(MikeB)
> Marious Barrier wrote:
>>
>> The curly braces are meant to wrap array variables, not functions.
>
> Right. Thanks. So that mean that if I want the result of a function in
> an output statement I should put it in a variable prior to the output
> statement?
>
> Like this:
>
> $ck=key($_COOKIE);
> echo "The key is $ck";
Yes. Or have a look at printf(), which is extremely handy in situations
like this, especially if there are more than one variable:
printf('The key is %s.', key($_COOKIE));
Micha
|
|
|