What is the difference between "?" and "= null" in PHP function parameters [duplicate] - php

This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Closed 1 year ago.
I was doing some research but wasn't able to find an answer (probably beacause I did not searched it right)
Consider this piece of code:
public function foo(?array $optionalParam);
And then this one:
public function foo(array $optionalParam = null);
What differs between them? Using PHPstorm I noticed that when I use the ?, it creates a PHPdoc and mark the variable type as type|null. But when I call the function without that argument, PHP screams on my face "you kidding me? where is $optionalParam". In the other side, I managed to use with no problems the =null option.
Sorry if this question is too simple, but i did not find any answers online.

First of all, the ? goes before the type, not after... other than this:
Using
public function foo(?array $optionalParam);
you are forced to pass something, that can be either null or an array, infact:
<?php
function foo(?array $optionalParam){
echo "test";
}
foo(); // doesn't work
foo(null); // works
foo([]); // works
where instead using
public function foo(array $optionalParam = null);
will accept null, an array, or 0 parameters
<?php
function foo(array $optionalParam = null){
echo "test";
}
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.

Related

PHP question mark operator (not a ternary operator) [duplicate]

This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Closed 4 years ago.
I ran into a code in PHP, which has strange question mark operators. Since the code itself doesn't have any comments on that part, i tried to google it, but failed to succeed.
The code i'm interested in goes like:
<?php
class Cart
{
private $_user;
private $_items = [];
public function __construct(?User $user)
{
$this->_user = $user;
}
public function getUser(): ?User
{
return $this->_user;
}
My first thought was of type hinting, which was introduced in PHP 7, but it's apparently not the case, nor it is the strict type declaration. I've got no idea, what it is. Can you help me?
http://php.net/manual/en/migration71.new-features.php
Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.
This means your parameter or return can be null.
PHP 7.1 introduced it : http://php.net/manual/en/migration71.new-features.php

return NULL; vs. return; [duplicate]

This question already has answers here:
Should a function use: return null;?
(7 answers)
Closed 7 years ago.
I wonder if there are any differences in PHP.
Lets assume I have the following function(s)
public function myFunc() {
// some logic here
return;
}
and this here:
public function myFunc2() {
// some more logic here
return null;
}
I understand, that returning "" (an empty String) is something different than null. A var_dump() on each of these functions returns NULL. Is this internally (bitwise or for some comparison) somehow handled differently?
Does it affect the parsing-time? Is it just a good practice to write return NULL or is it more like a convention?
I didn't studied source code of PHP, but as a developer I'm using return null when function should return a value (by design) and simple return when I need to just leave a function which is not returning any value.

What is the difference in the code [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
The below code is for sanitizing the posted values. Can some tell me What is the difference between,
<?php
function sanitize_data(&$value, $key) {
$value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>
and
<?php
function sanitize_data($value, $key) {
$value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>
Thanks
The first uses value as a refrence, so any time you call it with some variable the variable will be changed in the outer scope, not only in the function itself.
Look in the manual for 'reference' if you want more info.
It's called 'pass by reference'. &$value will relate to the original $value passed into the function by pointer, rather than working on a function version.
Please see the PHP Manual.
The first function the value of the first parameter is passed by reference and in the second not. If the variable is passed by referenced, changes to it will also be done on the value outside of the function scope (in the scope you call the function).
Also read the PHP documentation (pass by reference) and is also demonstrated on the array_walk doc page.
First method is called as "Passing value as reference".
So $_POST array values are changed .
In second method will not change the value of $_POST
You can check SO Link: Great Explanation about it.
https://stackoverflow.com/a/2157816/270037
The first function gets $value passed by reference so it can modify it directly, the second function gets passed $value's value.

Can I immediately evaluate an anonymous function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Immediately executing anonymous functions
I want to immediately evaluate an anonymous function rather than it appearing as a Closure object in method args. Is this possible?
For example:
$obj = MyClass;
$obj->Foo(function(){return "bar";}); // passes a Closure into Foo()
$obj->Foo(function(){return "bar";}()); // passes the string "bar" into Foo()?
The 3rd line is illegal syntax -- is there any way to do this?
Thanks
You could do it with call_user_func ... though that might be a bit silly when you could just assign it to a variable and subsequently invoke the variable.
call_user_func(function(){ echo "bar"; });
You might think that PHP 5.4 with it's dereferencing capabilities would make this possible. You'd be wrong, however (as of RC6, anyway).

Unlimited arguments for PHP function? [duplicate]

This question already has answers here:
PHP function with unlimited number of parameters
(8 answers)
Closed 2 years ago.
In php how would you create a function that could take an unlimited number of parameters: myFunc($p1, $p2, $p3, $p4, $p5...);
My next question is: how would you pass them into another function something like
function myFunc($params){
anotherFunc($params);
}
but the anotherFunc would receive them as if it was called using anotherFunc($p1, $p2, $p3, $p4, $p5...)
call_user_func_array('anotherFunc', func_get_args());
func_get_args returns an array containing all arguments passed to the function it was called from, and call_user_func_array calls a given function, passing it an array of arguments.
Previously you should have used func_get_args(), but in a new php 5.6 (currently in beta), you can use ... operator instead of using .
So for example you can write :
function myFunc(...$el){
var_dump($el);
}
and $el will have all the elements you will pass.
Is there a reason why you couldn't use 1 function argument and pass all the info through as an array?
Check out the documentation for variable-length argument lists for PHP.
The second part of your question refers to variable functions:
...if a variable name has parentheses appended to it, PHP will look for a function with the
same name as whatever the variable evaluates to, and will attempt to execute it.

Categories