+ Reply to Thread
Results 1 to 6 of 6

Thread: PHP function reference

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    PHP function reference

    I was wondering if you could create a PHP function on one page and reference it on several other pages, not unlike the javascript <script src="filepath.js"></script> functionName(params) method.
    Last edited by as4s1n; 03-10-2010 at 01:45 PM.

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: PHP function reference

    http://php.net/manual/en/function.include.php
    The include() and require() functions are what you want. Put your shared function in a file, include it with include or require and then you can use it just like normal.
    Last edited by lemon-tree; 03-10-2010 at 02:00 PM.

  3. #3
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP function reference

    Cool, thanks.

    I have a question. Would you be able to include mysql_error() as one of the parameters of a function

    i.e. writeError(mysql_error())

    Or would there be a different way to do that? I do not think mysql_error() would work globally if I used that inside my function
    Last edited by as4s1n; 03-10-2010 at 07:35 PM.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: PHP function reference

    Quote Originally Posted by as4s1n
    Would you be able to include mysql_error() as one of the parameters of a function

    i.e. writeError(mysql_error())
    That will work.
    Nothing is always absolutely so.

  5. #5
    misson is offline Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: PHP function reference

    Quote Originally Posted by as4s1n View Post
    i.e. writeError(mysql_error())
    If you intend to display the output of mysql_error to the user, first read "Writing Error Messages for Security Features: Information Disclosure". The short version is that mysql_error should only be shown to site admins.

    Generally speaking, you can pass the output of functions as the input to other functions (the exceptions are some special forms in some languages and, for PHP before 5.3.0, func_get_args). Functional programming would be very ungainly if you couldn't.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: PHP function reference

    That was really the problem I was trying to fix: A simple way to write one to two lines of code that will run whenever I need it and display a custom error message: "There is an error" to the user and write to an errors file "errors.log" that I would look at and see what is wrong and how to fix it.

    Here is the writeError() function:
    PHP Code:
    function writeError($error) {
    #Get the page
    $page $_SERVER['PHP_SELF'];
    #Get the date it happened
    $date date('r');
    #Write to the user
    echo "There was an error. The webmaster has already been notified";
    #Write the error to a file I will read later
    $logstring "Error on page $page on $date; ERROR: $error";
    $fp fopen("Logs/erros.log","a");
    fwrite($fp$logstring);
    fclose($fp);

    Last edited by as4s1n; 03-12-2010 at 02:00 PM.

+ Reply to Thread

Similar Threads

  1. in reference to my last thread... (ticket)
    By chupacabrah in forum Free Hosting
    Replies: 7
    Last Post: 01-21-2010, 07:07 AM
  2. PHP Max Function Help
    By diabolo in forum Programming Help
    Replies: 2
    Last Post: 07-22-2009, 10:12 PM
  3. How to get reference to all Form elements
    By anuj_web in forum Programming Help
    Replies: 2
    Last Post: 05-10-2008, 01:25 AM
  4. External Javascript reference failing
    By ansultd in forum Programming Help
    Replies: 2
    Last Post: 05-07-2008, 05:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers