Using an IF statement in Php to display a class / font awesome image

Hello all,

I'm trying to get a font awesome class to display depending on the whats in my table. The following works -

  <?php
if($teacher['sex'] == "F")
				  {
 echo  "person is female";  
print ("<IMG SRC =../img/Logo_200x200.jpg>");
					  
				  }

Open in new window


However, I want to run the following line of code when the condition is met:

<i class="fa fa-female fa" aria-hidden="true"></i>

Open in new window


However, the following code returns an error relating to this line:

 if($teacher['sex'] == "F")
				  {
					 
					 <i class="fa fa-female fa" aria-hidden="true"></i>
					  
				  }

Open in new window


The error message is
Parse error: syntax error, unexpected '<'

Something pretty basic I'm doing wrong I imagine. Many thanks for reading.

Best regards,

Adam
AdamTrying to learn phpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim RiddlesPrepress/OMS SpecialistCommented:
You need to echo or print your output as in your first example.
if($teacher['sex'] == "F")
{
  echo '<i class="fa fa-female fa" aria-hidden="true"></i>';
}

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fabio AmorimDelphi DeveloperCommented:
The Jim Riddles's answer is correct, but if you want, you can use this approach too:
if($teacher['sex'] == "F")
{
  print ('<i class="fa fa-female fa" aria-hidden="true"></i>');
}

Open in new window


Which is very similar to your first approach:

I'm trying to get a font awesome class to display depending on the whats in my table. The following works -

  <?php
if($teacher['sex'] == "F")
				  {
 echo  "person is female";  
print ("<IMG SRC =../img/Logo_200x200.jpg>");
					  
				  }

Open in new window

1
AdamTrying to learn phpAuthor Commented:
Many thanks Jim. That works perfectly.

I struggle with when to use single quotes, double quotes and when not required at all. Thanks for the quick response.

Thanks also Fabio for your contribution.
1
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Julian HansenCommented:
I struggle with when to use single quotes, double quotes and when not required at all
Single quotes when
- you want to only output string data (no embedded variables)
- you want to include un-escaped double quotes in your string
Single quotes in the string must be escaped.

Example
echo 'John said, "That\'s my bike"';

Open in new window


Double quotes when
- You have variables in the string
- You want to include unescaped single quotes
Double quotes must be escaped
Example
$thing = 'bike';
echo "John said, \"That's my {$thing}\"";

Open in new window


HEREDOC when you want the best of both worlds
HEREDOC allows for both unescaped single and double quotes and the embedding of variables
Example
$thing = 'bike';
echo <<< TEXT
John said, "That's my {$thing}"
TEXT;

Open in new window


Sidebar to the answers above. There is another method for outputing HTML in PHP that does not use echo or print. That is where we close the <?php tags before the output. So from your original post.
<?php if($teacher['sex'] == "F"): ?>
<i class="fa fa-female fa" aria-hidden="true"></i>
<?php endif;?>

Open in new window

Note the use of the alternative block statements: ':' and 'endif'
There is no reason why you could not use the standard '{' '}' here. By convention I use the ':' 'endif' when I am including HTML in a condition or loop block - it makes it easier to see where the block ends
Using the above you can type straight HTML between lines 1 and 3 as they are now outside the PHP script.
2
Jim RiddlesPrepress/OMS SpecialistCommented:
Excellent comment, Julian.  I am actually a big fan of HEREDOCs, personally.
0
Julian HansenCommented:
Thanks Jim,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
if statements

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.