Home > Backend Development > PHP Tutorial > How to create a function in php function?

How to create a function in php function?

醉折花枝作酒筹
Release: 2023-04-10 12:24:01
Original
6786 people have browsed it

This article will start a new chapter about functions. Everything we want to execute cannot be separated from functions. Today we will learn the simplest one, which is to create functions. Only when the function is created can it be called. Come and learn.

What we want to learn is a function, so how can we not know what a function is? Let's first look at what a function is.

Functions are usually divided into traditional definitions and modern definitions. In fact, the two definitions are essentially the same, but their starting points are different. The traditional definition starts from the perspective of motion change, while the modern definition starts from the perspective of collection and mapping. Set off. The modern definition of a function is: Suppose there is a number set A in which the element is The equivalent relationship with x can be expressed by y=f(x). A function has three elements: domain, range, and corresponding rules. The core is the correspondence law, which is the essential feature of functional relationships.

After we understand what a function is, let’s take a look at how to create it.

In PHP, in addition to the built-in functions, we can also create functions according to our own needs. Functions are blocks of statements that can be used repeatedly in a program. The function does not execute immediately when the page loads. Functions only execute when called.

After we understand this information, let's create the function.

function functionName() {
  被执行的代码;
}
Copy after login

Suddenly when I saw function, did I feel confused? Am I really learning PHP? Why is it so special like javascript?

No, no, I didn’t make a typo. In php, function is used to create functions. Feel free to use it.

But when using function to create a function, you need to pay attention:

  • The function name can start with a letter or underscore (not a number).

  • Function names are not case sensitive.

  • The function name should reflect the task performed by the function.

Only under these conditions can we create a function.

Just saying it may not be so impressive, so let’s look at a small example, and friends can also follow along and write it down.

<?php
function sayhi() {
   echo "Hello world!";
}

sayhi();
?>
Copy after login

Let’s look at this function, what does it want to do? Does it output "Hello world!" when called? Does this function realize its function? Feel free to let us see the answer.

Hello world!
Copy after login

We can see that this function has achieved its function, indicating that our function was successfully created. That’s awesome, give yourself a round of applause.

All the PHP knowledge you want is here→php video tutorial

The above is the detailed content of How to create a function in php function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template