I am trying to print out yes if a boolean table field from a database query is true, and no if it is false.
I am doing this:
echo "<td>"{$row['paid'] ? 'Yes' : 'No'";}</td>";
Why is this incorrect?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Other guys have corrected your mistake, but I thought you might like to know why.
Your use of a ternary isn’t actually the problem, it’s the way you join it to the other stuff.
Echo is a function that takes one variable; a string. It’s actually this (although people tend to leave the brackets off):
In your case, SomeString needs to be “” followed by the outcome of your ternary, followed by “”. That’s three strings, which need to be glued together into one string so that you can “echo()” them.
This is called concatenation. In PHP, this is done using a dot:
Which can be placed inside an echo() like this:
Alternatively, you can skip concatenation by using a function that takes more than one string as a parameter. Sprintf() can do this for you. It takes a “format” string (which is basically a template) and as many variable strings (or numbers, whatever) as you like. Use the %s symbol to specify where it needs to insert your string.
The world is now your oyster.