Table of Contents
This guide covers PHP functions with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
The previous chapter is familiar with functions like Fopen () and Fread (). These are built-in functions, but PHP also gives you the option to create your own functions.
This chapter helps you understand the following two sections:
- Create a function in PHP
- Call a function in PHP
In fact, you almost don't need to create PHP functions yourself, because there are already over 1000 built-in functions in the library to handle different tasks and you just need to call them according to your needs.
How to Create Functions in PHP
In PHP, it is very simple to create your own function. Suppose you want to create a function, this function will simply write a message in the browser when you call it. The following example will create a QTMMessage () function and call it after creating.
Note that while creating a function, the function name should start with the Function Keyword and all PHP code should be put in {and} as follows:
?ây là hàm PHP php /* ??nh ngh?a hàm PHP */ function QTMMessage () { echo "Chúc các b?n h?c PHP th?t t?t!" ; } /* G?i hàm PHP */ QTMMessage (); ?>
Save the program in a file named Test. PHP in Htdocs , then open the browser and type the address HTTP: /// localhost: 8080 / test. PHP To see the result.
Wish you all good PHP students!
Function with Parameters in PHP
PHP gives you the option to pass parameters into a function. You can use as many parameters as you like. These parameters work as variables in the function. The following example uses two integer parameters and their sum and prints.
Hàm PHP v?i tham s? php function hamTimTong ( $num1 , $num2 ) { $sum = $num1 + $num2 ; echo "T?ng c?a hai s? là: $sum" ; } hamTimTong ( 15 , 35 ); ?>
Save the above program in a file named Test. PHP in Htdocs , then open the browser and type the address HTTP: /// localhost: 8080 / test. PHP Will result:
The sum of two numbers is: 50
Pass Parameters by Reference in PHP
It is possible to pass parameters to functions by reference. That is, a reference to the variable manipulated by the function instead of copying a value of that variable.
The following example describes these two cases:
Truy?n tham s? b?ng tham chi?u trong PHP php function themNam ( $num ) { $num += 5 ; } function themSau (
" ; themSau ( $orignum ); echo "Giá tr? bi?n orignum là $orignum
" ; ?>
Save the above program in a file named Test. PHP in Htdocs , then open the browser and type the address HTTP: /// localhost: 8080 / test. PHP Will result:
The value of orignum is 10 The value of the orignum variable is 16
The Function Returns the Value in PHP
A function can return a value by using the Return Statement associated with a value or an object. The return in PHP function stops the execution of the function and sends the value back to the calling code.
You can return more than one value from a function by using the Return array (1, 2, 3, 4) .
The following example takes two integer parameters and calculates their total value, then returns the result for the calling program. Note that the Return Keyword is used to return a value from a function.
Hàm tr? v? giá tr? trong PHP php function hamTimTong ( $num1 , $num2 ) { $sum = $num1 + $num2 ; return $sum ; } $return_value = hamTimTong (2 0 , 79 ); echo "Giá tr? hàm tr? v? là : $return_value" ; ?>
Save the above program in a file named Test. PHP in Htdocs , then open the browser and type the address HTTP: /// localhost: 8080 / test. PHP Will result:
The return function value is : 99
Set the Default Value for the Function Parameter in PHP
You can set a default value for the parameter if the caller does not pass the value to it.
The following function prints the Default String in the case of not passing any value to this function.
??t giá tr? m?c ??nh cho tham s? php function inTB ( $param = "M?c ??nh" ) { print $param ; } inTB ( "Truy?n tham s? cho hàm
" ); inTB (); ?>
Save the above program in a file named Test. PHP in Htdocs , then open the browser and type the address HTTP: /// localhost: 8080 / test. PHP Will result:
Pass parameters to functions Default
Call the Dynamic Function in PHP
In PHP, you can assign function names as strings to variables and then treat these variables as if you had the function name itself. The following example describes this behavior:
G?i hàm ??ng trong PHP php function sayChao () { echo "Xin chào!
" ; } $function_holder = "sayChao" ; $function_holder (); ?>
Save the above program in a file named Test. PHP in Htdocs , then open the browser and type the address HTTP: /// localhost: 8080 / test. PHP Will result:
Hello!
Follow tutorialspoint
Previous article: File & I / O in PHP
Next article: Cookie in PHP
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Reader Comments 0
Sign in with email or Google to join the discussion.