Jump to content

returning variables as variables to be used later


phily245

Recommended Posts

Hi. I have some code which needs to return a single variable from the function and stored as a variable within this page, so it can be echoed later on in the page. I couldn't get it to return as a variable, so i used "return compact();" to return the variable and "extract (myFunction());" to extract it to variables. However, when I turned on php's display errors and error reporting function, I got an error message saying "Warning: extract() [function.extract]: First argument should be an array in /my/web/site/index.php on line 6" (which is where my extract function is). This works fine with passing more than one variables through, is there another way pass one variable from a function to be stored as a variable on the page which called the function?

 

Here is the function:

<?php
//This is a list of numeric error codes against their text. this will return the error code as the variable $issue
function checkLoginIssue() {

//If there is an error code
if (isset($_GET["issue"])) {

 	//cycle through the list until the code is reached, return the text and break the switch
    switch ($_GET["issue"]) {

      case "1":
              $issue = '<p class="warning">Please log in to view this page</p>';
              break;
		  
      case "2":
              $issue = '<p class="warning">Wrong Username or Password</p>';
              break;

	      case "3":
              $issue = '<p class="warning">No user found with those details</p>';
              break;

    }

 //return the variable in an array with a single value
 return compact('issue');

}

}
?>

 

And here is the code which calls the function:

<?php
extract(checkLoginIssue());
?>

$issue contains a text string, created by my switch in the code in the initial post:

<?php
//If there is an error code
if (isset($_GET["issue"])) {

 	//cycle through the list until the code is reached, return the text and break the switch
    switch ($_GET["issue"]) {

      case "1":
              $issue = '<p class="warning">Please log in to view this page</p>';
              break;
		  
      case "2":
              $issue = '<p class="warning">Wrong Username or Password</p>';
              break;

	      case "3":
              $issue = '<p class="warning">No user found with those details</p>';
              break;

    }
?>

 

return compact returns an array containing the variables and their variables, so in the case of switch case("3"), the array the function outputs would be:

<?php
array("issue" => "<p class="warning">No user found with those details</p>");
?>

 

which, when extracted, brings the following variable onto the page to use as though I had hand coded it:

<?php
$issue = '<p class="warning">No user found with those details</p>';
?>

I really think your overcomplicating something which is quite simple and using extract to inject variable into the current scope is going to lead to nothing more than code that is highly unmaintainable.

 

Whats wrong with simply returning the data you need and storing it in the normal way?

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.