ElectronSurf
ElectronSurf

Reputation: 205

put HTML inside the PHP code

sorry if my question is so silly!

i have a php code:

<div class="breadcrumbs">
    <?php if(function_exists('bcn_display') && !is_archive())
    {
    bcn_display();
    }?>
</div><br />

and i want put div inside php if order:

    <?php if(function_exists('bcn_display') && !is_archive())
    {
<div class="breadcrumbs">
    bcn_display();
</div><br />
    }?>

but the way i'm doing it is not right, i hope someone can help me.

thanks in advance.

Upvotes: 0

Views: 222

Answers (6)

Phil
Phil

Reputation: 824

You need to encapsulate any PHP code inside <?php ?> tags, while any non-PHP has to be outside of them.

The cleanest way is this:

<?php if(function_exists('bcn_display') && !is_archive()): ?>
  <div class="breadcrumbs">
    <?php bcn_display(); ?>
  </div><br />
<?php endif; ?>

These are so called short tags. They are designed to make the code look prettier. Without them, it would need to look like this:

<?php if(function_exists('bcn_display') && !is_archive())
{ ?>
<div class="breadcrumbs">
<?php bcn_display(); ?>
</div><br />
<?php    }?>

This is much harder to read, so you should probably go for the first version, but the second might help you understand what's wrong with your code.

Upvotes: 1

Satish Sharma
Satish Sharma

Reputation: 9635

Use this

 <?php 
    if(function_exists('bcn_display') && !is_archive())
    {
 ?>

        <div class="breadcrumbs">
         <?php bcn_display();  ?>
        </div>
        <br />
 <?php
    }
 ?>

OR

<?php 
        if(function_exists('bcn_display') && !is_archive())
        {


            echo '<div class="breadcrumbs">';
             bcn_display();  ?>
            echo '</div>
            <br />';

        }
     ?>

Upvotes: 1

robi
robi

Reputation: 123

it looks like you want to use a shorthand php statement. You should do this way:

<?php if(function_exists('bcn_display') && !is_archive()):?>
    <div class="breadcrumbs">
    <?php bcn_display(); ?>
    </div><br />
<?php endif;?>

Upvotes: 1

Anonymous
Anonymous

Reputation: 12027

Just close the php tag before the HTML then open it again at the end of the HTML:

<?php if(function_exists('bcn_display') && !is_archive())
{ ?>
    <div class="breadcrumbs">
    <?=bcn_display();?>
    </div><br />
<?php }?>

That is also assuming that you want to echo the result of bcn_display() inside the div.

Upvotes: 1

user632287
user632287

Reputation:

You'll want to do it like this:

    <?php if(function_exists('bcn_display') && !is_archive())
    { ?>
<div class="breadcrumbs">
    <?php bcn_display(); ?>
</div><br />
    <?php }?>

Note the <?php and ?>

Upvotes: 3

Ryan
Ryan

Reputation: 3582

You need to output the html, not just whack it in among the rest of the code otherwise PHP will try to execute it.

Example:

echo '<div class="breadcrumbs">';

Depending on what your bcn_display(); function does (it displays something judging from the function name) you could output the containing div element within that function, with the rest of the output.

Upvotes: 0

Related Questions