search
HomeBackend DevelopmentPHP TutorialPHP function regular parameter function and pseudo-type parameter function

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

php常规参数函数

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

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

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

在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)            // 必须使用一个资源类型作为参数

php伪类型参数的函数

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

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

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

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
?>

说明:这里需要注意的是,参数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
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
How can you check if a PHP session has already started?How can you check if a PHP session has already started?Apr 30, 2025 am 12:20 AM

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Describe a scenario where using sessions is essential in a web application.Describe a scenario where using sessions is essential in a web application.Apr 30, 2025 am 12:16 AM

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

How can you manage concurrent session access in PHP?How can you manage concurrent session access in PHP?Apr 30, 2025 am 12:11 AM

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

See all articles

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 Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.