How to add functions in php

藏色散人
Release: 2023-03-13 19:48:01
Original
2297 people have browsed it

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()
{
    // 要执行的代码
}
?>
Copy after login

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();
?>
Copy after login

Output:

My name is Kai Jim Refsnes
Copy after login

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");
?>
Copy after login

Output:

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

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!

Related labels:
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