1

I'm using PHP 7.3.2 on my laptop that runs on Windows 10 Home Single Language 64-bit operating system.

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

Please do consider below prototype definition of the built-in PHP function htmlspecialchars() from the PHP Manual:

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 i.e. $double_encode with a boolean value FALSE and keep all other optional parameters set to their default values.

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.

Then why I'm not able to get the output and getting some warning? Why? What mistake I'm making in my code?

Dharman
  • 30,962
  • 25
  • 85
  • 135
PHPLover
  • 1
  • 51
  • 158
  • 311

1 Answers1

3

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);
Rarst
  • 2,335
  • 16
  • 26
  • If such is the case then why I'm getting the same output upon setting the optional parameter `double_encode` to `FALSE` and `TRUE`. The results should have to be different for the value `TRUE` and for the value `FALSE`. **Why it's not happening then?** In both cases I'm getting the same output as `&amp` – PHPLover Mar 03 '19 at 14:11
  • For your specific example this argument is irrelevant. You might observe the difference if you use `&` (your string misses `;`). – Rarst Mar 03 '19 at 14:14
  • Yes, you are correct. I missed the semicolon at the end. Thanks for your help bro. – PHPLover Mar 03 '19 at 14:16
  • Do I need to reproduce all the remaining defaults whenever I want to pass a customized optional argument or more than one customized arguments? If yes, then let me know is this applicable to only built-in PHP functions or to both the user-defined and built-in PHP functions? Thank You. – PHPLover Mar 04 '19 at 09:30
  • You have to reproduce anything _before_ your customized arguments, anything _after_ will fall back to defaults natively. For user defined function the behavior is up to implementation, but generally you will have to fill in preceding defaults as well. – Rarst Mar 04 '19 at 10:42