Jump to content

[SOLVED] Function inside of custom function...?


Patrick3002

Recommended Posts

I was wondering if it is possible to to have a function inside of a custom created function?

 

Heres an example:

 


<?php

function orderBy()
{
switch ($selorder)
{
case snasc:
	$field = "sname";
	$ad = "ASC";
	$ordered = "Service Name (ASC)";
	break;
case sndesc:
	$field = "sname";
	$ad = "DESC";
	$ordered = "Service Name (DESC)";
	break;
default:
	$field = "sname";
	$ad = "ASC";
	$ordered = "Service Name (ASC)";
}

}

?>

A switch case is not really a "function" it can be inside a function as it acts just like an if/elseif/else statement.

 

The problem here lies within the case portion, try this:

 

switch ($selorder)
{
case 'snasc':
	$field = "sname";
	$ad = "ASC";
	$ordered = "Service Name (ASC)";
	break;
case 'sndesc':
	$field = "sname";
	$ad = "DESC";
	$ordered = "Service Name (DESC)";
	break;
default:
	$field = "sname";
	$ad = "ASC";
	$ordered = "Service Name (ASC)";
}
?>

 

Those words need to be surrounded by quotation marks or else php takes it as a constant.

Ding, my bad man. If field, ad and ordered are not defined as global they are local only to the function. You can define them as being global or return them in an array. You also have to pass the parameter in or set that global also. IE:

 

<?php

function orderBy($selorder)
{
  global $field, $ad, $ordered;
switch ($selorder)
{
case snasc:
	$field = "sname";
	$ad = "ASC";
	$ordered = "Service Name (ASC)";
	break;
case sndesc:
	$field = "sname";
	$ad = "DESC";
	$ordered = "Service Name (DESC)";
	break;
default:
	$field = "sname";
	$ad = "ASC";
	$ordered = "Service Name (ASC)";
}

}
?>

 

OR

 

<?php

function orderBy($selorder)
{
switch ($selorder)
{
case snasc:
	$field = "sname";
	$ad = "ASC";
	$ordered = "Service Name (ASC)";
	break;
case sndesc:
	$field = "sname";
	$ad = "DESC";
	$ordered = "Service Name (DESC)";
	break;
default:
	$field = "sname";
	$ad = "ASC";
	$ordered = "Service Name (ASC)";
}

return array("field" => $field, "ad" => $ad, "ordered" => $ordered);
}

$getOrder = orderBy("sndesc");
foreach ($getOrder as $key => $val) {
     // set the key equal to a variable with the value;
     $$key = $val;
}
?>

 

Hope that helps.

 

One minor comment...

 

You need to be very conscious of local vs. global scope.  Anything defined outside the function will be ignored unless you use the global command or the $GLOBALS[] variable.  On the same accord, anything defined within the function will be unavailable to the global scope unless it is returned. 

 

Example:


$some_var = 'This is global scope.';

$final_var = some_function();  // Option 1
$another_final_var = another_function($some_var); // Option 2 - which is what I would recommend

function some_function() {
   global $some_var;  // this pulls $some_var into local scope to be acted upon.

   $new_var = $some_var.'  But I can act upon it with the global command.';

   return $new_var;  // the combined will then be returned.  NOTE that $new_var never leaves local scope.
}

function another_function($some_var) {
   // $some_var is now part of local scope
   $new_var = $some_var.'  This is another option.';

   return $new_var;  // again, $new_var never leaves local scope.
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.