-3

I am working on laravel project. I have this code.

please explain

what is ? and :

if($request->pre_delivered){
    $grain_sale->undelivered_bushels = ($request->bushels_sold >= $request->pre_delivered ? $request->bushels_sold - $request->pre_delivered : NULl);
    dd($grain_sale->undelivered_bushels);
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42

4 Answers4

0

There are already many posts about the ternary operator ?:, but in short:

$var = condition ? <if true> : <if false>;

is a shorthand for:

if(condition){
    $var = <if true>;
}else{
    $var = <if false>;
}

Have a look at the php manual for more details on the PHP implementation. Here is an extract:

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE. Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

Derlin
  • 9,572
  • 2
  • 32
  • 53
0

It's the ternary operator. If the part before the ? is true, the part right after ? and before : is executed. If the part before ? is false, the part after : is executed.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

It's ternary operator

if condition met then statement before : will execute otherwise after: will execute.

A very easy example to understand:- https://eval.in/771934

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
-1

ternary operator isn't a laravel thing it's a php operator and it's available in many languages too. have a look on this article that describes the difference between ternary and if

M.Elwan
  • 1,904
  • 1
  • 16
  • 21