static variables

Discussion in 'PHP' started by mz906, Jun 27, 2008.

  1. #1
    is it possible to use static variables inside a function?
     
    mz906, Jun 27, 2008 IP
  2. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes.

    To use variables from outside do this:

    
    function addslashes2($value) {
         return addslashes($value);
    }
    
    $username = addslashes2($username);
    
    PHP:
     
    itnashvilleCOM, Jun 28, 2008 IP
  3. mz906

    mz906 Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    but my function lives in another file, i was reading that you can use static variables to replace globals, is this true?
     
    mz906, Jun 28, 2008 IP
  4. 2slick

    2slick Peon

    Messages:
    73
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    use pass by reference :

    e.g.

    
    $a=3;
    
    ref($a);
    
    function ref(& $v){
       $v=$v+1;
    }
    echo $a;
    
    Code (markup):
    or most commonly used by globals

    
    global $a;
    $a=3;
    
    ref();
    
    function ref(){
      global $a;
      $a=$a+1;
    }
    echo $a;
    
    
    Code (markup):
     
    2slick, Jun 28, 2008 IP