PHP function reference

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
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:

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
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:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
as4s1n said:
Would you be able to include mysql_error() as one of the parameters of a function

i.e. writeError(mysql_error())

That will work.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
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:
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:
Top