0

I want to add a condition with PHP within the $output. For the item in , I know that I can simply replace the PHP tag with '. But for the IF clause, I am not sure how to correct it as the PHP code doesn't work in the variable. Thanks!

$output .='<div class="square-card">
        <?php if($row['Tutor_subject_2'] != ''){ ?>
        <span class="subject"><?= $row['Tutor_subject_2']; ?></span> 
        <? }?>
</div>';
13
  • Please do not add code as an image. edit your question and add the code as text so that it's more readable. Commented Jul 23, 2020 at 12:37
  • 1
    Please include relevant code as text in the question. (We can help with formatting if need be.) It's not clear to me what you're asking. Why are you trying to put PHP code in a variable? What are you planning to do with that variable? Are you just trying to conditionally add text to the variable? If so, this is very much over-complicating it. You can just wrap the concatenation of the text in an if block. Commented Jul 23, 2020 at 12:38
  • I am trying to make a filter page I followed a youtube video to make it. I put PHP code in the variable because I wanna only show the items which fit the condition set in the filter. As I just started learning to code, may I know how can I simplify the code? Thanks! Commented Jul 23, 2020 at 12:44
  • @sevenine what you actually want??? Commented Jul 23, 2020 at 12:48
  • KUMAR thank you for your help and now I face an error as the PHP code does work in the $output. it said a unexpected T_string Commented Jul 23, 2020 at 12:49

1 Answer 1

1

It looks like you're overcomplicating things. You don't need to put PHP code inside of a string inside of your PHP code. Just use PHP code to build your string. For example:

$ouput .= '<div class="square-card">';
if($row['Tutor_subject_2'] != '') {
    $ouput .= '<span class="subject">' . $row['Tutor_subject_2'] . '</span> ';
}
$ouput .= '</div>';
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.