Home  >  Article  >  Backend Development  >  How to add functions in php

How to add functions in php

藏色散人
藏色散人Original
2021-11-12 09:07:152298browse

How to add a function in php: 1. Create a PHP sample file; 2. Create a function through "function functionName(){...}".

How to add functions in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to add functions to php?

Creating PHP functions:

Functions are executed by calling functions.

Syntax

<?php
function functionName()
{
    // 要执行的代码
}
?>

PHP function guidelines:

The name of the function should indicate its function

The function name starts with a letter or underscore (not a number Beginning)

Example

A simple function that outputs my name when called:

Example

<?php
function writeName()
{
    echo "Kai Jim Refsnes";
}
 
echo "My name is ";
writeName();
?>

Output:

My name is Kai Jim Refsnes

PHP Function - Adding 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.

The following example will output different names, but the surname is the same:

Example

<?php
function writeName($fname)
{
    echo $fname . " Refsnes.<br>";
}
 
echo "My name is ";
writeName("Kai Jim");
echo "My sister&#39;s name is ";
writeName("Hege");
echo "My brother&#39;s name is ";
writeName("Stale");
?>

Output:

My name is Kai Jim Refsnes.
My sister&#39;s name is Hege Refsnes.
My brother&#39;s name is Stale Refsnes.

Recommended learning:《PHP video tutorial

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

Statement:
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