Results 1 to 8 of 8

Thread: [RESOLVED] Using a variable outside of a function

  1. #1

    Thread Starter
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Resolved [RESOLVED] Using a variable outside of a function

    can anyone help me on how to use a variable oution of a function.

    for example

    i have a variable name $none outside of a function and i want to use that variable inside that function, it returns and empty string when i tried it.
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Using a variable outside of a function

    Could you post the code on how you use the variable?

    Basically what I did is just I call the function that has an argument for the variable in html script and pass the variable to it.

  3. #3

    Thread Starter
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: Using a variable outside of a function

    OK
    a sample script below

    PHP Code:
    $my_name "nobody";

    function 
    test(){
    $test_var "Iam";
    $test_var $test_var.$my_name;
    return 
    $test_var;
    }

    echo(
    test()); 
    that outputs "Iam" without the "Nobody"
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Using a variable outside of a function

    PHP Code:
    <?php

    $test
    ="marvin";

    function 
    x($test){
    $t="i am ";
    $t=$t.$test;
    return 
    $t;
    }

    echo(
    x($test));

    ?>
    Dont know if that would be the best way.

  5. #5

    Thread Starter
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: Using a variable outside of a function

    yes for now i'm restricted to using that, but i felt there should be a way of doin it without having to define the variable along with the function
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

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

    Re: Using a variable outside of a function

    Global variables need to be imported into functions using the global keyword. Otherwise, they are created anew inside the function's scope.

    Superglobals, such as $_POST etc., do not require this.

    PHP Code:
    $my_name "nobody";

    function 
    test()
    {
      global 
    $my_name;

      
    $test_var 'I am' $my_name;

      return 
    $test_var;
    }

    echo 
    test(); 

  7. #7
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Using a variable outside of a function

    As what I've said there would be a better way.

  8. #8

    Thread Starter
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: Using a variable outside of a function

    Great! thanks...
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

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