Solved

PHP IF statement

Posted on 2013-01-25
2
345 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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.

756 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