Trying to use an if statement in php to display a Font Awesome Class

Hello all,

I’m trying to get a font awesome class to display depending on the what’s in my string value $teacher[‘gender’] using an if statement.

I tried the following:

[php] if($teacher[‘gender’] == “F”)
{

				 <i class="fa fa-female fa" aria-hidden="true"></i>
				  
			  }[/php]

…but I got this error:
Parse error: syntax error, unexpected ‘<’ in….

The following works however, but it’s not really what I want to achieve:

[php] <?php
if($teacher[‘gender’] == “F”)
{
echo “person is female”;
print ("");

[/php]

Any help on how to apply the class would be great.
Thanks for reading.
Best regards,
Jool

In the first example you’re mixing PHP and HTML which won’t work. You need to exit PHP before you can write normal PHP again

Your second example would work if you just changed to the code you wanted.

I’d recommend that you do not echo HTML though. Exiting PHP and outputting HTML normally (echoing values with PHP where needed) makes more sense, it even makes your IDE syntax highlight the code

[php]if($teacher[‘gender’] == “F”) { ?>

<?php } ?>[/php]

or

[php]if($teacher[‘gender’] == “F”): ?>

<?php endif; ?>[/php]

If you have multiple of these though then why not make a general one?

[php]<?php
// some php stuff here
?>
<i class=“fa fa-<?= ($teacher['gender'] == "F") ? 'female' : 'male' ?> fa” aria-hidden=“true”>[/php]

Note that this will only work if every user must be either male or female. But it shows how you can have one line instead of multiple.

Fantastic!
Many thanks for your help and advice JimL - have a great day.

Sponsor our Newsletter | Privacy Policy | Terms of Service