- PHP 8.1: Passing null to non-nullable internal function parameters is deprecated
- Upgrading null parameters to default values
- Backwards Compatibility Impact
- Php pass null parameter
- Php funtion pass string or null in php
- Pass by null reference in PHP
- Is passing NULL param exactly the same as passing no param
- Php 8.1 — explode(): Passing null to parameter #2 ($string) of type string is deprecated
- What is the difference between «?» and «= null» in PHP function parameters [duplicate]
PHP 8.1: Passing null to non-nullable internal function parameters is deprecated
In PHP, passing null to a function/method parameter that is not declared nullable is not allowed. Prior to PHP 8.1, this restriction was only applied to user-land functions, and not internal functions.
From PHP 8.1, passing null to a function/method parameter not declared nullable emits a deprecation notice. This is in preparation to remove this functionality to throw a \TypeError exception in a future PHP version as it does already for user-land functions/methods.
For example, strlen function expects the first parameter to be of type string . By definition, it does not accept null values (i.e. not declared as ?string nor string|null union type). For historical reasons, PHP allowed passing null , for these parameters. This behavior is inconsistent from user-land functions/methods because they would have thrown a \TypeError exception if null is passed to a parameter type not declared as nullable.
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in . on line .
This behavior was allowed in the first place for historical reasons. PHP 7.0’s introduction of scalar types did not allow null for parameters unless the parameter was explicitly declared nullable. However, internal functions were not changed to strictly enforce scalar types to ease upgrading to PHP 7.0.
This deprecation notice will likely occur often in legacy code bases that relied on PHP to coerce the default value for a parameter from null .
Upgrading null parameters to default values
Code that uses null in a function/method call in place of the default value will need to replace the null value with the default value from the function declaration.
For example, a strlen(null) call emits a deprecation, and the sensible upgrade to this call would be strlen(») .
To bring another example, preg_split function has a third parameter int $limit = -1 . Passing null to this parameter emits a deprecation notice in PHP 8.1, and the sensible upgrade to this call would be setting $limit parameter to -1 .
Backwards Compatibility Impact
This is a backwards-compatibility breaking change. Updating the null parameters to adhere to the expected type should fix the deprecation notice across all PHP versions.
Further, it may be possible for automated code fixing tools to update said null parameters to the default values from the function declaration.
© 2018-2023 PHP.Watch, with ❤ from Ayesh • About PHP.Watch
Php pass null parameter
To experiment on performance of pass-by-reference and pass-by-value, I used this script. Conclusions are below.
#!/usr/bin/php
function sum ( $array , $max ) < //For Reference, use: "&$array"
$sum = 0 ;
for ( $i = 0 ; $i < 2 ; $i ++)#$array[$i]++; //Uncomment this line to modify the array within the function.
$sum += $array [ $i ];
>
return ( $sum );
>
$max = 1E7 //10 M data points.
$data = range ( 0 , $max , 1 );
$start = microtime ( true );
for ( $x = 0 ; $x < 100 ; $x ++)$sum = sum ( $data , $max );
>
$end = microtime ( true );
echo «Time: » .( $end — $start ). » s\n» ;
/* Run times:
# PASS BY MODIFIED? Time
— ——- ——— —-
1 value no 56 us
2 reference no 58 us
3 valuue yes 129 s
4 reference yes 66 us
1. PHP is already smart about zero-copy / copy-on-write. A function call does NOT copy the data unless it needs to; the data is
only copied on write. That’s why #1 and #2 take similar times, whereas #3 takes 2 million times longer than #4.
[You never need to use &$array to ask the compiler to do a zero-copy optimisation; it can work that out for itself.]
2. You do use &$array to tell the compiler «it is OK for the function to over-write my argument in place, I don’t need the original
any more.» This can make a huge difference to performance when we have large amounts of memory to copy.
(This is the only way it is done in C, arrays are always passed as pointers)
3. The other use of & is as a way to specify where data should be *returned*. (e.g. as used by exec() ).
(This is a C-like way of passing pointers for outputs, whereas PHP functions normally return complex types, or multiple answers
in an array)
5. Sometimes, pass by reference could be at the choice of the caller, NOT the function definitition. PHP doesn’t allow it, but it
would be meaningful for the caller to decide to pass data in as a reference. i.e. «I’m done with the variable, it’s OK to stomp
on it in memory».
*/
?>
Php funtion pass string or null in php
IMHO, the no UIDs were found concept is better represented by an empty array and, if you can expect empty strings as well, they may well be handled together with : Solution 3: Just cast the parameter as string, will convert null to ». Solution 1: First of all, the goes before the type, not after. other than this: Using you are forced to pass something, that can be either or an , infact: where instead using will accept , an , or 0 parameters Solution 2: It’s a PHP 7.1 feature called Nullable Types Both of the lines you wrote are identical.
Pass by null reference in PHP
I would suggest an enum but this is PHP. So this should do it for you:
class YourClass < const DoSomethingSmall = 0; const DoSomethingBig = 1; public function doSomething(&$ids, $actionType) < // can also use a switch here if($actionType == self::DoSomethingSmall) < // do small >else if($actionType == self::DoSomethingBig) < // do big >> >
$this->doSomething($bigIds, self::DoSomethingBig); $this->doSomething($smallIds, self::DoSomethingSmall);
From outside the class you can use YourClass::DoSomethingBig and YourClass::DoSomethingSmall
There could be loads of things you might rather do, for instance what @ad7six says in a comment, and you could also just give it some sort of setting and just one array..
public function doSomething(&$bIds, $mode)
It all depends on what you need really
Pass by null reference in PHP, In PHP I need to pass some arguments to a function by reference. I don’t want to write 2 different methods for similar behaviour. So i need to select behaviour by argument. But I can’t pass null by reference. So I created a dummy array. So i run it either by Code sample$this->doSomething($bigIds, self::DoSomethingBig);$this->doSomething($smallIds, self::DoSomethingSmall);Feedback
Is passing NULL param exactly the same as passing no param
No. Presumably, the exact signature is:
function afunc($p1, $p2, $p3, $p4 = SOM_CONST)
and the function uses func_get_arg or func_get_args for $p5 .
func_num_args will be different depending whether you pass NULL. If we do:
It’s up to the function whether to handle these the same.
No. It is only the same if the parameter’s default value is NULL , otherwise not:
function a($a, $b, $c = 42) < var_dump($c); >a(1, 2, NULL);
So you cannot say it is always the same. NULL is just another value and it is not that special.
Not to mention non-optional parameters: Calling a(1) would give you an error (warning) but a(1,NULL) works.
Passing NULL is exactly the same as not passing an argument — if the default value is NULL , except in the context of the func_num_args function:
Returns the number of arguments passed to the function.
test(); //outputs: 0 test(NULL); //outputs: 1 ?>
Cannot pass null argument when using type hinting, PHP 7.0 or older You have to add a default value like function foo (Type $t = null) < >That way, you can pass it a null value. This is documented in the section in the manual about Type Declarations: The declaration can be made to accept NULL values if the default value of the parameter is set to NULL. Share …
Php 8.1 — explode(): Passing null to parameter #2 ($string) of type string is deprecated
Use the null coalescing operator to default the value to an empty string if the value is null .
$comp_uids = explode(',', $result['comp_uids'] ?? '');
There’s no need to explode null , you know beforehand that it won’t return matches. If you really want [»] as result, it’s more intuitive to do it explicit:
$comp_uids = $result['comp_uids'] !== null ? explode(',', $result['comp_uids']) : [''];
I still find this a bit counter-intuitive for your fellow programmers. IMHO, the no UIDs were found concept is better represented by an empty array and, if you can expect empty strings as well, they may well be handled together with null :
$comp_uids = $result['comp_uids'] != '' ? explode(',', $result['comp_uids']) : [];
Just cast the parameter as string, will convert null to ».
$comp_uids = explode(',', (string)$result['comp_uids']);
PHP str_replace(): Passing null to parameter #3, PHP 7 and lower just would ignore the missing parameter, while PHP 8+ displays the warning. It may also vary with your environment / debug settings. It may also vary with your environment / debug settings.
What is the difference between «?» and «= null» in PHP function parameters [duplicate]
First of all, the ? goes before the type, not after. other than this:
public function foo(?array $optionalParam);
you are forced to pass something, that can be either null or an array , infact:
foo(); // doesn't work foo(null); // works foo([]); // works
public function foo(array $optionalParam = null);
will accept null , an array , or 0 parameters
foo(null); // works foo(); // work foo([]); // works
It’s a PHP 7.1 feature called Nullable Types
Both of the lines you wrote are identical.
array ?$optionalParam : either an array or null array $optionalParam = null : either an array or null
Tho using ? you’d still need to add the parameter when calling the function.
Php — Is passing NULL param exactly the same as, It is highly recommended in PHP to keep the optional parameters after all the non optional parameters. At that point, if you need to skip some of the optional parameters, you can provide NULL as their values and the function will use their default value. It will be similar to using the null coalesce operator in $x = $x ?? 12.