2

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 !

3
  • Vague question. Are you saying you want a framework? Commented Aug 18, 2011 at 19:26
  • The C version isn't any less verbose than the PHP version. You'll encounter this kind of tedium no matter what kind of framework or templating system you use. at some point you just have to bite the keyboard and start typing. Commented Aug 18, 2011 at 19:27
  • Is there anything wrong with doing if ( cond) { ...; ...; ...; ...; }? Commented Aug 18, 2011 at 19:28

10 Answers 10

6

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; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. BTW, you can also put <?php blocks inside of the if logic, as I find that to be one of the most useful parts of this whole thing.
2

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.

Comments

1

There's an alternative syntax:

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

Comments

1

You can add HTML between PHP conditions as follows:

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

1 Comment

Hope it Helps!!
0

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

Comments

0

You can do something like:

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

Comments

0

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.

Comments

0

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.

Comments

0

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.
}
?>

Comments

-2

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/

2 Comments

This question was not: "What is your favorite PHP framework? Please advertise it."
Not advertising my favorite. Just giving an example off the top of my head.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.