Home Backend Development PHP Tutorial PHP function regular parameter function and pseudo-type parameter function

PHP function regular parameter function and pseudo-type parameter function

May 10, 2017 am 11:24 AM

在php函数中,函数的参数才是决定如何成功应用一个函数或是控制一个函数执行行为的标准。参输的设置和应用会有多种方式,学会声明具有不同参数的函数,以及可以成功调用各种形式参输的函数,是学习函数的关键。前面介绍如何声明及应用各种形式的php函数。本节将根据函数参数的特点,介绍其中的两个参数类型:常规参数函数伪类型参数函数。

php常规参数函数

常规参数函数,就是实参和形参应该个数相等、类型一致,像 C 语言或者 JAVA 等强类型语言中的参数使用方法一样。 这类函数的调用比较容易,因为灵活性不大,像强类语言一样要求比较严格(参数个数是固定的,每个参数的类型也是固定的)。

常规参数的函数格式说明类似如下的形式:

example(name, age, height)                // 常规参数的函数格式
Copy after login

在php中,如果声明这样的函数就发挥不了PHP作为弱类型语言的优势。例如,在上面的常规参数的函数语法格式示例中,声明一个名为 example 的函数,函数执行后返回一个字符串类型的值。该函数有三个参数,调用时传递的参数个数和顺序必须一致,并且第一个参数必须是字符串类型,第二个参数必须是整型,第三个参数必须是双精度类型。例如,在自定义函数中求两个整数的平方和函数就是一个常规参数的函数,要求必须有两个整型的参数。系统函数也有很多是这种类型,一些使用常规参数的系统函数如下:

string chr(int ascii)          // 必须使用一个整数作为参数
float ceil(float value)       // 必须使用一个浮点数作为参数
array array_combine(array keys, array values)            // 必须使用两个数组作为参数
int strnatcmp (string str1, string str2)          // 必须使用两个字符串作为参数
string implode(string glue, array pieces)    // 第一个参数必须是字符串,第二个参数必须是数组
string readdir(resource dir_handle)            // 必须使用一个资源类型作为参数
Copy after login

php伪类型参数的函数

PHP是弱类型的语言,不仅在声明变量时不需要指定类型,在声明函数时参数也不需要指定类型,所以在PHP中函数的每个参数都可以为其传递任意类型的值。因为弱类型是 PHP 语言最大的特点,在声明一个函数时,可以让同一个参数接受任意类型的值。而在 C 或者 JAVA 等强类型编程语言中,如果要声明对数组进行排序的方法,就必须为每一种类型的数组写一个排序的方法,这就是所谓的函数重载。而PHP 这类弱类型参数则不存在重载的概念。在PHP中,如果对各种类型的数组进行排序,只要声明一个函数就够了,所以伪类型参数的函数是PHP中最常见的函数应用形式。

伪类型参数的函数格式说明类似如下所示:

mixed funName (mixed $args)    // 在参数列表中出现类型使用 mixed 描述的参数
number funName (number $args)    // 在参数列表中出现类型使用 number 描述的参数
Copy after login

PHP的伪类型,包括 mixed、number 和 callback 三种。在声明函数时,如果参数能够接受多种不同但并不必须是所有类型的值,在函数的说明文档中就可以使用 mixed 标记这个参数类型。如果说明一个参数可以是 整型或者浮点型,就可以使用 number 标记参数。除了参数可以传递伪类型的参数,函数的返回值也可以根据参数类型的不同返回不同类型的值,像 empty()、pow()等都是这样的函数。

例如call_user_func()函数就可接收用户自定义的函数作为一个参数,它是php的一个内置函数。callback函数不仅可以是一个函数,也可以是一个对象的方法,静态类的方法也可以。一个php函数用函数名字符串来传递,可以传递任何内置的或者用户自定义的函数。

下面来一个简单的实例:

<?php
$data = array("name"=>"callback" , "value"=>"test");
$rs1 = http_build_query($data);        //直接调用php函数
$rs2  = call_user_func("http_build_query",$data);    //使用回调函数
echo $rs1;     //name=callback&value=test
echo "<br />";
echo $rs2;     //name=callback&value=test
?>
Copy after login

说明:这里需要注意的是,参数1必须是可使用的函数可以通过function_exists()返回true的函数,这里提醒isset,empty,is_null 等认为的常用函数实际上是一个操作符.并不能算函数。

【相关教程推荐】

1. 《php.cn独孤九贱(4)-php视频教程

2.  php编程从入门到精通全套视频教程

3.  php实战视频教程

The above is the detailed content of PHP function regular parameter function and pseudo-type parameter function. For more information, please follow other related articles on the PHP Chinese website!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles