Solved

PHP IF statement

Posted on 2013-01-25
2
339 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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now