Results 1 to 3 of 3

Thread: [RESOLVED] it doesn't work in function

  1. #1

    Thread Starter
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Resolved [RESOLVED] it doesn't work in function

    ok My code doesn't work when its in a function, yet it does when its not why is that?

    This Works:
    PHP Code:
    $handle fopen("http://www.runescape.com/lang/en/aff/runescape/serverlist.ws?lores.x=0&plugin=0&order=WPML","r");
    if(
    $handle){
    while (!
    feof($handle)) {
    $contents .= fread($handle,8192);}
    fclose($handle);}

    $world 2;
    $lengthA strpos(stristr($contents'World '.$world), 'FULL');
    $lengthB strpos(stristr($contents'World '.$world), 'Players');

    if (
    $lengthA $lengthB){
            echo 
    'Full';
        }
    else
        {
            echo 
    strpos(stristr($contents'World '.$world), 'Players');
        } 
    And This Doesn't:
    PHP Code:
    $handle fopen("http://www.runescape.com/lang/en/aff/runescape/serverlist.ws?lores.x=0&plugin=0&order=WPML","r");
    if(
    $handle){
    while (!
    feof($handle)) {
    $contents .= fread($handle,8192);}
    fclose($handle);}
       

    function 
    Population($world)  {
        
    $lengthA strpos(stristr($contents'World '.$world), 'FULL');
        
    $lengthB strpos(stristr($contents'World '.$world), 'Players');
        
        if (
    $lengthA $lengthB){
                return 
    "Full";
            }
        else
            {
                return 
    strpos(stristr($contents'World '.$world), 'Players');
            }
    }
    echo 
    Population(2); 
    Why is that?
    Last edited by wiz126; Feb 6th, 2007 at 05:15 PM.
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: it doesn't work in function

    Its because of variable scope ($handle is used outside the function). Any variable initialised outside a function must be accessed via the $GLOBALS array or declared in the function using global:
    PHP Code:
    $handle = ....

    function 
    blah()
    {
        echo(
    $GLOBALS['handle']);
    }

    function 
    glah()
    {
        global 
    $handle;

        echo(
    $handle);

    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: it doesn't work in function

    WOW I am dumb! I forgat the
    PHP Code:
    global $contents
    I have waisted a whole 30 minuts on that crap! and all the problem was a line i forgat.... thank man
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

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