Thread: Functions

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    21
    Rep Power
    0
    Hello...

    I am trying to make a function (called sub in perl "i think"). Lets say that i made this function:
    _________________________
    function printme() {
    print "hellon";
    }
    _________________________

    Now i want to "call" this function/subroutine whatever u call it.

    I tried with :

    printme();

    but i this error: unsupported function call or something....

    I know there isnt anything wrong with php...error is in my head Anyone knows what i am doing wrong?

    Normally in perl its like this:

    &printme;

    sub printme {
    print "Hellon";
    }

    Thanks in advance!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    43
    Rep Power
    15
    You could try:

    _________________________
    function printme() {
    print("hellon");
    }
    _________________________

    ------------------
    webpatron.com
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    21
    Rep Power
    0
    Hmm the problem is not in my function, the function works perfectly. My problem is that i dont know how to "call" the function the right way. I tried with functionname();

    But it doesn seems to work.

    do i have to specify the function before i call it in the code? Is my function call correct by typing functionname(); ?

    thx
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    21
    Rep Power
    0
    Hmm ok i think i found REAL ERROR in this script. The problem in the script below is that $username and $password from my FORM are not passed to my function checkpwd. Anyone who has a clue why? i can print the variables
    everywhere in the script except in the function, so in some weirdo way all my variables from the FORM are forgotten in my function. PLZ helpå me with this one.

    __________________________________________
    <form action="index.php3" method="post">
    <input type=text name=username ><br>
    <input type=password name=password > <br>
    <input type=submit value=Go>

    <?

    function checkpwd()
    {
    if($password == "blabla")
    {

    print "Pass OK";
    }
    else
    {
    print "Incorrect Password";

    }
    }


    if($username == "moron")
    {
    checkpwd();
    }
    else{
    print "Incorrect Username";
    exit;
    }

    ?>
  8. #5
  9. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Your code is fine. Yes, you need to declare functions before calling them in PHP.
  10. #6
  11. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    form action="index.php3" method="post">
    <input type=text name=username ><br>
    <input type=password name=password > <br>
    <input type=submit value=Go

    close your form with </form>tag

    <form action="index.php3" method="post">
    <input type=text name=username ><br>
    <input type=password name=password > <br>
    <input type=submit value=Go>
    </form>


    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    21
    Rep Power
    0
    heh nice tip Shiju, but it still doesnt work.
    I wrote this into my function:

    print "$password , $username";

    and it returned NADA. Which means that my FORM variable data is still unaccessible from the function.
  14. #8
  15. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    just try the following:

    test.php3
    ---
    <html>
    <body>
    <b> Test</b>
    <form action="<? echo $PHP_SELF;?>" method="post">
    <input type="text" name="username"><br>
    <input type="password" name="password"> <br>
    <input type="submit" value="Go">
    </form>
    <?

    function checkpwd()
    {
    if($password == "blabla")
    {

    print "Pass OK";
    }
    else
    {
    print "Incorrect Password";

    }
    }


    if(isset($username))
    {
    checkpwd();
    }
    else{
    print "Incorrect Username";
    exit;
    }

    ?>
    </body>
    </html>


    Good Luck!!

    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."

    [This message has been edited by Shiju Rajan (edited July 03, 2000).]
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    7
    Rep Power
    0
    You either have to pass the variables to the function as arguments:

    function checkpwd($password)
    {
    // later you can call the function with
    // checkpwd($password);
    }

    OR declare them as globals in the function:

    function checkpwd()
    {
    global $password;
    ...
    }

    otherwise the variable is outside the scope of the function.
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    21
    Rep Power
    0
    God bless u guys.

    Both of your solutions Shiju and Danmckay worked.



    ps! DanMckay: the global $password: thing worked but the other one:
    function checkpwd($password)

    Generated following error:

    ____________________________________________
    Warning: Missing argument 1 in call to checkpwd() in /home/httpd/html/index.php3 on line 14
    ____________________________________________

    Anyway thx A LOT GUYS!!!!
  20. #11
  21. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    otherwise the variable is outside the scope of the function.


    That is very correct actually..

    you should pass the variable to the function..


    Your script should be:
    <?
    function checkpwd($value)
    {
    if($value == "blabla")
    {

    print "Pass OK";
    }
    else
    {
    print "Incorrect Password";

    }
    }


    if(isset($username))
    {
    checkpwd($password);
    }
    ?>

    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."

    [This message has been edited by Shiju Rajan (edited July 03, 2000).]
  22. #12
  23. No Profile Picture
    a total freak
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    33
    Rep Power
    15
    it's acctually simple,.. just do what danmckay suggest:
    set the $password and $username as global so:

    function checkpwd() {
    global $password,$username;

    ...

    }

    PHP sets all variable within user-defined functions as local variables within functions, to use variables that is outside the function, global is then used..

    ------------------
    ---freakyG!---
    kinumedia, web developer

Similar Threads

  1. functions slowing code
    By dave2k in forum PHP Development
    Replies: 5
    Last Post: February 7th, 2004, 07:40 PM
  2. problem with functions
    By veni123 in forum C Programming
    Replies: 8
    Last Post: February 5th, 2004, 03:22 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 2
    Last Post: January 9th, 2004, 03:26 PM
  4. finding all Javascript objects and functions in a page
    By WESuydam in forum JavaScript Development
    Replies: 4
    Last Post: July 31st, 2003, 02:29 PM
  5. Using functions within functions.
    By winddancer in forum PHP Development
    Replies: 7
    Last Post: May 4th, 2003, 12:25 PM

IMN logo majestic logo threadwatch logo seochat tools logo