HOME
PHP
Introduction Php Installation
PHP Basic Scripts
PHP variables
PHP operators
PHP Conditional statement
PHP functions
PHP Arrays
PHP Objects & classes
PHP with Forms
PHP Files
PHP With Database
PHP session
PHP cookies
PHP Regular Expressions
PHP Server Environment
PHP Graphics
PHP Advance
|
Create a PHP Function
A function is just a name we give to a block of code that can be executed whenever we need it.
This might not seem like that big of an idea, but believe me, when you understand and use functions you will
be able to save a ton of time and write code that is much more readable!
A function is a block of code that is not immediately but can be called by php script whenever needed.functions can be userdefined or build-in
For example, you might have a ADD that you have to display at least once on every webpage. If you don't, then you get fired! Well, being the savvy PHP programmer you are, you think to yourself, "this sounds like a situation where I might need functions."
Creating Your First PHP Function
-
All functions start with the word "function()"
-
Name the function - It should be possible to understand what the function does by
- its name. The name can start with a letter or underscore (not a number)
-
Add a "{" - The function code starts after the opening curly brace
-
Insert the function code
-
Add a "}" - The function is finished by a closing curly brace
When you create a function, you first need to give it a name, like ADD. It's with this function name that you will be able to call upon your function, so make it easy to type and understand.
First, you must tell PHP that you want to create a function. You do this by typing the keyword function followed by your function name and some other stuff (which we'll talk about later).
Here is how you would make a function called ADD
PHP Code:
<?php
function ADD(){
}
?>
|
EXAMPLE -1
| |