| Website PHP - Functions |
PHP - Functions: Despite the fact that PHP comes with such a large selection of functions to perform all sorts of tasks, you will want to create your own functions when the need arises. If you find yourself doing the same thing repeatedly, or you want to share code across projects, user functions are for you.
Writing monolithic programscode that starts at the beginning and runs straight through to the endis considered very bad for program maintainability, as you are not able to reuse code. By writing functions, you make your code shorter, easier to control and maintain, and less prone to bugs.
A Simple User Function
You can give your functions whatever name you like; they follow the same guidelines (without the $) as PHP's variables. You may not redefine PHP's built-in functions, and care should be taken to ensure that your function names do not collide with existing PHP functionsjust because you don't have the imagepng( ) function available, it doesn't mean others also won't.
The simplest user function in PHP looks something like this:
Return Values
You can return any variable you want, as long as it is just one variableit can be an integer, a string, a database connection, etc. The return keyword sets up the function return value to be whatever variable you use with it, then exits the function immediately. You can also just use return;, which means "exit without sending a value back." If you try to assign to a variable the return value of a function that has no return value (e.g., it uses return; rather than return $someval;), your variable will be set to NULL.
As you can see, recursive functions make programming certain tasks particularly easy, and it is not all math, eitherconsider how easy it is to write a function showchildren( ) for a forum, which automatically shows all replies to a message, and all replies to those replies, and all replies to the replies to the replies, and so on.
Read More PHP:PHP - Functions | PHP - Including Other Files | PHP - Comments Tutorial | PHP - Example Whitespace | PHP - Variables of PHP | PHP - Basics of PHP | PHP Abnormal Script | PHP - Output Control
Links to this article:"PHP - Functions"
| Users' Comments (0) |
|
No comment posted








