函数 (有些资料会将此翻译成过程,所谓的面向过程开发就是以 函数 为基础的开发)在面向对象开发(简称OO--Object-Oriented Method)时代之前,是PHP中最重要的一个部分,是代码复用唯一的方法。在OO出现之后(虽然早期PHP版本就已经有OO的身影,但真正引入
函数(有些资料会将此翻译成过程,所谓的面向过程开发就是以函数为基础的开发)在面向对象开发(简称OO--Object-Oriented Method)时代之前,是PHP中最重要的一个部分,是代码复用唯一的方法。在OO出现之后(虽然早期PHP版本就已经有OO的身影,但真正引入面向对象主要特性是在PHP5版本之后)两种方法常常会并存于一个项目中,这点和很多面向对象语言(JAVA或ASP.NET等)不太一样。其原因我个人认为原因有二:其一是由于PHP在运行函数时速度要优于面向对象(网友的速度测试),而面向对象又有其良好扩展性及实用性的优势;其二是很多项目起始于面向过程的时代,在版本更新时并没有完全去除能够显著提高速度而不影响可读性的部分面向过程的代码。
设置函数的默认值:
function wrap_html_tag($string, $tag = 'b') { return "$string$TAG>"; }
传递一个变量引用:
function wrap_html_tag(&$string, $tag = 'b') { $string = "$string$TAG>"; }
指定多个参数默认值的另一种方法:
function pc_assign_defaults($array, $defaults) { $a = array(); foreach ($defaults as $d => $v) { $a[$d] = isset($array[$d]) ? $array[$d] : $v; } return $a; } function image($img) { $defaults = array('src' => 'cow.png', 'alt' => 'milk factory', 'height' => 100, 'width' => 50 ); $img = pc_assign_defaults($img, $defaults); ... }
传递可变个数的参数:
//$numbers是可变个数 function mean($numbers) { // 初始化以消除警告 $sum = 0; // 统计参数的个数 $size = count($numbers); // 迭代数组中的元素并计算总和 for ($i = 0; $i <h4 id="返回变量的引用">返回变量的引用:</h4> <p>可以理解为return后面的值与调用<strong>函数</strong>的等号左边的变量指向的是同一个东东(内存地址)</p> <pre class="brush:php;toolbar:false">function &pc_array_find_value($needle, &$haystack) { foreach ($haystack as $key => $value) { if ($needle == $value) { return $haystack[$key]; } } } $minnesota = array('Bob Dylan', 'F. Scott Fitzgerald', 'Prince', 'Charles Schultz'); $prince =& pc_array_find_value('Prince', $minnesota); $prince = 'O(+>'; print_r($minnesota); Array ( [0] => Bob Dylan [1] => F. Scott Fitzgerald [2] => O(+> [3] => Charles Schultz )
返回多个值:
function averages($stats) { ... return array($median, $mean, $mode); } list($median, $mean, $mode) = averages($stats);
调用可变函数
(根据参数调用不同的函数,call_user_func_array()):
function get_file($filename) { return file_get_contents($filename); } function put_file($filename, $data) { return file_put_contents($filename, $data); } if ($action == 'get') { $function = 'get_file'; $args = array('graphic.png'); } elseif ($action == 'put') { $function = 'put_file'; $args = array('graphic.png', $graphic); } // calls get_file('graphic.png') // calls put_file('graphic.png', $graphic) call_user_func_array($function, $args);
动态函数(create_function()):
$add = create_function('$i,$j', 'return $i+$j;'); $add(1, 1); // returns 2

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

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.

SublimeText3 Mac version
God-level code editing software (SublimeText3)