Php double question mark

What Does Double Question Mark () Operator Mean in PHP

What does double question mark (??) operator mean in PHP

It’s the «null coalescing operator», added in php 7.0. The definition of how it works is:

It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

So it’s actually just isset() in a handy operator.

Those two are equivalent 1 :

$foo = $bar ?? 'something';
$foo = isset($bar) ? $bar : 'something';

In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

Читайте также:  Html javascript php mysql джентльменский набор web мастера

And original RFC https://wiki.php.net/rfc/isset_ternary

EDIT: As this answer gets a lot of views, little clarification:

1 There is a difference: In case of ?? , the first expression is evaluated only once, as opposed to ? : , where the expression is first evaluated in the condition section, then the second time in the «answer» section.

What are the ?? double question marks in Dart?

The ?? double question mark operator means «if null». Take the following expression, for example.

This means a equals b , but if b is null then a equals ‘hello’ .

Another related operator is ??= . For example:

This means if b is null then set it equal to hello . Otherwise, don’t change it.

The Dart 1.12 release news collectively referred to the following as null-aware operators:

  • ?? — if null operator
  • ??= — null-aware assignment
  • x?.p — null-aware access
  • x?.m() — null-aware method invocation

Try Catch using double question mark(??) in laravel view(blade) does’nt work

The double question mark, also called null coalescing operator is, in your case, ran after the == operation. Soo if you want to achieve this, you need to put parenthesis like that:
($clr->id == ($data->classroom??»))[. ]

PHP syntax question: What does the question mark and colon mean?

This is the PHP ternary operator (also known as a conditional operator) — if first operand evaluates true, evaluate as second operand, else evaluate as third operand.

Think of it as an «if» statement you can use in expressions. Can be very useful in making concise assignments that depend on some condition, e.g.

$param = isset($_GET['param']) ? $_GET['param'] : 'default';

There’s also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise. For example:

It is worth mentioning that the above code when using i.e. $_GET or $_POST variable will throw undefined index notice and to prevent that we need to use a longer version, with isset or a null coalescing operator which is introduced in PHP7:

$param = $_GET['param'] ?? 'default';

php — Meaning of question mark colon operator

It is a shorthand for an if statement.

$username = $_COOKIE['user'] ?: getusername($_COOKIE['user']);
if( $_COOKIE['user'] ) 
$username = $_COOKIE['user'];
>
else
$username = getusername($_COOKIE['user']);
>

see test suite here: https://3v4l.org/6XMc4

But in this example, the function ‘getusername’ probably doesn’t work correct, because it hits the else only when $_COOKIE[‘user’] is empty . So, the parameter inside getusername() is also kind of empty.

PHP: is there a good syntax to initialize an array element depending on whether it is undefined or not, without using if?

The array_filter() method only returns the non-empty values from an array by default.

This code shows the various outcomes:

$bar1=1;
$fooArr = [$bar1, $bar2 ?? null];
print_r($fooArr);

$bar2=2;
$fooArr = [$bar1, $bar2 ?? null];
print_r($fooArr);

unset($bar1,$bar2);

$bar1=1;
$fooArr = array_filter([$bar1, $bar2 ?? null]);
print_r($fooArr);

$bar2=2;
$fooArr = array_filter([$bar1, $bar2 ?? null]);
print_r($fooArr);

What does the question mark as well as the colon (:) do in php, particularly in this line?

This is called a ternary operator. It is presented in this form :

condition ? value_if_condition_true : value_if_condition_false;

For example you can use the result of this expression for an assignment, let’s say :

$load_page = is_user_logged_in() ? true : false;

In fact, this is the equivalent of writing :

 if (is_user_logged_in()) 
$load_page = true;
else
$load_page = false;

EDIT Because I love the ternary operator, I want to write more.
This operator form has no effect on performance or behaviour compared to the classic else/if format, and only serves the purpose of being an elegant one liner. (Which is the reason why some languages don’t implement it, taking the view that it is unnecessary and often bad practice to have N ways of writing the same instruction)

But to show how elegant it is, imagine you know the age of a user, and the age_of_subscription to your website, and you want to display a message depending on these two variables according to these rules :

  • if the user is older than 20 and had a subscription for more than 3 year, he is allowed to a discount
  • if the user is younger than 20 and had a subscription for more than 1 years, he is allowed to a discount
  • in any other case, the user is paying the full price.

In the classic IF/ELSE form, you would write :

if ($age > 20)
$text = "You are above 20 and you have been a user for ";
if ($age_of_subscription > 3)
$text .= "more than 3 years so you are allowed a discount";
>
else
$text .= "less than 3 years so you are paying full price";
>

>
else
$text = "You are below or 20 and you have been a user for ";
if ($age_of_subscription > 1)
$text .= "more than 1 year so you are allowed a discount";
>
else
$text .= "less than 1 year so you are paying full price";
>

>
echo $text;

But now, watch the beauty of ternary operator :

echo 'You are ',
($age > 20 ?
($age_of_subscription > 3 ? 'above 20 and you have been a user for more than 3 years
\ so you are allowed a discount' : 'above 20 and you have been a user for less than 3 years
\ so you are paying full price.')
: ($age_of_subscription > 1 ? 'below or 20 and you have been a user for more than 1 year
\ so you are allowed a discount' : 'below or 20 and you have been a user for less than 1 year
\ so you are paying full price.')); // pretty uh?

Источник

What does double question mark (??) operator mean in PHP ?

PHP 7 has added a new operator double question mark (??) operator. In PHP 7, the double question mark(??) operator known as Null Coalescing Operator.

It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand. It evaluates from left to right. Null Coalescing operator also can be used in a chain format.

Let’s take the below example to demonstrate the double question mark (??) operator.

Example

Output

Example

Output

Alok Prasad

  • Related Articles
  • What does the Double Star operator mean in Python?
  • What is the use of the double question mark “??” in Swift?
  • What does operator ~= mean in Lua?
  • Double not (!!) operator in PHP
  • What does the Star operator mean in Python?
  • What does the two question marks together (??) mean in C#?
  • What Does a Double-Dash in Shell Commands Mean
  • What does [Ss]* mean in regex in PHP?
  • What is the meaning of the question mark «?» in Swift?
  • What is double address operator(&&) in C++?
  • What is the «double tilde» (~~) operator in JavaScript?
  • Which number should replace the question mark?
  • What is the Kotlin double-bang (!!) operator?
  • How to remove question mark from corrplot in R?
  • A comma operator question in C/C++ ?

Источник

Double Question Mark in PHP

Double Question Mark in PHP

  1. Use Double Question Mark as Null Coalescing Operator in PHP
  2. Use The Double Question Mark on The Values From a Form in PHP

The double question mark is called Null Coalescing Operator in PHP. It was introduced in PHP7.

The double question mark returns the value from the operands, which is not Null .

It checks the operands from left to the right and returns the first non-Null value.

The Null Coalescing Operator can be used if there is a need to use a ternary in conjunction; before PHP7, we used PHP built-in function isset() with ?: instead of ?? .

Use Double Question Mark as Null Coalescing Operator in PHP

php $Temp = null; $Demo = $Temp ?? 'Nothing'; echo $Demo."
"
;
$Temp = "Test Double Question Mark"; $Demo = $Temp ?? 'something'; echo $Demo; ?>

The code above will first print “nothing” because the value of $Demo is null, and then it prints the string Test Double Question Mark because the first operand is not null .

Nothing Test Double Question Mark 

Use The Double Question Mark on The Values From a Form in PHP

We can use the Null Coalescing Operator on the form values, so if there is not any value inserted, it can print something else. See example:

 html> body> form action="test.php" method="post"> Test Value 1: input type="text" name="test1">br> Test Value 2: input type="text" name="test2">br> input type="submit">  form>  body>  html> 

This HTML code will ask you to enter values, and those values will be printed on test.php given below.

php echo $_POST["test1"] ?? $_POST["test2"] ?? "Please enter a test value"; ?> 

The code will print the first non-Null value it gets from the form, and if it doesn’t get any value, it will print the output:

Please enter a test value 

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

Related Article — PHP Operator

Источник

PHP double question marks (Null coalescing operator) explained

Several paper question marks on a cardboard.

PHP double question marks ( ?? ) – officially known as Null coalescing operator – is a convenient alternative to ternary expressions in conjunction with isset() .

Woman thinking

Psssst! Do you want to learn web development in 2023?

You might have seen the following expression in your Laravel or PHP projects:

  $result = $value ?? $alternativeValue; 

But what do two question marks mean in PHP?

The above expression returns its first operand ( $value ) if it exists and is not null; otherwise, it returns the second operand ( $alternativeValue ).

The above expression is equivalent to:

  $result = isset($value) ? $value : $alternativeValue;  

How to use PHP double question marks

Before the Null coalescing operator, you’d have to use a ternary operator with a call to isset() :

  $iceCreamFlavor = isset($_POST['flavor']) ? $_POST['flavor'] : 'vanilla';  
  $iceCreamFlavor = 'vanilla'; if (isset($_POST['flavor']))  $iceCreamFlavor = $_POST['flavor']; > 

But with the Null coalescing operator, you can summarize it into:

  $iceCreamFlavor =$_POST['flavor'] ?? 'vanilla'; 

You can also chain multiple operators:

  $display_name = $first_name ?? $last_name ?? 'Anonymous'; 

The above code will return the first defined value among $first_name , $last_name , and Anonymous .

The Null coalescing operator has been added to PHP since version 7.

I hope this quick guide gave you the answer you were looking for.

Reza Lavarian Hey 👋 I’m a software engineer, an author, and an open-source contributor. I enjoy helping people (including myself) decode the complex side of technology. I share my findings on Twitter: @rlavarian

If you read this far, you can tweet to the author to show them you care.

❤️ You might be also interested in:

Never miss a guide like this!

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

Источник

Оцените статью