Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

PHP Functions

Explore PHP Functions with clear explanations, practical examples, and useful tips.

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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.