What is the way to write php function?
Mar 18, 2021 am 09:09 AMThe writing method of php function is "<?php function functionName(){...}". The name of the function should indicate its function, and the function name should start with a letter or an underscore.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
PHP function
The real power of PHP comes from its functions.
In PHP, more than 1000 built-in functions are provided.
PHP Built-in Functions
For a complete reference manual and examples of all array functions, please visit our PHP Reference Manual.
PHP Functions
In this chapter, we will explain how to create your own functions.
To execute a script when the page loads, you can put it in a function.
Functions are executed by calling functions.
You can call functions anywhere on the page.
Creating PHP functions
Functions are executed by calling functions.
Syntax
1 2 3 4 5 6 |
|
PHP function guidelines:
The name of the function should indicate its function
The function name starts with a letter or Starts with an underscore (cannot start with a number)
Example
A simple function that prints my name when called:
Example
1 2 3 4 5 6 7 8 9 |
|
Output:
1 |
|
PHP Function - Add Parameters
In order to add more functions to the function, we can add parameters, which are similar to variables.
The parameters are specified within a bracket after the function name.
Example 1
The following example will output different first names, but the same last name:
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Output:
1 2 3 |
|
Example 2
The following function has two parameters:
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Output:
1 2 3 |
|
PHP function - return value
If you need the function to return a value, please use the return statement.
Example
1 2 3 4 5 6 7 8 9 |
|
Output:
1 |
|
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the way to write php function?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
