John Davidson

php - Parse error: syntax error within the If statement

0 comments
Message:


So if I have been trying to create an If statement in my HTML PHP file where it should display as "N/A" if the 'Workshop_Contact'=="0".


I've tried some ways but seems like I couldn't find the solution


Here is the line code:


<h4 class="card-title text-danger">Contact:
<?=
if ($row['Workshop_Contact']==0){
echo "N/A";
}
else{
echo "0" . $row['Workshop_Contact'];
}
?>
</h4>


My Answer:
The issue in your code is that you are using the short echo syntax `` with an if statement inside it. This is not allowed in PHP.

You can fix this by using a ternary operator instead. Here is the corrected code:

php

Contact:




This code uses the ternary operator to check if `Workshop_Contact` is equal to 0. If it is, it will display "N/A", otherwise it will display "0" followed by the value of `Workshop_Contact`.

Rate this post

5 of 5 based on 3284 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us