Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I include html in IF statement? 2

Status
Not open for further replies.

followtheboat

Programmer
Nov 29, 2008
53
IN
I'm a complete noob when it comes to PHP so this should be an easy one for people to answer. I just want to include some html depending upon a condition. How do I do it? I'm using an include as an example but want to replace the include with the html:

Code:
<?php
     if (is_page('2')) {
     include('sidebar_about.php');} 
     else {
     include('sidebar_home.php');}
?>

Any help appreciated!
 
Code:
<?php
     if (is_page('2')) {
      echo "<div>I am some html</div>";
     } else {
     echo "<div>I am some different html</div>";
     }
?>
 
Another method you might try:

Code:
<?php
     if (is_page('2')) { ?>

     <div style="border: 1px solid #000000;">Using this method allows you to inject HTML without escaping " or ' marks as you would with the 'echo' or 'print' commands.</div>

     <? } else { ?>

     <div>However, the code does become a little harder to read, if you have a lot of html (a whole page for example) I would use this method, but if its one or two lines, use echo or print</div>

          <? }
?>
 
I ended up with something like this:

Code:
    <td><a href="[URL unfurl="true"]http://localhost/wordpress/"[/URL] class="screenshot" rel="[URL unfurl="true"]http://localhost/wordpress/wp-content/themes/modern_era/images/sshome.gif">[/URL]
		<?php
    	 	if (is_home('')) {
      		echo "<img name=scribble_r2_c1 src=[URL unfurl="true"]http://localhost/wordpress/wp-content/themes/modern_era/images/scribble_r2_c1-line.gif[/URL] width=83 height=29 border=0></a></td>";
     		} else {
      		echo "<img name=scribble_r2_c1 src=[URL unfurl="true"]http://localhost/wordpress/wp-content/themes/modern_era/images/scribble_r2_c1.gif[/URL] width=83 height=29 border=0></a></td>";
     		}
		?>

Thanks all
 
Hi

A whole [tt]if[/tt]..[tt]else[/tt] for five character difference ? Personally I avoid such things, because it reduces the code's readability. I prefer something like this :
Code:
<?php
echo "<img name=scribble_r2_c1 src=/wordpress/wp-content/themes/modern_era/images/scribble_r2_c1",[red]is_home('')?'-line':''[/red],".gif width=83 height=29 border=0></a></td>";
?>

[gray]# or[/gray]

<?php
[red]$line=is_home(''):'-line':'';[/red]
echo "<img name=scribble_r2_c1 src=/wordpress/wp-content/themes/modern_era/images/scribble_r2_c1[red]$line[/red].gif width=83 height=29 border=0></a></td>";
?>

[gray]# or[/gray]

<?php
[red]$image=is_home(''):'scribble_r2_c1-line.gif':'scribble_r2_c1.gif';[/red]
echo "<img name=scribble_r2_c1 src=/wordpress/wp-content/themes/modern_era/images/[red]$image[/red] width=83 height=29 border=0></a></td>";
?>

Feherke.
 
Aha, ok, I see that. I'll try this in a bit. Thanks for your time, Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top