Page 1 of 1

get varialbe of one function from another function

Posted: Tue Oct 10, 2006 9:13 pm
by kalikaye
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


In the following script the function showvalues cannot recognize the variable dessert because the variable is initialized
in another function?....How can one function get to recognize the variables initialized in another function?
Can someone please improve this code...BTW I want to retain the table in echo format.
thanks so much

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<code>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form method="post" action="untitled.php">
<a href="untitled.php?action=showtable">Show Table</a>
<a href="untitled.php?action=showvalues&msguid=$msguid">Show Values</a>

<?php

function showtable(){
echo "<table>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit me!\" />\n";
echo "Do you like ice cream?\n";
echo "<input type=\"CHECKBOX\" name=\"dessert[]\" value=\"ice cream\">\n";
echo "<br>\n";
echo "Do you like cake?";
echo "<input type=\"CHECKBOX\" name=\"dessert[]\" value=\"cake\">\n";
echo "<br>\n";
echo "Do you like mint candies?";
echo "<input type=\"CHECKBOX\" name=\"dessert[]\" value=\"mint candies\">\n";
echo "<br>";
echo "</table>\n";
}
function showvalues(){
if (!is_array($_REQUEST['dessert'])) echo "not an array";     ///is always returned

echo $_REQUEST['dessert'];
echo $_REQUEST['dessert'][0];
echo $_REQUEST['dessert'][1];
echo $_REQUEST['dessert'][2];
}

switch ($action) {
case "showtable":
  showtable();
	break;
case "showvalues":
  showvalues();
	break;
default:
  showtable();
	break;
}		
?>	
</form>
</body>
</html>
</code>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Oct 10, 2006 9:31 pm
by Luke
you know you can pass arguments to functions, right?

Code: Select all

showtable($dessert);

Posted: Tue Oct 10, 2006 9:36 pm
by n00b Saibot
Please put your code inside

Code: Select all

tags

Posted: Wed Oct 11, 2006 2:54 am
by CoderGoblin
Several methods are available...

Pass variable by reference using the & character

Code: Select all

$desserts=array();

function setVariables(&$desserts)
{
  if (is_array($_REQUEST['desserts']) {
    $desserts=$_REQUEST['desserts']; // Should really validate each value if user input
  }
}

function useVars($desserts)
{
  print_r($desserts);
}
use Globals (OK I don't like doing this);

Code: Select all

$_GLOBALS['desserts']=array();
  function setVariables()
  {
    if (is_array($_REQUEST['desserts'])) {
      $_GLOBALS['desserts']=$_REQUEST['desserts']; // Should really validate each value if user input
    }
  }

  function useVars()
  {
    print_r($_GLOBALS['desserts']);
  }
use sessions (not really applicable if only used on this page)

Code: Select all

session_start(); // must be before any output (HTML or PHP);
   $_SESSION['desserts']=array();
 function setVariables()
  {
    if (is_array($_REQUEST['desserts'])) {
      $_SESSION['desserts']=$_REQUEST['desserts']; // Should really validate each value if user input
    }
  }

  function useVars()
  {
    print_r($_SESSION['desserts']);
  }

  unset($_SESSION['desserts']); // no longer needed

or my favourite solution is to redesign your function to validate and return the code needed, if only done once, or store in static variable if variables may be needed multiple times

Code: Select all

function setVariables()
  {
     static $cached=null; // only set the first time the function is called
      if ($cached == null) { //  Run only the first time this function is called
        if (is_array($_REQUEST['desserts'])) {
          // Should really validate each value if user input
          // $cached is only set if value is an array 
          $cached=$_REQUEST['desserts']; 
        } else {
          $cached=array();
        }
      }

      // returns an empty array if the request is not an array, or the desserts if it is.
      return $cached;
  }

  function useVars()
  {
    print_r(setVariables());
  }

Hopefully that gives you some things to play with. Remember never trust user input.