Results 1 to 4 of 4

Thread: Problems with scope

  1. #1

    Problems with scope

    Hi there, I'm having this small problem with scope, I'm very new to PHP, so this is probably a beginner's mistake. I have the following code:


    PHP Code:
    $array = array("A""B""C");
    $count count($array);
    showCount();


    function 
    showCount(){
        echo(
    $count);

    However nothing gets echoed. But if put the echo function not inside showCount() it works. What should I do to be able to echo $count from withing the showCount() function?

    Thanks for your help

  2. #2
    Guys I found it out, but for the sake of others I'll show how it should be:

    PHP Code:
    $array = array("A""B""C");
    $count count($array);
    showCount();


    function 
    showCount(){
        global 
    $count;
        echo(
    $count);


  3. #3
    439
    posts
    Registered User
    try this:

    PHP Code:
    function showCount(){
        
    $array = array("A""B""C");
        
    $count count($array);
        return 
    $count;
    }  

    echo 
    showCount(); 
    The reason it wouldn't work is because you had your variable declared outside of the function. To access variables that are set outside of the function you have to set as a global var.

    http://uk3.php.net/manual/en/languag...bles.scope.php

    Also, it's good practice not to echo values within a function, return them instead and echo the retruned value via the function. This depends on the circumstanses though in some cases.

    Hope this helps!

    EDIT: Just noticed you solved it Good work.
    Last edited by Patch^; September 22nd, 2009 at 12:00 PM. Reason: he solved the problem, minutes before my post lol :)
    while ( ! ( succeed = try() ) );

  4. #4
    1,627
    posts
    hugeExplosions = true;
    Quote Originally Posted by Patch^ View Post
    try this:

    PHP Code:
    function showCount(){
        
    $array = array("A""B""C");
        
    $count count($array);
        return 
    $count;
    }  
     
    echo 
    showCount(); 
    The reason it wouldn't work is because you had your variable declared outside of the function. To access variables that are set outside of the function you have to set as a global var.

    http://uk3.php.net/manual/en/languag...bles.scope.php

    Also, it's good practice not to echo values within a function, return them instead and echo the retruned value via the function. This depends on the circumstanses though in some cases.

    Hope this helps!

    EDIT: Just noticed you solved it Good work.
    You could pass count as a parameter to the function. e.g.

    PHP Code:
    $array = array("A""B""C");
    $count count($array);
    showCount($count);
     
     
    function 
    showCount($count){
        echo(
    $count);

    Useful in some circumstances
    MS Paint FTW!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012