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>';
KUMAR
  • 1,993
  • 2
  • 9
  • 26
sevenine
  • 19
  • 6
  • Please do not add code as an image. [edit] your question and add the code as text so that it's more readable. – aynber Jul 23 '20 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. – David Jul 23 '20 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! – sevenine Jul 23 '20 at 12:44
  • @sevenine what you actually want??? – KUMAR Jul 23 '20 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 – sevenine Jul 23 '20 at 12:49
  • `$output .= '
    '. (!empty($row['Tutor_subject_2'])?''. $row['Tutor_subject_2'] .'':'') .'
    ';`
    – IncredibleHat Jul 23 '20 at 12:49
  • @sevenine yes then now what you do??? – KUMAR Jul 23 '20 at 12:51
  • I'm quite sure this is not the best practice, if it's even possible. We need to see more of the code to know what is going on and how to solve your problem in a different way, because the way you want is either not possible or will require a eval(). And that is not recommended – Andreas Jul 23 '20 at 12:54
  • @KUMAR How can i edit the code so it can run properly? By following that youtube video I know that I can change to '.$row['Tutor_subject_1'].' but not sure how to change the IF statement accordingly – sevenine Jul 23 '20 at 12:54
  • @Andreas Thank you for your advice! I just start coding for 1-2 months so I just follow my logic to do that and I know there are still a lot to learn. May I know how can I show more code to you? I am following this video: https://www.youtube.com/watch?v=6UfPmrD4VWI 23:57 – sevenine Jul 23 '20 at 12:58
  • The reason you get the error is because you open the string with`'` and then you use the same in the code. This gives the unexpected string error because you end the string just before `Tutor_subject_2 ` but then there is still string data after it. The easy fix is to escape the `'` with `\'` everywhere between the opening of the string and the end of the string. Meaning at four places, on either side of Tutor_subj... – Andreas Jul 23 '20 at 13:01
  • Thank you @IncredibleHat !!!!! It works!!!!!!!!!!!!!!!!!!! You save my day!!!!! – sevenine Jul 23 '20 at 13:04
  • I am literally out in the woods and do not have the internet connection to look at the video. But I managed to see something and as I can see from the video you have not followed his example. He ends the string and concatenate the variable then resumes the string again using dots. Incorrect example (as you have): `' blabla bla string $array['item'] more string'` what it should be is: `' blabla bla string ' . $array['item'] .' more string'` notice the `'` and `.` where the variable is. – Andreas Jul 23 '20 at 13:07

1 Answers1

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>';
David
  • 208,112
  • 36
  • 198
  • 279