-1
   $output10 .= '            <div class="panel panel-profile list-view">
          <div class="panel-heading">
            <div class="media">
              <div class="media-left">
                <a href="#">
                  <img class="media-object img-circle" src="../images/photos/user1.png" alt="">
                </a>
              </div>
              <div class="media-body">
                <h4 class="media-heading">'. $row['username'] . '</h4>
               'if($pAdminLevel == 0) : '<p class="media-usermeta"><i class="glyphicon glyphicon-briefcase"></i>' . $pAdminLevel . '</p>' endif; '
              </div>
            </div><!-- media -->

I want to make a if statement, but it shows this error. The code I posted is not the full version, that's why closing brackets are missing, I just posted what I don't understand, thank you.

syntax error, unexpected 'if' (T_IF) in

4 Answers4

1

You cannot use if within concatenated string.

There are basically two options here:

Ternary operator:

$output = "abc" . ($pAdminLevel == 0 ? "text when condition is true" : "text when it is false") . " other text";

Separate logic for code:

$output = "abc";
if ($pAdminLevel == 0) {
    $output .= "cde";
} else {
    $output .= "xyz";
}
DevilaN
  • 1,317
  • 10
  • 21
0

You can try something like this:

   $output10 .= '            <div class="panel panel-profile list-view">
          <div class="panel-heading">
            <div class="media">
              <div class="media-left">
                <a href="#">
                  <img class="media-object img-circle" src="../images/photos/user1.png" alt="">
                </a>
              </div>
              <div class="media-body">
                <h4 class="media-heading">'. $row['username'] . '</h4>' .
               ( $pAdminLevel == 0 ? '<p class="media-usermeta"><i class="glyphicon glyphicon-briefcase"></i>' . $pAdminLevel . '</p>' : '' ) .
              '</div>
            </div><!-- media -->';

The syntax is ( boolean ? true : false ).

There is no other way to make inline conditionals. You could always just split up your string of course.

andreas
  • 7,844
  • 9
  • 51
  • 72
0

You stopped the string literal but didnt start it again.

$output10 .= '<div class="panel panel-profile list-view">
      <div class="panel-heading">
        <div class="media">
          <div class="media-left">
            <a href="#">
              <img class="media-object img-circle" src="../images/photos/user1.png" alt="">
            </a>
          </div>
          <div class="media-body">
            <h4 class="media-heading">'. $row['username'] . '</h4>';
if($pAdminLevel == 0) : 
    $output10 .= '<p class="media-usermeta"><i class="glyphicon glyphicon-briefcase"></i>' . $pAdminLevel . '</p>' ;
endif; 

$output10 .= '</div>
        </div><!-- media -->';
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

I'm a fan of the following pattern when managing html output with php:

$output10 .= '<div class="panel panel-profile list-view">';
$output10 .= '<div class="panel-heading">';
  $output10 .= '<div class="media">';
    $output10 .= '<div class="media-left">';
      $output10 .= '<a href="#">';
        $output10 .= '<img class="media-object img-circle" src="../images/photos/user1.png" alt="">';
      $output10 .= '</a>';
    $output10 .= '</div>';
    $output10 .= '<div class="media-body">';
      $output10 .= '<h4 class="media-heading">'. $row['username'] . '</h4>';
      if($pAdminLevel == 0) {
        $output10 .= '<p class="media-usermeta"><i class="glyphicon glyphicon-briefcase"></i>' . $pAdminLevel . '</p>';
      }
    $output10 .= '</div>';
  $output10 .= '</div><!-- media -->';

It's not the prettiest, but it avoids use of ternary conditions which can be bad for maintainability & avoids multiline strings (which have historically been problematic in PHP)

admcfajn
  • 2,013
  • 3
  • 24
  • 32