phily245 Posted October 28, 2011 Share Posted October 28, 2011 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/249973-returning-variables-as-variables-to-be-used-later/ Share on other sites More sharing options...
trq Posted October 28, 2011 Share Posted October 28, 2011 What exactly does compact('issue') return? Quote Link to comment https://forums.phpfreaks.com/topic/249973-returning-variables-as-variables-to-be-used-later/#findComment-1282963 Share on other sites More sharing options...
phily245 Posted October 28, 2011 Author Share Posted October 28, 2011 It currently returns $issue as a variable like I'd hand coded it onto the page, eg '$issue = "Example";'. This works, however the error message still occurs. Quote Link to comment https://forums.phpfreaks.com/topic/249973-returning-variables-as-variables-to-be-used-later/#findComment-1282972 Share on other sites More sharing options...
trq Posted October 28, 2011 Share Posted October 28, 2011 Is $issue an array? Your really not giving us enough detail to be able to help much. Quote Link to comment https://forums.phpfreaks.com/topic/249973-returning-variables-as-variables-to-be-used-later/#findComment-1282973 Share on other sites More sharing options...
phily245 Posted October 28, 2011 Author Share Posted October 28, 2011 $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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/249973-returning-variables-as-variables-to-be-used-later/#findComment-1282979 Share on other sites More sharing options...
trq Posted October 28, 2011 Share Posted October 28, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/249973-returning-variables-as-variables-to-be-used-later/#findComment-1282985 Share on other sites More sharing options...
phily245 Posted October 28, 2011 Author Share Posted October 28, 2011 I was expecting to have to do something waaaay more complicated than that. Problem solved. Sometimes it just takes someone to say stop overcomplicating things. Quote Link to comment https://forums.phpfreaks.com/topic/249973-returning-variables-as-variables-to-be-used-later/#findComment-1283012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.