Results 1 to 5 of 5

Thread: cant access vars within function?

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    cant access vars within function?

    PHP Code:
    <?php

       $a
    $HTTP_GET_VARS['a'];
        
    $b$HTTP_GET_VARS['b'];
        
    $c$HTTP_GET_VARS['c'];
        
    $d$HTTP_GET_VARS['d'];
        
    $e$HTTP_GET_VARS['e'];
        
    $f$HTTP_GET_VARS['f'];
        
    $g$HTTP_GET_VARS['g'];


     function 
    writeout($msg){
       
    $filename "files/" $a "tmp.html";

        
    $fhfopen($filename'a') or die("can't open file");
                
        if (
    filesize($filename) ==0) {
        
        
    //add the top stylesheet
        
    $SS "files/css.txt";
        
    $Stylefopen($SS'r') or die("can't open file");
        
    $read fread($Stylefilesize($SS)); 
        
    fwrite($fh$read);
        
    fclose($Style);
        }

       
    $b str_replace("&","&amp;",$b);
       
    $c str_replace("&","&amp;",$c);
       
    $a str_replace("&","&amp;",$a);
       
    $d str_replace("&","&amp;",$d);
       
    $e str_replace("&","&amp;",$e);
       
    $f str_replace("&","&amp;",$f);
       
    $g str_replace("&","&amp;",$g);

       
    $newline "\r\n";
     
      
    fwrite($fh$newline."<table border=2 width=" .chr(34)."5%".chr(34).">");
      
    fwrite($fh,"<tr class=".chr(34)."hdr".chr(34).">");
      
    fwrite($fh,"<td>".$a."</td>");
      
    fwrite($fh$newline."<a href="."http://www.site.com/".$msg." "."target=" .chr(34). "_blank".chr(34).">"."username"."</a></td>");
      
    fwrite($fh,"<td></td></tr>");
     
    fclose($fh);
    }


    writeout($a);
    writeout($b);
    writeout($c);
    writeout($d);
    writeout($e);
    writeout($f);
    writeout($g);

    ?>
    giving me undefined variable a,b,c,d,e,f,g etc.. as if i dont have access to the vars within the fucntion?

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: cant access vars within function?

    Try looking at global. This will allow you access variables from outside the function's scope.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: cant access vars within function?

    Yes - without a global statement in your function, you are writing to different vars.

  4. #4

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: cant access vars within function?

    good one.

    Next:

    If i have a string,

    $strstring = "a=first;b=next;c=middle;d=next"

    how would i extract middle, i know i have strstr()

    so strstr($strstring,'c=') would give me middle;d=next

    i need to do a double split?

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: cant access vars within function?

    Something like
    PHP Code:
    $vars explode(';'$strstring);
    echo 
    trim(substr($vars[2], $start strpos($vars[2], '='), strlen($vars[2]) - $start)); 

Posting Permissions

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



Click Here to Expand Forum to Full Width