SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: php functions

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi everyone,

    i'm experimenting with a simple php script, and here's what it looks like:

    if ($var1 == "222") {
    write();
    }
    else {
    echo("Error");
    }


    fuction write()
    echo("this is a test");

    but the function write() doesn't execute when i call the write(); , and i get an error from my server.
    i'm pretty new to php so would somebody please help me out on how to call a function up.

    Thank you
    Sang N
    sang@vinasite.com

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try replacing your write() function with the following:

    Code:
    function write() {
      echo("this is a test");
    }
    If this doesn't work, please let us know what the error message you're getting is.

    [Edited by kyank on 09-23-2000 at 04:23 PM]
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    the error i get is:
    "Fatal error: Call to unsupported or undefined function write() in /home/vina/vinasite-www/validate.php on line 24
    "

    and on line 24, i have:
    write();


    Sang N.
    sang@vinasite.com

  4. #4
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You have misspelled "function" as "fuction".
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's corrected, but i still get the same error...
    Sang N.
    -------------------------------
    Send an E-card today!
    http://ecard.vinasite.com

  6. #6
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The following code works perfectly here:

    Code:
    <?
    $var1="222";
    
    if ($var1 == "222") { 
      write(); 
    } 
    else { 
      echo("Error"); 
    } 
    
    
    function write() {
      echo("this is a test"); 
    }
    ?>
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    humm...
    weird..

    I guess it's a problem with my server's configuration.

    Thanks for your help

    Sang N.
    -------------------------------
    Send an E-card today!
    http://ecard.vinasite.com

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is possible that you may need to define your function above the call to it.
    So this should work:
    <?
    function write() {
    echo("this is a test");
    }
    $var1="222";
    if ($var1 == "222") {
    write();
    }
    else {
    echo("Error");
    }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    that worked!

    Sang N.
    -------------------------------
    Send an E-card today!
    http://ecard.vinasite.com

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem! If you were working in a class, you could call it from wherever using $this->functionname() but since it was stand alone you would have to define it before you could call it.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Odd. That was not required here (witness my code above). What version of PHP are you running?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    4.0.2 and I have run into that situation several times, as the page is parsed by the server it goes on down the page and tries to run write() before it know s what it is. Not sure why it worked for you
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    4.0.1pl2 here... My test file contained only the code in my post above, and worked withtout a hitch. To satisfy my curiosity, would you care to try it on your own server?

    From p.132 of "Professional PHP Programming" by WROX Press:

    In PHP3, the function declaration must appear in the code before any invocations of the function. However, in PHP4 the function can be called prior to its declaration.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  14. #14
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't you know "Curiosity killed the Cat!" No, just kidding it worked on my machine, so I don't know what happened with his machine?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  15. #15
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe he was runnning PHP3
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •