Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Correctly Nested Conditional Statements - PHP

Posted on 2012-03-14
5
Medium Priority
?
248 Views
Last Modified: 2012-03-14
Hi All,

I'm not a big PHP developer, I normally use VBscript...

Anyway I'm trying to write a nested conditional statement that does the following -

If HomePage then

Declare Variables (x2 sets)

If Variable Set 1 true then

Write this HTML

ElseIf Variable Set 2 true then

Write this HTML

Else

Write this HTML
End If
End If

My PHP guy is telling me that the logic in the code below is incorrect, but I just can't see where --

// home page fixed panels
if ( $_SERVER['REQUEST_URI'] == '/' ) {
$promotion_ends = strtotime('2012-03-19');
$promotion_starts = strtotime('2012-06-01');
$show_starts = strtotime('2012-09-01');
$show_ends = strtotime('2012-10-14'); 
 
	if ( (time() >= $promotion_starts ) && ( time() <= $promotion_ends )) {
    $fixed_panels[] = <<<HTML
      <div class="promotion">
        <h2><a href="/link1.html">Text here</a></h2>
      </div>
HTML;
 }
	elseif ( (time() >= $show_starts ) && ( time() <= $show_ends )) {
    $fixed_panels[] = <<<HTML
      <div class="show_2011 panel">
        <h2><a href="/link2.html">Text here</a></h2>
      </div>
HTML;
  }
  else {
    $fixed_panels[] = <<<HTML
      <div class="benefits panel">
        <h2>Partner Benefits</h2>
        <ul>
          <li><a href="/link3.html">Text here</a></li>
          <li><a href="/link4.html">Text here</a></li>
        </ul>
      </div>
HTML;
  }
}

Open in new window


Apparently I haven't nested the conditional statements.

Appreciate some feedback please.

Thank you
0
Comment
Question by:garethtnash
[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
  • 2
5 Comments
 
LVL 1

Assisted Solution

by:rakjosh
rakjosh earned 1000 total points
ID: 37719155
Hi,

please try below code, it will surely.

<?
if ( $_SERVER['REQUEST_URI'] == '/' ) {
$promotion_ends = strtotime('2012-03-19');
$promotion_starts = strtotime('2012-06-01');
$show_starts = strtotime('2012-09-01');
$show_ends = strtotime('2012-10-14');

      if ( (time() >= $promotion_starts ) && ( time() <= $promotion_ends )) {
    $fixed_panels = '<div class="promotion">
        <h2><a href="/link1.html">Text here</a></h2>
      </div>';

 }
      elseif ( (time() >= $show_starts ) && ( time() <= $show_ends )) {
    $fixed_panels = '<div class="show_2011 panel">
        <h2><a href="/link2.html">Text here</a></h2>
      </div>';

  }
  else {
    $fixed_panels = '<div class="benefits panel">
        <h2>Partner Benefits</h2>
        <ul>
          <li><a href="/link3.html">Text here</a></li>
          <li><a href="/link4.html">Text here</a></li>
        </ul>
      </div>';
  }
}
echo $fixed_panels;
?>
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1000 total points
ID: 37719192
I think the real problem is that you are using the same HEREDOC tags in three different places. The nesting of the IF statements looks OK
0
 

Author Comment

by:garethtnash
ID: 37719209
Thanks Rakjosh,

Just to check though, the nesting of the conditional statements is correct though? my logic is sound from the nesting side?

Thanks
0
 
LVL 1

Expert Comment

by:rakjosh
ID: 37719454
Hi garethtnash,
Your nesting of the conditional statements is correct, you can use HEREDOC tags or as i suggested.
0
 

Author Closing Comment

by:garethtnash
ID: 37719479
Thank you both :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

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