0

Parse error: syntax error, unexpected 'if' (T_IF) in

I keep on getting this error i cannot proceed am trying to execute my code below

can someone help me provide a solution?

$contentLogs .='

      <tr '.if($row['position']=='Non-Student' || $row['position']=='University'){ echo "style='display:none;'";}.'>
        <th style="width:50%" >ID Number:</th>
        <td>'.$row['id_number'].'</td>
      </tr>
';
KIX
  • 1
  • 2

3 Answers3

2

You cannot use an if statement inside a variable.

$style = ($row['position']=='Non-Student' || $row['position']=='University') ? " style='display:none;'" : '';

$contentLogs .='
      <tr'.$style.'>
        <th style="width:50%" >ID Number:</th>
        <td>'.$row['id_number'].'</td>
      </tr>
';

nixUser
  • 180
  • 1
  • 7
  • This is not an "if statement inside a variable". It is a conditional used to assign one of two strings to a variable which then simply holds a string. – arkascha Oct 05 '19 at 10:06
  • ahh thats why great thanks sir big help.. – KIX Oct 05 '19 at 10:07
0

Not wise using logic when assigning value to a variable.

     $custom_style = ($row['position']=='Non-Student' || $row['position']=='University')?"style='display:none;'":"";
      $contentLogs .='
               <tr '.custom_style.'>
             <th style="width:50%" >ID Number:</th>
            <td>'.$row['id_number'].'</td>
           </tr> ';
infomasud
  • 2,263
  • 1
  • 18
  • 12
0

I prefer to build my strings in a structured way which is readable:

<?php
$row = [
  'position' => 'Non-Student',
  'id_number' => 5
];

$contentLogs = <<<'EOT'
<table>
    <tr style="%1$s">
        <th style="width:50%%" >ID Number:</th>
        <td>%2$s</td>
    </tr>
</table>

EOT;

echo sprintf(
  $contentLogs, 
  ($row['position'] == 'Non-Student' || $row['position'] == 'University') ? "display:none;" : "",
  $row['id_number']
);

The output of that example obvious is:

<table>
    <tr style="display:none;">
        <th style="width:50%" >ID Number:</th>
        <td>5</td>
    </tr>
</table>
arkascha
  • 41,620
  • 7
  • 58
  • 90