rockstar
rockstar

Reputation: 21

if elseif else ternary operator not working in php

I am trying to keep if elseif else in ternary php please check my code Parse error: syntax error, unexpected ':' in am i doing wrong?

$v= array(
    'header' => 'Request Status',
    'value' => '($data->status == 0) ? "Pending" : (($data->status == 1) ? "Accepted" : "Rejected":(($data->status == "") ? "test")'
);

Upvotes: 0

Views: 711

Answers (4)

Xorifelse
Xorifelse

Reputation: 7911

<?php
  $v= array(
    'header' => 'Request Status',
    'value' =>   $data->status == 0  ? "Pending" 
               : $data->status == 1  ? "Accepted" 
               : $data->status == "" ? "empty" 
               : "Rejected")
);

However in my honest opinion, its best to avoid multi-ternary operators. There is some logic in ternary if you split the lines like so, but this is not the case in every scenario.

This is a far better solution:

function getStatus($v){
  switch($v) {
    case 0:  return "Pending";
    case 1:  return "Accepted";
    case "": return "Empty"; 
  }

  return "Rejected";    
}

$v= array(
    'header' => 'Request Status',
    'value' => getStatus($data->status)
);

Upvotes: 0

If your intention was something like this:

if($data->status === 0) "Pending"
elseif($data->status == 1) "Accepted"
elseif($data->status === "") "test"
else "Rejected"

You can achive the same this way :

'value' =>  $data->status === 0 ? "Pending" : (
               $data->status == 1 ? "Accepted" :
                   ($data->status === "" ? "test" : "Rejected") 
            )

Notes:

  1. Remove the single quotes '...' around the value. This makes your codes a string only.

  2. Put each else block inside a bracket if it has another ternary operator in that block. This removes the confusion on the precedence of the ternary operators.

Upvotes: 0

aliasm2k
aliasm2k

Reputation: 901

Instead of nesting ternary operator, it would be more readable if you use switch

switch($data->status) {
    case 0:
        $val = "Pending";
        break;
    case 1:
        $val = "Accepted";
        break;
    case "":
        $val = "Empty";
        break;
    default:
        $val = "Rejected";
        break;
}

Then you can use $val later in your array.

Upvotes: 1

hherger
hherger

Reputation: 1680

Firstly, as Tareq Mahmood said: your ternary operator is enclosed in single quotes and therefore is taken as a string. And in consequence, the whole command is assinged to the item named 'value'. Displaying the contents of the $v array proves that. The script...

$v= array(
    'header' => 'Request Status',
    'value' => '($data->status == 0) ? "Pending" : (($data->status == 1) ? "Accepted" : "Rejected":(($data->status == "") ? "test")'
);
// Display what is in the array
echo '<pre>';
print_r($v);
echo '</pre>';

...gives

Array ( [header] => Request Status [value] => ($data->status == 0) ? "Pending" : (($data->status == 1) ? "Accepted" : "Rejected":(($data->status == "") ? "test") )

(And, btw, there is no syntax error in the code from your question at all.)

My guess: the values of 'value' should be - depending on the value of $data-> status: == 0 : "Pending" == 1 : "Accepted" == "" : "Rejected" else : "test"

The correct corresponding code would be:

$v= array(
    'header' => 'Request Status',
    'value' => ($data->status == 0) ? "Pending" : ( ($data->status == 1) ? "Accepted" : ( ($data->status == "") ? "Rejected" : "test" ) )
);

`

... or in a more verbose format:

    $v= array(
   'header' => 'Request Status',
   'value' => 
      ($data->status == 0) 
         ? "Pending" 
         : (
            ($data->status == 1) 
               ? "Accepted" 
               : (
                  ($data->status == "") 
                     ? "Rejected" 
                     : "test"
               )
         )
    );

Upvotes: 0

Related Questions