Page 1 of 1

Function Idea

Posted: Thu Dec 28, 2006 6:02 pm
by orbstra
Is this idea possible? I have a bunch of cases for my switch, and they are all exactly the same, except they each have one variable that changes with each one. I thought I should just make a function called SC, then use that, although it is not working.

Apache Err Msg:
Parse error: parse error, unexpected T_STRING, expecting T_CASE or T_DEFAULT or '}' in C:\Documents and Settings\Administrator\My Documents\My Projects\Web Projects\Guava\guav-includes\lib_tags.php on line 31

IDE Err Msg:
Parser error "';' expected after expression (Found token: Variable($object))" - line 38

Code: Select all

function dir($arg)
		{
			switch ($arg){
				SC('theme'); //Line 31
				SC('admin');
				SC('includes');			
			}
		}
		function SC($object)
			{
				case $object:
					global $object; return $object;
				break;
			}
}
thnx all

Posted: Thu Dec 28, 2006 6:05 pm
by timvw
You will have to lookup the syntax of a switch statement...

Posted: Fri Dec 29, 2006 1:23 am
by orbstra
I have it dosen't help me here

Posted: Fri Dec 29, 2006 1:38 am
by volka
orbstra wrote:I have it dosen't help me here
But it does ;)

Code: Select all

switch ($arg){
	// missing a case statement here
	SC('theme'); //Line 31
	SC('admin');
	SC('includes');   
}

Posted: Fri Dec 29, 2006 12:03 pm
by RobertGonzalez

Code: Select all

<?php
switch ($var)
{
  case 'somecase':
    // handle processing
    break;

  case 'someothercase':
  default:
    // handle other processing
  break;
}
?>
timvw wrote:You will have to lookup the syntax of a switch statement...
I think that is what he meant. For your reference, the PHP manual explains the switch statement pretty well...

Posted: Fri Dec 29, 2006 1:33 pm
by orbstra
NVM, I was wondering if instead of putting the same case about 40 times, with only one changing variable in each whole case, I made a function which was shorter than writing out the whole case.

so instead of writing each case like this:

Code: Select all

case admin:
    global $admin;
    return $admin;
break;
I would have this function:

Code: Select all

function SC($object)
                        {
                                case $object:
                                        global $object; return $object;
                                break;
                        }
and write each case like this, very simple and short:

Code: Select all

SC('theme');
SC('page');
SC('sys');
SC('foo');
SC('bar');
SC('this_is_esiar_then_writing_out_each_whole_case');
I am going to rewrite each case statement alot of times. I am just going to do it manually wich each full case statement now but I thought I would just explain what my original idea was.

Posted: Fri Dec 29, 2006 3:42 pm
by feyd
Who says you have to put the same code in a whole bunch of times?

in_array() may be of interest, as may variable variables.

Posted: Fri Dec 29, 2006 4:26 pm
by orbstra
feyd wrote:Who says you have to put the same code in a whole bunch of times?

in_array() may be of interest, as may variable variables.
that's perfect thnx

Posted: Fri Dec 29, 2006 9:08 pm
by daedalus__
OOP ftw