Forum Moderators: coopster

Message Too Old, No Replies

PHP switch control structure

help with square brackets...

         

soquinn

11:41 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



I’m testing this control structure to serve content based on a variable but the square brackets are a problem… any suggestions?

<? switch ([VAR_NAME]) {
case 'FOO':
echo 'wooo';
break;
default: echo 'whoo';
}
?>

Thanks

StupidScript

11:48 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the purpose of the square brackets in the variable reference? Usually they're found in array references (as you probably know).

How about substituting it for a "normal" variable?

<?

$thisvar=[VAR_NAME];

switch ($thisvar) {

case 'FOO':

echo 'wooo';

break;

default: echo 'whoo';

}

?>

ironik

11:48 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



The square brackets are used to access elements in an array, if you just need a variable then they are prepended with a $ sign.

If it is an array you need to use then use something like $array['key']

Here's your example with a variable name:


<?php switch ($VAR_NAME) {
case 'FOO':
echo 'wooo';
break;
default: echo 'whoo';
}
?>

soquinn

12:04 am on Apr 15, 2005 (gmt 0)

10+ Year Member



Thanks... [VAR_NAME] is the variable format in the template? $VAR_NAME didn't work, so I'll try:

$thisvar=[VAR_NAME];

soquinn

1:07 am on Apr 15, 2005 (gmt 0)

10+ Year Member



When I tried:

<?
$thisvar=[VAR_NAME];
switch ($thisvar) {
case 'FOO':
echo 'wooo';
break;
default: echo 'whoo';
}
?>

I got:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING

ironik

2:31 am on Apr 15, 2005 (gmt 0)

10+ Year Member



You're using a template system? Templates are normally designed to rid the need of putting php in with html (and in the case of smarty, presentation logic from business logic).

But if you really need the 2 then you should be calling the [VAR_NAME] from where it was originally assigned (a proper PHP variable). A template will parse and replace it's own variables in a page, and I doubt that it will do it and allow PHP to be used at the same time... it wouldn't make sense.

Perhaps we need a bigger picture of what you are trying to achieve, and also what template system you are using.

soquinn

9:49 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Thanks, it seems the php file was not parsing the code in the section where I pasted the switch control structure code… was trying it after <<<EOF