Help with functions and.. global variables?

Discussion in 'PHP' started by Crayz, Jun 19, 2007.

  1. #1
    Well, I need to create a variable that will be updated inside one of my functions.

    Say I had..

    index.php
    <?php
    
    include 'functions.php';
    
    $var1 = "Hello";
    
    test();
    
    echo $var1;
    
    ?>
    Code (markup):

    Functions.php

    <?php 
    
    function test() {
      $var1 = "Bye";
    }
    
    ?>
    Code (markup):
    But it doesn't update my variable :(

    Suggestions?

    Thanks :)
     
    Crayz, Jun 19, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Go find out about scope.

    If you want a quick fix:
    function test() {
      global $var1;
      $var1 = "Bye";
    }
    PHP:
     
    krt, Jun 19, 2007 IP
  3. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can also pass the variable by reference to the function:
    function test(&$var1) {
      $var = "Bye!";
    }
    PHP:
    if you run the function like this:
    $var = "Hi";
    
    test($var);
    
    echo $var; // will output "Bye!"
    PHP:
     
    UnrealEd, Jun 20, 2007 IP
  4. samusexu

    samusexu Well-Known Member

    Messages:
    138
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Yeah I would also recomend passing by reference to functions when you need that value modified and returned. It's a memory saver too.
     
    samusexu, Jun 20, 2007 IP
  5. stauf

    stauf Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    not to budge in here, but what if you had say 7 variables to reference?
     
    stauf, Jun 20, 2007 IP
  6. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #6
    Then you should send the files containing your code onto a USB drive or a CD and then burn it. Smash whatever remains using a hammer, preferably a large one.
     
    krt, Jun 20, 2007 IP
  7. samusexu

    samusexu Well-Known Member

    Messages:
    138
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #7
    Then just puss them into an array and reference that array
     
    samusexu, Jun 20, 2007 IP
  8. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #8
    Ok cool, thanks everyone :)
     
    Crayz, Jun 20, 2007 IP
  9. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    oh, before i forget: always pass a variable to the function, not just a string or array. For instance this won't work:
    test ( array('a', 'b', 'c') );
    PHP:
    php will throw an error, as it can't update the value of the variable (because there is no value :) )
     
    UnrealEd, Jun 20, 2007 IP