This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 4 years ago.
I just saw this in a Symfony 4 application, and I can't find nowhere what it means
public function findOneBySomeField($value): ?Article
{
return $this->createQueryBuilder('a')
->andWhere('a.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
I know that, now with PHP 7 you can define the expected type of the returned value with ":int $val", but here, what does the ? symbol means ?
This is a new feature as of PHP 7.1. See the explanation here
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 the expected output of your function will be either an Instance of the class Article or it is NULL.
Related
This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 1 year ago.
In php, what is the difference between the two following variable declarations, both for procedural mode and inside a class:
string $str; // without ? before the type
?string $str; // with ? before the type
Thank you for your lights
Variable $str can only be of type string:
string $str;
Variable $str can be of type string or null:
?string $str;
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
This question already has answers here:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass
This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I'm using a CMS package written in PHP. In one of it's core files I saw following line that is for defining a function in a class body.
public static function &getLib($sClass, $aParams = array()) {
// Code
}
I didn't understand why the function name 'getLib' has been prepended with the ampersand(&) sign? I've never seen such thing before.
Can someone please explain me in detail why such thing has been done and what's the benefit it has over simply using the function name?
It means the function should return a reference to a variable rather than just the value itself.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.