Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with an array in a function

    Having the following example, I'm getting all messages correctly when the function isn't called but, why can't I get the messages inside the function when the function is called but I'm still retrieving correctly the variable and the rest of the messages outside the function? What am I doing wrong?
    PHP Code:
    /* my_functions.php */
    /* START FUNCTIONS */
    function my_function($var,$mess) {
      if (
    bla bla bla) {
        do 
    this to $var;
        
    $mess[] = "bla bla bla";
        return 
    $var;
      } else {
        
    $mess[] = "bla bla bla";
      }
    /* END FUNCTIONS */
    /* my_page.php */
    include('my_functions.php');
    /*START VALIDATION */
    if (bla bla bla) {
       do 
    this;
       
    $message[] = "bla bla bla";
    } else {
       
    $message[] = "bla bla bla";
    }
    if (
    bla bla bla) {
       
    my_function($variable,$message);
       
    $message[] = "bla bla bla";
    } else {
       
    $message[] = "bla bla bla";
    }
    /* END VALIDATION - START OUTPUT */
    for ($i 0$i count($message); $i++) {
      echo 
    $message[$i];
    }
    /* END OUTPUT */ 
    Thanks to all.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you'll need to pass the variable by reference:

    function my_function($var,&$mess) {
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That worked. Thanks a million. Could anybody explain the way it works and why it is handled this way? Is that because it is an array? Thanks.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's because you are changing the value within the function and not returning it.

    Normally variable are passed by value, so a new local variable is created using the values that you passed. Any changes will not affect the original. When you pass by reference you assign the variable inside the function to be another name for the variable that you pass to the function, so any changes to the variable locally will also be performed on the variable passed.
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)


  •  

    Posting Permissions

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