how to create function in php
how to create function in php
1 Answer
First of all, there are two types of function
- built-in Functions are shown by the language
- User-Defined Functions
built-in Functions
- like print_r(), var_dump(), var_export()
User-Defined Functions
- which are programmed by the developer as needed
When writing your function follow these rules
- Start with a
function
word - Follow with function_name
- can start with string or underscore
- not case-sensitive
- can`t start with number
- can`t use PHP Reserved words
Syntax
function function_name(){
// any code
}
Example
<?php
function say_hello(){
echo 'hello';
}
?>
The function is called by its name with brackets
say_hello();
Example
<?php
function say_hello(){
echo 'hello';
}
say_hello();
?>
Output
hello
answer Link