#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Can anyone please explain me this program


    <?php
    function x ()
    {
    function y ()
    {
    return "y";
    }
    return "x";
    }
    echo y();
    ?>

    these x and y are confusing me....y is returned to function y and x is returned to function x.but both are not defined.when i run the program i got an error saying call to undefined function y()..
    my doubts are

    1)is x defined?? if so how?
    2)why am i getting error when i call y() ????
    Last edited by sam_sam56; July 22nd, 2013 at 02:24 PM. Reason: to add one more doubt
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,582
    Rep Power
    1906
    You have function Y inside function X, you should have it separated.
    Code:
    function x()
    {
    // ...
    }
    
    function y()
    {
    // ...
    }
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Gosh, where do you got this from? Did your cat walk over the keyboard?

    No, y is not defined, because it only gets defined when you call x. But if you call x more than once, you get an redeclaration error. No sane person would use anything like that.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0
    [QUOTE=Jacques1]Gosh, where do you got this from? Did your cat walk over the keyboard?

    No, y is not defined, because it only gets defined when you call x

    can u tell me please how its happening??
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by sam_sam56
    can u tell me please how its happening??
    You do understand that function y is defined inside of function x right? Without calling x, there is no y, because y doesn't get defined.

    You first need to call x, and then you have y.

    Like I said, things like this aren't done in reality, because they lead straight to debugging hell. You'd have two separate function definitions like MrFujin showed above.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo