#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Undefined Global Array Problem


    My script here is still a work in progress, but I like to debug as I code in order to resolve issues beforehand.

    I keep getting an error saying that $aryRegister is an undefined variable. I thought by leaving the variable outside any functions would allow the variable to be global.

    Code:
    <?php
    
    $aryRegister[] = array(
      'email' => '',
      'user' => '',
      'pass' => '',
      'pass2' => '',
    );
    
    if(!empty($_POST)) // if form data has been posted
    {
    // store user submitted data in array 
    $aryRegister['email'] = mysql_real_escape_string($_POST['txtEmail']);
    $aryRegister['user'] = mysql_real_escape_string($_POST['txtUser']);
    $aryRegister['pass'] = mysql_real_escape_string($_POST['txtPass']);
    $aryRegister['pass2'] = mysql_real_escape_string($_POST['txtPass2']);
    }
    
    function ValidateData()
    {
      // CHECK IF ARRAY CONTAINS DATA                         
      if(!empty($aryRegister))
        {
        // CREATE ERROR MESSAGES AS NEEDED      
        $errors = array(); // error messages are stored here
        if(trim($aryRegister['email']) == '')
          $errors[] = 'Please enter your email';
        if(trim($aryRegister['user']) == '')
          $errors[] = 'Please enter your username';
        if(trim($aryRegister['pass']) == '')
          $errors[] = 'Please choose a secure password';
        if(trim($aryRegister['pass2']) == '')
          $errors[] = 'Please confirm your password';
        if(trim($aryRegister['pass']) != trim($aryRegister['pass2']))
          $errors[] = 'The passwords entered do not match';
        
        return $errors;
        }
    }
    
    function RegisterUser()
      {
      $errors = ValidateData();
    
      if(empty($errors))
        {
        $dbcon = mysql_connect(DELETED FOR SECURITY);
      
        if(!$dbcon)
          die('Could not connect: ' . mysql_error());
        
        mysql_select_db("DELETED FOR SECURITY", $dbcon);
      
        $dbquery = "INSERT INTO users (id, email, user, pass, verified, join_date, last_login)
                    VALUES ('', '$aryRegister[email]', '$aryRegister[user]',
                            '$aryRegister[pass]', '0', now(), '')"; 
                  
        if(!mysql_query($dbquery, $dbcon))
          die('Error: ' . mysql_error());     
        
        exit();
        }
      else
        {    
        $message = '<span style="color:red">';
      
        foreach($errors AS $error)
          {
          $message .= $error."<br />";
          }
        $message .= '</span>';
        $_SESSION['message'] = $message;
        }
      
      if(isset($_SESSION['message']))
        {
        echo $_SESSION['message'];
        unset($_SESSION['message']);
        }
      }
    ?>
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,580
    Rep Power
    1906
    You don't need [] in front of the variable name:
    Code:
    $aryRegister = array(
      'email' => '',
      'user' => '',
      'pass' => '',
      'pass2' => '',
    );
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    I removed the [] but the problem persists.

    Originally Posted by MrFujin
    You don't need [] in front of the variable name:
    Code:
    $aryRegister = array(
      'email' => '',
      'user' => '',
      'pass' => '',
      'pass2' => '',
    );
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    I thought by leaving the variable outside any functions would allow the variable to be global.
    No

    In this case you should either not being using a function, or you should be passing the array in as a parameter.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Passing the array as a parameter fixed the issue. Thank you for your response and help.

    Originally Posted by E-Oreo
    No

    In this case you should either not being using a function, or you should be passing the array in as a parameter.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    just to clear it up: $aryRegister is indeed a global variable, but inside a function or method you have to explicitly make it available using either the "global" keyword or the $GLOBALS array.

    e. g.
    PHP Code:
    <?php

    $x 
    10;

    function 
    f() {
        global 
    $x;        // access $GLOBALS['x']
        
    echo $x;
    }

    f();
    In your case, however, using the function parameters is indeed the only sensible option. You should generally be careful with global variables. Only use them for things which are actually supposed to be global and used everywhere (like configuration values). The normal way of passing values to functions is through the parameters.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Hyderabad
    Posts
    10
    Rep Power
    0

    try to declare arrays or variables as global


    Hello friend,

    you get these kind of errors when you do not declare the arrays or variables as global, so try to declare them as global or in the file where you are calling these functions but not in functions directly.

    Thank you.
  14. #8
  15. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Originally Posted by xhtmlchamps1
    try to declare them as global or in the file where you are calling these functions but not in functions directly.
    I challenge you to demonstrate how that is possible.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Hyderabad
    Posts
    10
    Rep Power
    0

    code with little error


    $array = array(

    // as of PHP 5.4
    $array = [
  18. #10
  19. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Allow me to be more specific:

    I challenge you to demonstrate how that is possible with an entire script which we can run ourselves that shows your solution.
    What you've posted is meaningless.

IMN logo majestic logo threadwatch logo seochat tools logo