简体   繁体   English

如何通过使所有其他强制/可选参数(如果有的话)保持默认值,仅将可选参数传递给PHP函数?

[英]How to only pass an optional parameter to a PHP function by keeping all other mandatory/optional parameters(if any) equal to their default values?

I'm using PHP 7.3.2 on my laptop that runs on Windows 10 Home Single Language 64-bit operating system . 我在运行Windows 10家庭单一语言64位操作系统的笔记本电脑上使用PHP 7.3.2

I've installed the latest version of XAMPP installer on my laptop which has installed the Apache/2.4.38 (Win32) and PHP 7.3.2 我已经在笔记本电脑上安装了最新版本的XAMPP安装程序,该安装程序已经安装了Apache / 2.4.38(Win32)PHP 7.3.2。

Please do consider below prototype definition of the built-in PHP function htmlspecialchars() from the PHP Manual : 请考虑以下来自PHP手册的内置PHP函数htmlspecialchars()原型定义:

htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = TRUE ]]] ) : string

Now, I only want to set the last optional parameter ie $double_encode with a boolean value FALSE and keep all other optional parameters set to their default values. 现在,我只想将最后一个可选参数(即$double_encode设置为布尔值 FALSE ,并将所有其他可选参数设置为其默认值。

For achieving this I tried below code : 为了实现这一点,我尝试了以下代码:

<?php
  echo htmlspecialchars('&amp', '', '', FALSE);
?>

After executing the above code I got following output in my web browser : 执行上述代码后,我在网络浏览器中得到以下输出:

Warning: htmlspecialchars() expects parameter 2 to be int, string given in th67i.php on line 2

I'm not understanding where I'm going wrong. 我不明白我要去哪里错了。 I took care of other parameters to be set to their default values by keeping blank spaces separated by commas and only assigning the optional parameter $double_encode to boolean value FALSE . 我将其他参数设置为默认值,方法是将空格用逗号隔开,并且仅将可选参数$double_encode分配给布尔值 FALSE

Then why I'm not able to get the output and getting some warning? 那为什么我不能得到输出并得到一些警告呢? Why? 为什么? What mistake I'm making in my code? 我在代码中犯了什么错误?

Please help me out from this problem. 请帮我解决这个问题。

Thanks in advance. 提前致谢。

Passing '' does not mean falling back to default argument value. 传递''并不意味着退回到默认参数值。 It means just that — trying to pass an empty string. 这意味着-尝试传递一个空字符串。

You would need to reproduce defaults if you want to achieve this: 如果要实现此目的,则需要重现默认值:

htmlspecialchars('&amp', ENT_COMPAT | ENT_HTML401, ini_get('default_charset'), FALSE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM