473,385 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Simple Problem With Variable Scope

This is my code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //start a session
  4. session_start();
  5.  
  6. $errors = NULL;
  7.  
  8. $firstname = $_POST["firstname"];
  9.  
  10. //validate fields
  11. function validate_firstname($firstname)
  12. {
  13. global $errors;
  14.  
  15. if (ereg("^[A-Z][a-z]+", $firstname))
  16. {
  17.     return true;
  18. }
  19. else
  20. {
  21.     $errors = $errors + "The first name entered is invalid.<br />e.g. Daniel<br /><br />";
  22.     return false;
  23. }
  24. }
  25.  
  26.  
  27.  
  28. //produce validation errors
  29. if (validate_firstname($firstname) == true) 
  30. {
  31.     include("header.php");
  32.     include("notmemberbar.php");
  33.     echo "<div class=\"content\">";
  34.     echo "<h1>Details Are Valid</h1>";
  35.     echo "<p>Well done</p>";
  36.     include("bottom.php");
  37.     return true;
  38. }
  39. else if (validate_firstname($firstname) == false)
  40. {
  41.     include("header.php");
  42.     include("notmemberbar.php");
  43.     echo "<div class=\"content\">";
  44.     echo "<h1>Details Are Invalid</h1>";
  45.     echo "<p>$errors</p>";
  46.     include("bottom.php");
  47.     return false;
  48. }
  49. ?>
  50.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

I'm having massive trouble printing out that variable $errors.
Any ideas? - I got an idea that its a very simple error im making.
Thanks.
William.
Jun 17 '07 #1
5 1268
pbmods
5,821 Expert 4TB
Heya, William. Welcome to TSDN!

Try declaring $errors as $GLOBALS['errors'] instead.
Jun 17 '07 #2
Atli
5,058 Expert 4TB
The global keyword is nothing but trouble.

I always define global variables like this:
Expand|Select|Wrap|Line Numbers
  1. $GLOBALS['varname'] = 'What?';
  2.  
Jun 18 '07 #3
THANKS a lot for your help, but it still didn't work.
Found a way round it though:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //start a session
  4. session_start();
  5.  
  6. //$GLOBALS['errors'] = NULL;
  7.  
  8. $firstname = $_POST["firstname"];
  9.  
  10. //validate fields
  11. function validate_firstname($firstname)
  12. {
  13.  
  14.  
  15. if (ereg("^[A-Z][a-z]+", $firstname))
  16. {
  17.     return true;
  18. }
  19. else
  20. {
  21.     echo "The first name entered is invalid.<br />e.g. Daniel<br><br />";
  22.     return false;
  23. }
  24. }
  25.  
  26.  
  27. //produce validation errors
  28. if (validate_firstname($firstname) == true) 
  29. {
  30.     include("header.php");
  31.     include("notmemberbar.php");
  32.     echo "<div class=\"content\">";
  33.     echo "<h1>Details Are Valid</h1>";
  34.     echo "<p>Well done</p>";
  35.     include("bottom.php");
  36.     return true;
  37. }
  38. else if (validate_firstname($firstname) == false)
  39. {
  40.     include("header.php");
  41.     include("notmemberbar.php");
  42.     echo "<div class=\"content\">";
  43.     echo "<h1>Details Are Invalid</h1>";
  44.     echo "<p>";
  45.     validate_firstname($firstname);
  46.     echo "</p>";
  47.     include("bottom.php");
  48.     return false;
  49. }
  50. ?>
  51.  
William.
Jun 18 '07 #4
Atli
5,058 Expert 4TB
Ok, that works. But you are calling the function three times, each time going through all the code and causing three times the overhead needed.

Might I suggest something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Create you function, and pass the
  3. // error variable with a & sign,
  4. // indicating that it is a reference
  5. // parameter that points to the $error
  6. // variable defined outside the function.
  7. function validate_firstname($fname, &$outError)
  8. {
  9.   // Do whatever test you want
  10.   if($fname == "John"){
  11.     // The name is valid, return true.
  12.     return true;
  13.   }
  14.   else{
  15.     // The name is invalid, change the 
  16.     // $outError,which will also change the 
  17.     // $error variable. Then return false.
  18.     $outError = "Sorry, John's only!";
  19.     return false;
  20.   }
  21. }
  22.  
  23. // this is the var for our error message
  24. $error = "Error";
  25.  
  26. // Try to validate
  27. if(validate_firstname("Jack", $error)) {
  28.   // Name valid
  29.   echo "Welcome John!";
  30. }
  31. else {
  32.   // Name invalid, print the error.
  33.   echo "Login failed: ". $error;
  34. }
  35. ?>
  36.  
Jun 18 '07 #5
moishy
104 100+
Make each error a variable within an array of all the errors.
Then use foreach to list out the errors
Jun 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dp | last post by:
I am new to VB.NET and I have a simple question. How do I show a form from a command button click event? The code I have below is not working. I am trying to show the form frmAgent. What am I...
15
by: Alex | last post by:
could somebody tell me the difference between those two styles: function abc(var1, var2){ /* logic in here */ } and abc = function(var1, var2){ /* logic in here */ }; When / why would I...
6
by: Justine | last post by:
Hi All, I have a very basic doubts about distroying an Object. In VB.NET we use Nothing. What is the equivalent in C#. kindly help me out... Thanz in Advance. Justine
12
by: Jeff B. | last post by:
If an object implements the IDisposable interface, should I always call the Dispose method or is just setting it to null and letting the GC handle it sufficient? Here is the pattern I've been...
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
3
by: Mehmet Yavuz S. Soyturk | last post by:
Hello, consider the next code: var obj = {}; with(obj) { var x = 10; } print(x); print(obj.x);
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
26
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.