" ; 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
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
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
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