Simple Problem With Variable Scope

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wwolfson
    New Member
    • Jun 2007
    • 13

    Simple Problem With Variable Scope

    This is my code:
    [code=php]
    <?php

    //start a session
    session_start() ;

    $errors = NULL;

    $firstname = $_POST["firstname"];

    //validate fields
    function validate_firstn ame($firstname)
    {
    global $errors;

    if (ereg("^[A-Z][a-z]+", $firstname))
    {
    return true;
    }
    else
    {
    $errors = $errors + "The first name entered is invalid.<br />e.g. Daniel<br /><br />";
    return false;
    }
    }



    //produce validation errors
    if (validate_first name($firstname ) == true)
    {
    include("header .php");
    include("notmem berbar.php");
    echo "<div class=\"content \">";
    echo "<h1>Detail s Are Valid</h1>";
    echo "<p>Well done</p>";
    include("bottom .php");
    return true;
    }
    else if (validate_first name($firstname ) == false)
    {
    include("header .php");
    include("notmem berbar.php");
    echo "<div class=\"content \">";
    echo "<h1>Detail s Are Invalid</h1>";
    echo "<p>$errors </p>";
    include("bottom .php");
    return false;
    }
    ?>
    [/code]

    [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.
    Last edited by pbmods; Jun 17 '07, 11:40 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, William. Welcome to TSDN!

    Try declaring $errors as $GLOBALS['errors'] instead.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      The global keyword is nothing but trouble.

      I always define global variables like this:
      [code=php]
      $GLOBALS['varname'] = 'What?';
      [/code]

      Comment

      • wwolfson
        New Member
        • Jun 2007
        • 13

        #4
        THANKS a lot for your help, but it still didn't work.
        Found a way round it though:
        [code=php]
        <?php

        //start a session
        session_start() ;

        //$GLOBALS['errors'] = NULL;

        $firstname = $_POST["firstname"];

        //validate fields
        function validate_firstn ame($firstname)
        {


        if (ereg("^[A-Z][a-z]+", $firstname))
        {
        return true;
        }
        else
        {
        echo "The first name entered is invalid.<br />e.g. Daniel<br><br />";
        return false;
        }
        }


        //produce validation errors
        if (validate_first name($firstname ) == true)
        {
        include("header .php");
        include("notmem berbar.php");
        echo "<div class=\"content \">";
        echo "<h1>Detail s Are Valid</h1>";
        echo "<p>Well done</p>";
        include("bottom .php");
        return true;
        }
        else if (validate_first name($firstname ) == false)
        {
        include("header .php");
        include("notmem berbar.php");
        echo "<div class=\"content \">";
        echo "<h1>Detail s Are Invalid</h1>";
        echo "<p>";
        validate_firstn ame($firstname) ;
        echo "</p>";
        include("bottom .php");
        return false;
        }
        ?>
        [/code]
        William.
        Last edited by Atli; Jun 18 '07, 07:23 PM. Reason: Added [code] tags, please use [code] tags when posting code!

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          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:
          [code=php]
          <?php
          // Create you function, and pass the
          // error variable with a & sign,
          // indicating that it is a reference
          // parameter that points to the $error
          // variable defined outside the function.
          function validate_firstn ame($fname, &$outError)
          {
          // Do whatever test you want
          if($fname == "John"){
          // The name is valid, return true.
          return true;
          }
          else{
          // The name is invalid, change the
          // $outError,which will also change the
          // $error variable. Then return false.
          $outError = "Sorry, John's only!";
          return false;
          }
          }

          // this is the var for our error message
          $error = "Error";

          // Try to validate
          if(validate_fir stname("Jack", $error)) {
          // Name valid
          echo "Welcome John!";
          }
          else {
          // Name invalid, print the error.
          echo "Login failed: ". $error;
          }
          ?>
          [/code]

          Comment

          • moishy
            New Member
            • Oct 2006
            • 104

            #6
            Make each error a variable within an array of all the errors.
            Then use foreach to list out the errors

            Comment

            Working...