SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrays/variables don't write inside function?

    I am having a bit of a brain freeze today. I am writing a function, and as part of the function, I want a counter to be incriminated, and a record added to my function. This should run after a form is submitted, so it checks how many name/email combos have been submitted, and ads them to the array accordingly.

    The code I have, looks like this

    Code PHP:
    $i=0;
    $people = array();
    if(isset($_POST['submitMe'])){
    	function writeArray($name,$email){
    		$i++;
    		echo $i;
    		$people[$name] = $email;
    		print_r($people);
    	}
     
    	if($_POST['name1']!=NULL){
    		writeArray($_POST['name1'],$_POST['email1']);
    	}
    	if($_POST['name2']!=NULL){
    		writeArray($_POST['name2'],$_POST['email2']);
    	}
    }

    So if in the form, I enter a name into both name 1 and 2 fields, I'll get output like this:

    Code:
    1Array ( [Dan Smith] => dan.smith@example.com )
    1Array ( [Joe Blow] => joe@blow.com )
    It seems to not save the previous name, and just creates a new array with 1 user. The counter is also not incremented. How can I get my counter to show 2, and both of these gentlemen's names to be shown?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This is what's called scope. The $i and $people in your function are local variables. They are not the $i and $people you're using outside of the function. The variables are in different scopes; literally, they are part of two different symbol tables for the interpreter. If you want to modify them, either pass them in by reference, or make the pair a return value.

    You can bring the other variables into scope by making them globals, but unnecessary use of global variables leads to messy, hard to maintain code.

    PHP Code:
        function writeArray($name,$email){
            global 
    $i;
            global 
    $people;
            
    $i++;
            echo 
    $i;
            
    $people[$name] = $email;
            
    print_r($people);
        } 
    versus

    PHP Code:
    function writeArray($people$name$email) {
    ...

    return 
    $people;
    }

    $people writeArray($people$name$email); 
    If $i just keeps track of the size of the array, count($people) would give you that directly.

  3. #3
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dan. Very helpful.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •