get varialbe of one function from another function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kalikaye
Forum Newbie
Posts: 3
Joined: Thu Sep 21, 2006 4:18 am

get varialbe of one function from another function

Post 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]
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you know you can pass arguments to functions, right?

Code: Select all

showtable($dessert);
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Please put your code inside

Code: Select all

tags
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply