Home > Backend Development > PHP Tutorial > How to Skip Optional Arguments in PHP Function Calls?

How to Skip Optional Arguments in PHP Function Calls?

DDD
Release: 2024-11-23 07:20:12
Original
643 people have browsed it

How to Skip Optional Arguments in PHP Function Calls?

Skipping Optional Arguments in PHP Function Calls

When constructing a function with optional parameters, it's desirable to know how to skip these arguments and assign them their default values. Consider the example below:

function getData($name, $limit = '50', $page = '1') {
    ...
}
Copy after login

To call this function and intentionally skip the $limit parameter while specifying the $page parameter, the following syntax can be used:

getData('some name', '', '23');
Copy after login

The empty string '' acts as a placeholder for the skipped $limit parameter, preserving its order in the function call. As specified in the function declaration, $limit will be assigned the default value '50'.

However, it's worth noting that if an optional parameter is declared at the end of the parameter list, it's not possible to skip over it without specifying the preceding parameters. To work around this limitation, you can assign them default values instead and check for their presence within the function itself.

The above is the detailed content of How to Skip Optional Arguments in PHP Function Calls?. For more information, please follow other related articles on the PHP Chinese website!

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