PHP functions

PHP functions are similar to other programming languages. A function is a piece of code that takes one or more inputs in the parameter form, then performs some processing and returns a value.

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 will help you understand the following two sections:

  1. Create a function in PHP
  2. 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.

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.

Any changes made to a parameter in these cases will change the value of the original variable. You can pass a parameter by reference by adding a symbol & for the variable name in: function call or function definition.

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 ( & $num ) { $num += 6 ; } $orignum = 10 ; themNam ( $orignum ); echo "Giá trị biến orignum là $orignum
" ; 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

4 ★ | 2 Vote

May be interested

  • Python locals () functionPython locals () function
    the locals () function in python returns a dictionary containing the variables defined in the local namespace.
  • Functions are fields in GolangFunctions are fields in Golang
    in golang, you can define functions as fields in a struct. this feature allows you to bind behavior (methods) directly to data types.
  • Date time functions in ExcelDate time functions in Excel
    excel supports you to process and calculate quickly with the functions that excel provides such as calculation functions, date functions ... one of them is the function of time to help you handle the prices. time value: hour, minute, second conveniently
  • The MIN and MAX functions (the smallest and largest value functions) in ExcelThe MIN and MAX functions (the smallest and largest value functions) in Excel
    the function to return the maximum value (max) and the function to return the minimum value (min) are two common statistical functions used by a lot of people in the process of manipulating and processing data in excel.
  • 8 little-known Excel functions that can save you a lot of work8 little-known Excel functions that can save you a lot of work
    even seasoned excel users often find themselves stuck performing tasks manually that could be automated with a few clever functions.
  • Python functions are user definedPython functions are user defined
    besides the built-in python functions, you can also define python functions yourself, these functions are called user-defined functions (python). what are the benefits of using these self-defined functions, the best way to define functions in python, we'll learn in this python lesson.
  • Tips for working with functions in ExcelTips for working with functions in Excel
    functions that work with excel functions functions are predefined formulas for performing specific calculations, or for manipulating spreadsheets.
  • 8 useful functions in Google Sheets you may not know8 useful functions in Google Sheets you may not know
    in this article, i will show 10 useful functions for google sheets spreadsheets that you should know such as vlookup, countif, sumif, join, index,... in addition, you can refer to these functions. and other tips follow the link in the article.
  • Logical functions (logical) in ExcelLogical functions (logical) in Excel
    logical functions are used a lot during data processing in excel. the article summarizes the syntax and functions of functions in logical function groups in excel.
  • Common Excel functions you need to know about accountingCommon Excel functions you need to know about accounting
    mastering the common excel functions helps accountants save time and effort compared to conventional manual calculations. today, software tips will list and guide readers on how to use some functions commonly used in accounting.