-2

I have tried like

         foreach ($this->Bay as $k => $obj) {
        $obj->{'BayId'} = ($obj->{'BayId'}=='') ? new MongoDB\BSON\ObjectID(); $obj->{'IsDeleted'} = "No"; : new MongoDB\BSON\ObjectID($obj->{'BayId'});

    }

if this condition ($obj->{'BayId'}=='') is true, there are two statemenrs which needs to be done.

The above code is throwing error... Please help!!!

The question is not related to parsing error. The question is that if BayId does not contain anything it should be able to overwrite array element with $obj->{'IsDeleted'} = "No"; else it should not do anything

I tried like this also

$obj->{'BayId'} = ($obj->{'BayId'}=='') ? $obj->{'IsDeleted'} = "No";

it does not work

Nida Amin
  • 735
  • 1
  • 8
  • 28
  • >Parse error: syntax error, unexpected ';' in – Nida Amin Nov 03 '18 at 15:37
  • From https://stackoverflow.com/questions/18349750/multiple-statements-in-ternary-operator it seems like you could use `AND` but using an `if` `else` would be more readable IMO – L. Faros Nov 03 '18 at 15:39
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Jeto Nov 03 '18 at 15:50
  • i have updated my question – Nida Amin Nov 03 '18 at 16:08
  • @NidaAmin Your code is not syntaxically correct. Better to [learn about it](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) rather than expect us to fix it for you. – Jeto Nov 03 '18 at 17:38
  • @NidaAmin Please write down the error you are getting. – engrhussainahmad Nov 03 '18 at 20:19

2 Answers2

0

Maybe you will find what's wrong when I organize your code like this :

foreach ($this->Bay as $k => $obj) {
    $obj->{'BayId'} = ($obj->{'BayId'}=='') ? new MongoDB\BSON\ObjectID();
    $obj->{'IsDeleted'} = "No";
    : new MongoDB\BSON\ObjectID($obj->{'BayId'});
}

I assure you that it's exactly the same code... So ? :)

Nigel
  • 23
  • 5
0

The purpose you are trying to achieve will be more readable and understandable if you use an if statement.However you can proceed like this:

suppose $x=null && $y=null then according to a condition you want the two variables values to be changed using ternary operator you can simply do

(your condition here)?$x="new value"&&$y="new value too":"";

example:

$x=$y=null;
($_GET['age']>10)?$x=10&&$y=12:'';

Remarks: I use some analogy because I don't really know your objects but the principle is the same no matter what your are trying to achieve...

Elementary
  • 1,443
  • 1
  • 7
  • 17