开发者

PHP and HTML formatting, super basic question

I got to a phase in my code where I have too much code and too much HTML is depended on the consequences of some server conditions.

I simply want to know, is there any way to get around the:

<?php if (cond) echo '<p class="someclass">some HTML</p>'; ?> 

?

I just wish there 开发者_如何学运维was something like in C where you can simply go like:

#ifdef x 
    do_a_lot_of_html_stuff;
#endif

All I can see that I can do now is go like:

<?php if (x) require_once("includes/all_needed_part.php"); ?>

Thanks !


Not exactly sure what you're asking, so if I am understanding your question correctly, you're looking for a way to print off blocks of HTML with PHP?

<?php if ($a == $b): ?>
  <div>a == b</div>
  <p>a is equal to b</p>
<?php else: ?>
  <div>a != b</div>
  <p>a is not equal to b</p>
<?php endif; ?>


I generally do it like this:

<?
    function print_heading()
    {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title><? print_title(); ?></title>
    <link rel="stylesheet" type="text/css" href="..." />
    <script language="javascript" type="text/javascript" src="..."></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="title" content="<? print_title(); ?>" />
</head>
<?
    }
?>

But I would think that replacing function print_heading() with if (condition) would work too.


There's an alternative syntax:

<?php if($x): ?>
do_a_lot_of_html_stuff;
<?php endif; ?>


You can add HTML between PHP conditions as follows:

<?php 
$a=1;
if($a==1){ ?>
<div>All the HTML Stuffs</div>
<?php } ?>


You may use the alternative syntax for control structures in combination with the heredoc-syntax.


You can do something like:

<?php if( cond ) { ?>
    SECRET!
<?php } ?>


I would recommend that you check out a template engine, many exist for PHP with one of the most mature being Smarty. One of the newer (and cleaner) solutions is Twig, which is employed by the Symphony framework.


i guess you are at a point in your code where you should use Object Oriented Programming. procedural coding is good but you'll eventually reach the point where there is just to much code in your page.


well you could use a c like syntax and still can do lots of stuff like.

This is typical php approach of using if using c like syntax.

<?php
    if(1==1):
        echo 'i can do lots of stuff here';
        $variable = 'i hold some value';
        $array = array('1','two','three');
    endif;
?>

another way you could implement is by using brackets. for example.

<?php
if(condition) {
   //do some stuff here
} else if(cond) {
   //do another stuff here based on some conditions 
} else if(cond) {
    //you can extend the nested elseif as many times as you like
} else {
    //else execute this.
}
?>


Guessing you are asking if there are frameworks available for php? The answer is yes, and here is at least one good one: http://cakephp.org/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜