neridaj Posted February 27, 2008 Share Posted February 27, 2008 Hi, I'm trying to understand why declaring a variable global within a function is not working. I have a function that displays a table of users and is supposed to set a global variable to true when called in order to let another function know whether or not to display a hyperlink. Here is what I have: function display_users($user_array) { // display the table of users // set global variable, so we can test later if this is on the page global $user_table; $user_table = true; ?> <br /> <form name='user_table' action='delete_users.php' method='post'> <table width=300 cellpadding=2 cellspacing=0> <?php $color = "#000000"; echo "<tr bgcolor='$color'><td><strong>User</strong></td>"; echo "<td><strong>Delete?</strong></td></tr>"; if (is_array($user_array) && count($user_array)>0) { foreach ($user_array as $user) { if ($color == "#cccccc") $color = "#ffffff"; else $color = "#cccccc"; // remember to call htmlspecialchars() when we are displaying user data echo "<tr bgcolor='$color'><td><a href=\"$user\">".htmlspecialchars($user)."</a></td>"; echo "<td><input type='checkbox' name=\"del_me[]\" value=\"$user\"></td>"; echo "</tr>"; } } else echo "<tr><td>No users on record</td></tr>"; ?> </table> </form> <?php } function display_admin_menu() { ?> <a href="admin_member.php">Home</a> | <?php // only offer the delete option if user table is on this page global $user_table; if($user_table==true) echo "<a href='#' onClick='user_table.submit();'>Delete User</a> | "; else echo "<font color='#cccccc'>Delete User</font> | "; } Thanks for any help, Jason Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 27, 2008 Share Posted February 27, 2008 IIRC, using the word global that way means you defined the variable outside of the function, and want to use it IN the function. I think you need to have the function return it, or maybe use the $_GLOBALS array to set it this way? Quote Link to comment Share on other sites More sharing options...
neridaj Posted February 27, 2008 Author Share Posted February 27, 2008 It might help if I called the functions in the correct order. Thanks, Jason Quote Link to comment 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.