Solved

PHP IF statement

Posted on 2013-01-25
2
346 Views
Last Modified: 2013-01-25
I have text on a page.
Menuhead found <?php $num?> Results

I am trying to do an if statement with formating and the number of results included.  So if its one resukt the word would be result and if it zero or many it would be results.

<?php
if ($num === 1)
	{
   echo <h2 align="left">MENUHEAD FOUND 1 RESULT</h2>;
   }
 else
   {
   echo <h2 align="left">MENUHEAD FOUND <?php echo $num ?> RESULTS</h2>;
   }
   ?>

Open in new window


This however is fraught with errors.
0
Comment
Question by:DS928
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 38821362
Your overall logic is correct, but the problem you are having is that there are a number of syntactical errors in your code. Your strings in the echo statements are not enclosed in doublequotes. Even if they were, you are not escaping the doublequotes in the [align="left"] part of the string for your H2 tags, and finally you have a redundant "<?php" opening tag.

Try this instead:

<?php
if ($num == 1) {
    echo "<h2 align=\"left\">MENUHEAD FOUND 1 RESULT</h2>";
} else {
    echo "<h2 align=\"left\">MENUHEAD FOUND " . $num . " RESULTS</h2>";
}
?>

Open in new window


Read up on this page for how to do escaping of characters in PHP strings:

http://php.net/manual/en/language.types.string.php
0
 

Author Closing Comment

by:DS928
ID: 38821370
I would have been here all night trying to figure this one out!  Works perfectly!  Also thank you for the link.  It really helps!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question