Solved

PHP IF statement

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult 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 count occurrences of each item in an array.

932 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

12 Experts available now in Live!

Get 1:1 Help Now