PHP custom function official documentation

WBOY
Release: 2016-07-29 09:16:09
Original
860 people have browsed it

A function can be defined by the following syntax:

Example #1 Pseudocode showing the purpose of the function

<span><span><?php<br></span><span>function </span><span>foo</span><span>(</span><span>$arg_1</span><span> , </span> <span>$arg_2</span><span>, </span><span>/* ..., */ </span><span>$arg_n</span><span>)<br>{<br> echo </span><span>"Example function.n"</span><span>;<br> return </span><span>$ret val</span><span>;<br>}<br> </span><span>?></span></span>

Any valid PHP code may appear inside a function, even other function and class definitions.

Function names have the same naming rules as other identifiers in PHP. Valid function names begin with a letter or underscore, followed by letters, numbers, or underscores. It can be expressed as a regular expression: [a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*.

Tip

Function does not need to be defined before calling, unlessin the following two examples when the function is conditionally defined.

When a function is conditionally defined, it must be defined before calling the function .

Example #2 Conditional function

<span><span><?php<br>$makefoo </span><span>= </span><span>true</span><span>;<br></span><span>/* The foo() function cannot be called here, <br> because it It doesn't exist yet, but the bar() function can be called. */<br></span><span>bar</span><span>();<br>if (</span><span>$makefoo</span><span>) {<br> function </span><span>foo</span><span>()<br> {<br> echo </span><span>"I don't exist until program execution reaches me.n "</span><span>;<br> }<br>}<br></span><span>/* It is now safe to call function foo(), <br> because $makefoo is true */<br></span><span>if (</span><span>$makefoo</span><span>) </span><span>foo</span> <span>( );<br>function </span><span>bar</span><span>()<br>{<br> echo </span><span>"I exist immediately upon program start.n"</span><span>;<br>}<br></span><span>?></span></span>

Example #3 Function Function in

<span><span><?php<br></span><span>function </span><span>foo</span><span>()<br>{<br> function </span><span>bar</span><span>()<br> {<br> echo </span><span>"I don't exist until foo( ) is called.n"</span><span>;<br> }<br>}<br></span><span>/* You can’t call the bar() function now because it doesn’t exist yet */<br></span><span>foo</span><span>();<br></span><span>/* You can now The bar() function is called, because the execution of the foo() function <br> makes the bar() function a defined function */<br></span><span>bar</span><span>();<br></span><span>?></span></span>

All functions and classes in PHP have global scope and can be defined within a function and called outside it, and vice versa.

PHP does not support function overloading, and it is not possible to undefine or redefine declared functions.

Note: Function names are case-insensitive, but when calling a function, it is a good habit to use the same form as when it was defined.

You can call recursive functions in PHP.

Example #4 Recursive function

<span><span><?php<br></span><span>function </span><span>recursion</span><span>(</span><span>$a</span><span>)<br>{<br>         if ( </span><span>$a </span><span>< </span><span> 20. </span><span>1<br></span>);<span>  }</span>}<span></span><span>? ></span><span><br></span><span></span><span></span>Note: <span>But avoid recursive function/method calls beyond 100-200 levels as it may crash the stack and terminate the current script. Infinite recursion can be considered a programming error. </span><span></span><span></span>Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. <span><br> <br> The above introduces the official documentation of PHP custom functions, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials. <br> </span> <span></span></span>

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!