2

PHP isn't really my language of choice, that being said I remember seeing a php if being used something like this:

<?php 
    if (blah) { 
?>
        // loads of html and js that doesn't need to be escaped since we closed
        // the php already with "?>"
<?php
    }
?>

Or something like this. I don't know what it's called and googling php if doesn't help.

What's this technique called and what's the correct syntax for it?

qwertymk
  • 34,200
  • 28
  • 121
  • 184
  • 2
    going in and out of php tags? – Matisse VerDuyn Apr 27 '12 at 12:19
  • 1
    The correct syntax for it is as above. I call the technique "horrible". If you must do this, consider creating separate .html files and `include`ing them, mixing PHP and HTML creates an unreadable mess with sloppily white-spaced output. – DaveRandom Apr 27 '12 at 12:19

12 Answers12

3

It can be used as you posted in the question, or like this (which I prefer):

<?php 
    if(blah):
?>
        // loads of html and js that doesn't need to be escaped since we closed
        // the php already with "?>"
<?php
    endif;
?>

This is most often seen on templates where you don't do any logic, just simple if and loops like:

<?php foreach($something as $test): ?>
    <p><?php echo $test; ?></p>
<?php endforeach; ?>
MrCode
  • 63,975
  • 10
  • 90
  • 112
1

They are called php tags. See here:

http://www.php.net/manual/en/language.basic-syntax.phptags.php

The syntax is as used in your example. There are variations, but the this one is preferable.

The other "technique" is called using control structures. See here:

http://ch.php.net/manual/en/control-structures.if.php

Dario Pedol
  • 2,070
  • 14
  • 24
1

You might want to check this out:

Can HTML be embedded inside PHP in IF statement

and

PHP Manual

Community
  • 1
  • 1
Silviu
  • 835
  • 14
  • 22
1

The cleanest way is like this

<?php if ($test) : ?>
    <p>Some HTML</p>
<?php endif; ?>
trapper
  • 11,716
  • 7
  • 38
  • 82
1

That syntax is correct. There is also an alternative syntax which can improve readability of code if you have to mix PHP and HTML in places:

<?php if( blah ): ?>
  <!-- my HTML -->
<?php endif; ?>

I disagree with those who make the blanket statement for not mixing php and html. I use a templating system to separate application logic from HTML as much as possible, but there are still places where an if statement or for loop is needed within the template file.

n00dle
  • 5,949
  • 2
  • 35
  • 48
1

As another user pointed out, it's called Escaping from HTML, it's just an alternative to echo/print'ing out every line of HTML that is inside the PHP if statement.

That being said, the syntax you're using is just fine:

// Option 1
<?php if ($variable) { ?>
<!-- HTML stuff here -->
<?php } ?>

Or, the other way:

// Option 2
<?php
if ($variable) {
    echo '<div>';
    echo '<span>Totally putting HTML here</span>';
    echo '</div>';
}
?>

Or, the third way as user1266222 pointed out:

// Option 3
<?php if ($variable) { include 'path/to/htmlfile/filename.html'; } ?>

Which would just include the HTML file there.

The method depends on what you wanna do, all are viable in all usecases, but for only very small snippets of HTML, I would go with option 2. For a whole HTML file (or even just partial), I would go with option 3. And if lazy and you have a longer piece of HTML, there is option 1.

Tehnix
  • 2,020
  • 2
  • 18
  • 23
1

In the manual page it is called Escaping from HTML
Of course, you can use whatever PHP statement with it, not only if

Correct syntax is one you used in your code snippet: use ?> to close PHP mode and <?php to open it again, with the text between them directly going into browser

The usage of this technique have to be limited to PHP-based templates. Yet being extremely useful for this purpose.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

do you mean something like this?

<?php include("filename.html"); ?>

or you can just write html code if you are not inside of the php tags

0
<?php
if(blah){
include('htmlfile.html');
}
?>

htmlfile.html

<!-- Your HTML Code -->
Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52
0

It's called embedding PHP in HTML (or vice versa), there's an alternative syntax to make it look slightly better:

<?php if (condition) : ?>
HTML HTML HTML
<?php endIf; ?>

Using include(file.html); is usually a better way to go though.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

It's a perfectly valid syntax, although doesn't look very pretty, so for files with lots of HTML you can also use:

<?php if($foo) : ?>
<p>Unescaped html goes here</p>
<?php endif; ?>

http://www.php.net/manual/en/control-structures.alternative-syntax.php

lafor
  • 12,472
  • 4
  • 32
  • 35
-1

You might consider the heredoc

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Adam Lukaszczyk
  • 4,898
  • 3
  • 22
  • 22