?
Solved

PHP IF statement

Posted on 2013-01-25
2
Medium Priority
?
348 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 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

719 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