Home Backend Development PHP Problem What is the way to write php function?

What is the way to write php function?

Mar 18, 2021 am 09:09 AM
php

The 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.

What is the way to write php function?

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

&lt;?php

function functionName()

{

    // 要执行的代码

}

?&gt;

Copy after login

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

&lt;?php

function writeName()

{

    echo &quot;Kai Jim Refsnes&quot;;

}

  

echo &quot;My name is &quot;;

writeName();

?&gt;

Copy after login

Output:

1

My name is Kai Jim Refsnes

Copy after login

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

&lt;?php

function writeName($fname)

{

    echo $fname . &quot; Refsnes.&lt;br&gt;&quot;;

}

  

echo &quot;My name is &quot;;

writeName(&quot;Kai Jim&quot;);

echo &quot;My sister&#39;s name is &quot;;

writeName(&quot;Hege&quot;);

echo &quot;My brother&#39;s name is &quot;;

writeName(&quot;Stale&quot;);

?&gt;

Copy after login

Output:

1

2

3

My name is Kai Jim Refsnes.

My sister&#39;s name is Hege Refsnes.

My brother&#39;s name is Stale Refsnes.

Copy after login

Example 2

The following function has two parameters:

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

&lt;?php

function writeName($fname,$punctuation)

{

    echo $fname . &quot; Refsnes&quot; . $punctuation . &quot;&lt;br&gt;&quot;;

}

  

echo &quot;My name is &quot;;

writeName(&quot;Kai Jim&quot;,&quot;.&quot;);

echo &quot;My sister&#39;s name is &quot;;

writeName(&quot;Hege&quot;,&quot;!&quot;);

echo &quot;My brother&#39;s name is &quot;;

writeName(&quot;Ståle&quot;,&quot;?&quot;);

?&gt;

Copy after login

Output:

1

2

3

My name is Kai Jim Refsnes.

My sister&#39;s name is Hege Refsnes!

My brother&#39;s name is Ståle Refsnes?

Copy after login

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

&lt;?php

function add($x,$y)

{

    $total=$x+$y;

    return $total;

}

  

echo &quot;1 + 16 = &quot; . add(1,16);

?&gt;

Copy after login

Output:

1

1 + 16 = 17

Copy after login

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles