search
HomeheadlinesDeclaration and use of php functions

* Function

* 1. Declaration syntax;

* 2. Calling method;

* 3. Parameter setting;

* 4. Return value;

* 5. Scope

//Declaration

function hello() //无论有无参数,圆括号不能省略
{
    echo '欢迎来到php中文网学习';
}

//Call: call by name, must bring parentheses

hello();
echo &#39;<hr>&#39;;
//可以设置参数
function hello1($siteName)
{
    echo &#39;欢迎来到&#39;.$siteName.&#39;学习&#39;;
}

//You must bring parameters when calling now

hello1(&#39;php中文网&#39;);
hello1(&#39;m.sbmmt.com&#39;);

//If you forget to give parameters when calling, you can give the function parameters a default value

function hello2($siteName = &#39;php中文网&#39;)
{
    echo &#39;欢迎来到&#39;.$siteName.&#39;学习&#39;;
}
echo &#39;<hr>&#39;;

//You can pass parameters when calling now , you can also pass no parameters

hello2();
echo &#39;<br>&#39;;
hello2(&#39;PHP中文网_m.sbmmt.com&#39;);

//If there are multiple parameters, the default value should be written to the end

function hello3($name ,$siteName = &#39;php中文网&#39;)
{
    echo &#39;我是&#39;.$name.&#39;,欢迎来到&#39;.$siteName.&#39;学习&#39;;
}
echo &#39;<hr>&#39;;

//Calling method

hello3(&#39;peter zhu&#39;); //第一个参数没有默认值,必须传参
echo &#39;<br>&#39;;
hello3(&#39;peter zhu&#39;, &#39;m.sbmmt.com&#39;); //实参与形参的位置必须一一对应
echo &#39;<hr>&#39;;

//Scope : Function internal variables cannot be accessed from the outside. Similarly, external variables cannot be accessed from inside the function.

$siteName = &#39;php中文网&#39;;

//External variables, or global variables, will automatically become an element in the global variable array $GLOBALS. The variable is The key name of the element

echo $GLOBALS[&#39;siteName&#39;]; //它里面的数据就是变量siteName中的值,而且这是一个超全局变量,可以在函数中使用
function hello4()
{
    $name = &#39;peter zhu&#39;;
//    return $siteName;
    return $GLOBALS[&#39;siteName&#39;];
}
echo hello4();
echo &#39;<br>&#39;;
echo $name; //外部是访问不到函数内的变量的,除非函数将这个变量返回到外部
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.