B5ae59b19cc356df333baffffb9f2c91

Hi, I am just scratching the surface with PHP. I am trying to write a conditional statement with PHP to generate a div block with different classes based on the page I am on.

I would like to display the block with class="slideshow" if the ID is 8 or the empty div block with class="header".

It's escaping the quotes that's getting me, I think. :-) Thanks in advance!

1
2
3
4
5
6
7
8
<?php if ( the_ID() == 8) {
echo "<div id='menu_show' class='slideshow'>
  <img src='http://www.someone.com/wp-content/themes/True%20Nature/img/rotate1.jpg' alt='A picture' width='748' height='245' />
</div>"
else {
echo "<div class='header'></div>"
}
?>

Refactorings

No refactoring yet !

441c4f02db55ef2cbe96027af7012e01

techietim

December 7, 2007, December 07, 2007 19:31, permalink

1 rating. Login to rate!

This is one way to shorten it

1
2
3
<?php 
echo (the_ID() == 8) ? '<div id="menu_show" class="slideshow"><img src="http://example.org/image.gif" alt="A picture" width="748" height="245" /></div>':'<div class="header"></div>';
?>
B849433e0e1a3f4ca0cf7cc55b8acd53

Casper

December 7, 2007, December 07, 2007 19:35, permalink

2 ratings. Login to rate!

You have a missing close brace "}" before the else statement.

Also when you have a lot of HTML code it's easier not to "echo" it, just type it out outside the PHP block instead, and keep the PHP code inside the <? ?> blocks. See below alternative 1.

Or..if you have a big block of text you can use what's called the heredoc operator. This will let you type in quotes and any text until the end of the heredoc. See alternative 2. Not as "clean" as alternative 1 perhaps, but demonstrates the heredoc operator.

Alternative 1: HTML code outside the PHP blocks

1
2
3
4
5
6
7
8
<?php if (the_ID() == 8) { ?>
  <div id="menu_show" class="slideshow">
    <img src="..." />
  </div>
<?php } else { ?>
  <div class="header"></div>
<?php } ?>

Alternative 2: HTML code with echo + heredoc operator

1
2
3
4
5
6
7
8
9
10
11
12
<?php if (the_ID() == 8) {
  echo <<<EOL  // <- We use the heredoc operator. Notice 3 times '<'
    <div id="menu_show" class="...">
      ...
    </div>
EOL;  // End the heredoc block. Has to be the first characters on the line,
      // so don't indent it.
} else {
  echo "<div class=\"header\"></div>";
}
?>
B5ae59b19cc356df333baffffb9f2c91

K

December 7, 2007, December 07, 2007 19:43, permalink

No rating. Login to rate!

Tim, thanks a lot! This is very helpful.

Casper, I like your first alternative. That's gonna help me as I am going to check for multiple conditions with elseif and extend the blocks.

The second alternative might actually come in handy should I use CASE statement for the same.

I had blogged about RefactorMyCode before at Shankrila.com but this is the first time I have posted on here. I am going to literally live here. You guys rock! :-)

B5ae59b19cc356df333baffffb9f2c91

K

December 7, 2007, December 07, 2007 19:44, permalink

No rating. Login to rate!

Can I ask you guys a favor and ask you to edit the url in the image to someone.com. That's really stupid of me to post the domain name!

0481b9ea2b2aad6844b33dbca2d7fe0e

Paul Kemper

December 8, 2007, December 08, 2007 08:57, permalink

2 ratings. Login to rate!

I suggest that you adhere to the following rules concerning the use of ' and "

1. Never put variables inside quotes (which you adhere to)
2. Never use " to enclose you strings, except in these two cases:
a. Your string has control characters like \n and \t
b. Your string is a SQL statement

That way, you can use the " to enclose attribute values in your HTML/XML strings, without the headaches

Also note that HEREDOC processing in PHP is quite time consuming, so try to avoid it.

Furthermore, a construct like if () ... else ... is clearer when reviewing your code than the ..?..:.. construct. If you have big pieces of code or strings to process, best use the if..else... construct. Consider using ..?..:.. only if there are two clear, small options (e.g. $name = ($_POST['name']==='')?'default':$_POST['name']; ). When the code gets executed, it is not slower or faster than ..?..:..

9bb39a91d11502e243caf099e79683f1

jenicun

April 10, 2008, April 10, 2008 02:42, permalink

No rating. Login to rate!

Sorry for putting it in the wrong topics,and of course,my bad english lol.can someone help me too ? can div statement be put we use php function ( we usually use javascript function ), but for somereason, i must combine the div tags with php function
thanks....

Your refactoring





Format Copy from initial code

or Cancel