SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2001
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Functions in PHP

    I have a little question.
    When I want to display the result of a certain function entitled say tax, could I just go
    tax(); ? or do I *have* to go echo(tax());

    The reason why I ask is cos in this book I'm learning PHP from it says you need to echo out the funtion in order for it to be displayed on the browser. However in their example they neglected that fact. I thought it might be an error so I wrote that down all the code myself and it came out fine. Here's the code:

    <?php
    $GlobalVariable = "Global";
    function local()
    {
    static $StaticVariable = 0;
    echo"<br>The contents of the GlobalVariable are" . $GLOBALS["GlobalVariable"];
    echo"<br>The contents of the StaticVariable are $StaticVariable";
    return $StaticVariable = $StaticVariable + 1;
    }
    echo "<b>Locally</b><br>";
    echo "<br>The contents of the GlobalVariable are $GlobalVariable";
    echo "<br>The contents of the StaticVariable are $StaticVariable<p>";
    local();
    echo "<p><b>Locally</b><br>";
    echo "<br>The contents of the GlobalVariable are $GlobalVariable";
    echo "<br>The contents of the StaticVariable are $StaticVariable<p>";
    local();
    ?>

    Weird thing is when I type echo(local()); I get a slightly different result.

    Appreciate anyone if they could help me with the confusion..
    Bardius

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on the function. If you have echo statements in the function then you don't need to echo the function call itself. However, many functions don't echo anything directly, they return a string that you can echo. This is the best way to write your funtions because it allows you do to other things with the string like store it to a variable, output to a file or database, perform regex on it, etc.
    ck :: bringing chris to the masses.

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To clarify, some functions like this:

    PHP Code:

    function testfunc()
    {

        if (
    $test) {
           
    $return 'Success';
        }
        else {
           
    $return 'Failed';
        }
        return 
    $return;

    need to have the echo to print out the results.

    echo testfunc();

    Others like :

    PHP Code:

    function testfunc()
    {

        if (
    $test) {
           echo 
    'Success';
        }
        else {
           echo 
    'Failed';
        }


    will return the results without echoing it.

    like

    testfunc();

    well...its better practise to use return $return; in functions as it allows more control over how the results are displayed.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also its up to the programmer to provide sufficient documentation for the functions they write. For example, when you look up a function in the manual, it will tell you what the function does, what parameters it takes as arguements and what it returns.


Bookmarks

Posting Permissions

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