Forum Moderators: coopster

Message Too Old, No Replies

Array syntax in control structures and heredoc

         

mealybar

2:35 am on Mar 8, 2006 (gmt 0)

10+ Year Member



Hi,
I'm just wondering if there is a way to do the following (I cant seem to find it anywhere)

if($example['foo'] == "bar") { ... }

or in any other control statement; if, for, while,,,

At the moment I have to;

Sfoo = $example['foo'];
if($foo == "bar") { ... }

is there a way to condence it down to the one line? Also if you try to use the same array syntax in a heredoc it will not work; reason? fix?

Out of interest on anyone viewing this thread asking the same; I find it strange that this (from php docs):
$string = "Blah blah {$example['foo']}";
will work, but not in heredoc or in if, while,,,

whoisgregg

3:05 am on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if($example['foo'] == "bar") { ... }

Works for me all the time. What error are you getting?

mealybar

3:45 am on Mar 8, 2006 (gmt 0)

10+ Year Member



Dont get an error, just doesnt work :-/

coopster

12:59 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The braces syntax should work just fine in double-quotes as well as heredoc syntax. I would have a closer look at the values in the variables and the value with which you are comparing -- dump them to the browser using var_dump() [php.net].

whoisgregg

3:11 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So if you take this complete script and run it on your server, what do you get?

<?php
$example['foo'] = 'bar';
if($example['foo'] == "bar") {
echo 'Foo *is* bar!';
} else {
echo 'Foo is *not* bar?';
}
?>

mealybar

1:51 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



This is really strange, that piece of code worked!

Would it make any difference defining a string with single quotes? $foo = 'bar'; $foo = "bar";

By the same token; $example["foo"] and $example['foo']?

inveni0

3:47 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



Sometimes these things are weird!

whoisgregg

8:01 pm on Mar 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If that code works, then there must be something else going on in your actual code.