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 6 of 6
  1. #1
    New Coder
    Join Date
    May 2008
    Posts
    70
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Variables in function

    Hi, i've define a variable outside a function and use the vars inside the function but it doesn't work.
    my code:

    PHP Code:
    <?php
    $money 
    100;
    $bowl 5;

    //then i fetch data from my database
    if ($admin == 1){
    adminmenu();
    }
    else{
    membermenu();
    }
    ?>
    <body topmargin="0" leftmargin="0">
    <?php
    function adminmenu(){
    ?>
    <table>
    Admin Menu <br>
    amount of money : <?php echo $money ?>
    amount of bowl : <?php echo $brick ?>
    </table>
    <?
    }
    function 
    membermenu (){
    ?>
    <table>
    Member Menu <br>
    amount of money : <?php echo $money ?>
    amount of bowl : <?php echo $brick ?>
    </table>
    <?php
    }
    ?>
    The $echo money and $ echo bowl doesn't show up.

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Because you haven't passed the variables to the function. To do this add parameters to the function and when it's called pass the variables to it.

    PHP Code:
    <?php
    $money 
    100;
    $bowl 5;

    //then i fetch data from my database
    if ($admin == 1){
    adminmenu($money$brick);
    }
    else{
    membermenu($money$brick);
    }
    ?>
    <body topmargin="0" leftmargin="0">
    <?php
    function adminmenu($money$brick){
    ?>
    <table>
    Admin Menu <br>
    amount of money : <?php echo $money ?>
    amount of bowl : <?php echo $brick ?>
    </table>
    <?
    }
    function 
    membermenu ($money$brick){{
    ?>
    <table>
    Member Menu <br>
    amount of money : <?php echo $money ?>
    amount of bowl : <?php echo $brick ?>
    </table>
    <?php
    }
    ?>
    Alternatively you could set it to $_SESSION and then you won't need to add the parameters.
    Last edited by Iszak; 06-10-2008 at 12:29 PM.

  • #3
    New Coder
    Join Date
    May 2008
    Posts
    70
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Oh thanks, so you mean to use that function, i MUST pass the variables? because i thought my function is just like replacing this script with another if the condition are met.

  • #4
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Well it only seems like what you said because of your conditions. I might also add your code is very erm un-needed because the functions are only outputting HTML so why not just use the conditions to display the HTML and variables instead of calling a function? Or put the HTML in a file and call the file.

  • #5
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    You can use the keyword global.

    PHP Code:
    $myvar "Billy";
    function 
    print_var() {
        global 
    $myvar;
        print 
    $myvar;
    }
    print_var(); 
    But it's bad practice to do this - defining everything on the global level breaks encapsulation and makes your code difficult to read. Only do this if absolutely necessary. Passing variables between functions is easier to read and easier to debug too.
    zok@zoklet:~$ whereis zok
    zok: http://zoklet.net | http://zoklet.net/otg | /derzok/at/gmail/dot/com

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    The purpose of a function is to allow a useful block of code to be used one or more times in one file without needed to type or copy/paste the code every place it is used and to allow that useful block of code to be easily reused in a different file by simply including the function definition and then calling the function any place it is needed.

    To accomplish this, functions are autonomous. They operate independent of the program they are part of (consider the case of all the built in php functions.) They accept optional input parameters, perform the processing they were designed to do, and return or output the results.

    If you have variables that are "part of the function" and need to be referenced by other functions or need to persist after the function has been called, then you should actually be using an OOP class to contain both the function(s) and related variable(s).

    If what you are trying to do with a function does not fit this model, then you should probably not be using a function for the block of code you are trying to make into a function.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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