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.
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.
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.
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.
That will work.Originally Posted by as4s1n
Nothing is always absolutely so.
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.
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.