Edward Posted March 25, 2008 Share Posted March 25, 2008 Hi, I am using a form on my site. I define the form in a function, and then call the function as required. For some reason the form isn't displaying my variables. OK I admit I am defining them after the form, but I am defining them before I call the function. Like so... function test() { echo 'Name: '.$name; } $name = 'John Smith'; test(); Shouldn't this display 'Name: John Smith'? At present it only displays 'Name: '. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted March 25, 2008 Share Posted March 25, 2008 You need to read up on variable scope. http://us3.php.net/variables.scope Once you understand what is happening, I'll warn you about using the keyword global. Don't use it. Global variables are bad. Pass the variable into your function instead. Quote Link to comment Share on other sites More sharing options...
Edward Posted March 25, 2008 Author Share Posted March 25, 2008 Thank you. I've read through examples 1-7. While I'll read them again later, I do get the gist of what is happening, and why it therefore doesn't work. Rather than use 'global $name' or '$GLOBALS['name']', which you say are bad, can you please tell me what you would recommend and also explain why global variables are bad? Are you suggesting I use static variables? Quote Link to comment Share on other sites More sharing options...
Edward Posted March 26, 2008 Author Share Posted March 26, 2008 The (basic!) example below shows why I was trying to use a variable with global scope inside my function. If someone can suggest a better (rather than using the Global array) way to do what I'm trying to achieve, that would be great! <?php function form() { echo '<input type="text" name="first_name" size="20" value="<?php echo $_POST['first_name']; ?>" />'; if ($errors = TRUE) { // Here is where I want to reference a variable of global scope echo 'There was an error with your name!'; } echo '<input class="button_submit" type="submit" value="validate" name="submit" />'; } if (isset($_POST['submit'])) { # Validate First Name if (strlen($_POST['first_name']) < 1) { $errors = TRUE; } if ($errors = TRUE) { echo '<p>There was an error!</p>'; test(); } else { echo 'Thank you'; } } else { test(); } ?> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted March 26, 2008 Share Posted March 26, 2008 Follow the link in my signature to Part I for PHP forms; it will give you a pretty good outline for building PHP forms. I do use a global variable in the example to make it slightly more simple; I'm working on Part II where the global is eliminated and a few other things are cleaned up. Globals are bad for a number of reasons, the most significant of which is they create unmaintainable code in large programs. What will happen is you will have a large number of global variables and be unable to tell from where they are being set and / or changed. You will create a global named $errors in one script and use it again in another, not meaning to overwrite the existing one. Program bugs like this can be very difficult to debug. The best solution to access data in a function is to pass it in as a parameter (or argument). This means your functions can only use data given to them and can't inadvertently modify data in another part of the program. The only global variables in PHP you can use safely are $_POST, $_GET, $_COOKIE (or $_COOKIES), $_SESSION, $_SERVER, and any other non-deprecated auto-globals I forgot to mention. Quote Link to comment Share on other sites More sharing options...
Edward Posted March 26, 2008 Author Share Posted March 26, 2008 Thank you very much! I'd be really interested to see part 2 of your form guide when it's available. Just to be sure, are you suggesting I do something like the following in my function: function form($first_name_validity, $last_name_validity, $email_address_validity, $errors) { // Form if (isset($errors)) { // display generic error message } if ($first_name_validity = FALSE) { // display name error message } } This will mean I have a very long line of code when I pass the parameters to the function, but you're saying this is a better option to using global variables, right? If so, this is the way I'll proceed. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted March 26, 2008 Share Posted March 26, 2008 Not quite and I'm not so tired right now so I can give you a bit more of an explanation. ValidateForm() will go through and check all of your validation conditions. Each time it finds one that fails you add an item to the $errors array. Therefore, $errors is an array with keys: fname_invalid, lname_invalid, email_invalid, etc. Each one of these indexes is the error message you wish to display. This allows you in ShowForm() to do something simple like: echo '<p>We found the following errors:</p>' . '<ul><li>' . implode('</li><li>', $errors) // this line expands the $errors array . '</li></ul>'; Now if you want to eliminate the global variable, you can just have ValidateForm() return a local variable $errors and pass this into your ShowForm() equivalent as a single argument. Quote Link to comment Share on other sites More sharing options...
Edward Posted June 22, 2008 Author Share Posted June 22, 2008 Thanks very much roopurt18. I got the form working but I've just come across a similar problem and I'm not sure how it can be fixed. This time, I am defining a variable INSIDE the function, and I want to access it OUTSIDE the function. Is this possible? I've set up a script on my local server but the variable isn't being accessed. Here is my script so far: function connect_to_mysql() { if ($db_connect = @mysql_connect('localhost:8889', 'root', 'root')) { if (!@mysql_select_db('my_db')) { $errors .= '<li class="errors">The database could not be collected because: <b>' . mysql_error() . '</b>.</li>'; } } else { $errors .= '<li class="errors">The connection to MySQL could not be made because: <b>' . mysql_error() . '</b>.</li>'; } } connect_to_mysql(); if (!$errors) { echo 'ok'; } else { echo 'error'; } Do you know what I'm doing wrong? THank you very much for your help so far. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 22, 2008 Share Posted June 22, 2008 You can't access local variables globally. That's the whole point of making them local. Instead, change your function to return $errors and call it like so: $errors = connect_to_mysql(); Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Or look into OOP. *Cough cough* Quote Link to comment Share on other sites More sharing options...
Edward Posted June 23, 2008 Author Share Posted June 23, 2008 Hi roopurt18, thank you for telling me that, I didn't know local variables were permanently local. I never seen what you demonstrated before, what exactly does that line of code mean? You seem to be assigning a function to a variable's value?? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 23, 2008 Share Posted June 23, 2008 It's the other way around. The function returns a value that you assign to the variable. Read the documentation on functions and look for an example. Quote Link to comment Share on other sites More sharing options...
Edward Posted June 24, 2008 Author Share Posted June 24, 2008 Thanks roopurt18! I've got it sorted, thanks to you! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 24, 2008 Share Posted June 24, 2008 Glad it's working. I would have given more detailed responses as of late but work has been keeping me very busy. 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.