Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts

    Question Undefined variable?

    HI People

    How come the following code generates undefined variable error?

    Error:
    Code:
    Notice: Undefined variable: abc in D:\xampp\htdocs\test2.php on line 6
    Code:
    <?php 
    error_reporting(E_ALL);
     $abc  = 'hello';
    
    function xyz(){
    	echo $abc;
    }
    
    echo xyz();
    
    ?>

    Any help will be appreciated


    Thanks

  • #2
    New Coder
    Join Date
    Apr 2008
    Location
    Blagoevgrad, Bulgaria
    Posts
    47
    Thanks
    0
    Thanked 8 Times in 8 Posts
    Set the $abc into the function

    PHP Code:
    <?php 
    function xyz() {
    $abc  'hello';
    return 
    $abc;
    }
    echo 
    xyz();
    ?>

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    No, I want to access a variable that is declared outside a function, within that function. can we?

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    2
    Thanked 3 Times in 3 Posts
    You have to pass variables into a function, they cannot read variables created outside. So now, function xyz reads the first parameter entered (in this case $abc is sent in the line echo xyz($abc) and uses it as variable $test within the function xyz...

    PHP Code:
    <?php 
    $abc  
    'hello';

    function 
    xyz($test){
        echo 
    $test;
    }

    echo 
    xyz($abc);

    ?>
    http://www.w3schools.com/php/php_functions.asp (scroll down to adding parameters)

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    you could also make use of the superglobals (in this case the $GLOBALS array) that PHP provides.

  • #6
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For a start you effectively are duplicating the echo command, once within the fuction and once when calling the function. Secondly you need to declare the variable global within the function.

    Code:
    <?php 
    error_reporting(E_ALL);
    $abc  = 'hello';
    
    function xyz(){
       global $abc;
        echo $abc;
    }
    
    xyz();
    ?>
    OR
    Code:
    <?php 
    error_reporting(E_ALL);
    $abc  = 'hello';
    
    function xyz(){
       
        echo $GLOBALS['abc'];
    }
    
     xyz();
    ?>

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    globals destroy reusability. Pass you're variable as a parameter to you're function instead.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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