spires Posted April 13, 2010 Share Posted April 13, 2010 Hi I have place my form error code inside a function so I can reuse it through out the site. I am returning the errors, but I also want to return a 2nd variable. This will place a star next to the fields that have not been filled in. The Function: function message_errors($message, $errors){ $output = ""; $star = array(); if (!empty($message)) { $output .= "<p class=\"message\">" . $message . "</p>"; } if (!empty($errors)) { $output .= "<p class=\"errors\">"; foreach ($errors as $error) { $output .= " - " . $error . "<br />"; $star[] = $error; } $output .= "</p>"; } return $output; } The Form: <?PHP $message = message_errors($message, $errors); echo $message; ?> <form action="edit_subject.php?subj=<?PHP echo urlencode($sel_subject['id']); ?>" method="POST"> <p>Subject name: <?PHP if (in_array('menu_name',$star)) { echo "*"; }?> <input type="text" name="menu_name" value="<?PHP echo $sel_subject['menu_name']; ?>" id="menu_name" /> </p> <p>Subject position: <?PHP if (in_array('position',$star)) { echo "*"; }?> <input type="text" name="position" value="<?PHP echo $sel_subject['position']; ?>" id="position" /> </p> </form I can only get the returned output from the function, how do I also get the variable $star out as well? Thanks for your help Link to comment https://forums.phpfreaks.com/topic/198359-getting-variables-out-of-functions/ Share on other sites More sharing options...
litebearer Posted April 13, 2010 Share Posted April 13, 2010 a function can return multiple results. it can return arrays AND you can set session variables in an array that then are available outside the function - a plethora of possibilities Link to comment https://forums.phpfreaks.com/topic/198359-getting-variables-out-of-functions/#findComment-1040834 Share on other sites More sharing options...
trq Posted April 13, 2010 Share Posted April 13, 2010 Besides functions being able to return arrays, your $star array is exactly the same as the $error array you passed into the function. Why not just use that? Link to comment https://forums.phpfreaks.com/topic/198359-getting-variables-out-of-functions/#findComment-1040943 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.