0

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.

3
  • echo '<div class="breadcrumbs">' Commented Jan 23, 2014 at 11:51
  • @Vucko thank you can you write this all in a answer then i can choose it as a correct answer. Commented Jan 23, 2014 at 11:52
  • It's OK, see other answers and accept one which you prefer the best :) Commented Jan 23, 2014 at 11:54

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

5 Comments

Or just use one of PHP's output functions. If he has that in many places it's going to get messy.
True. However, I find this more convenient because if you are employing HTML highlighting while developing the code, it would not work if you were to echo the HTML and may help catch errors.
That depends on the IDE/Text editor you use. There are plugins that will highlight the HTML syntax within a string.
Ryan I would avoid using echo or print when using HTML.
@RoyalBg for a one liner such as a containing div? What are you reasons for that?
1

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;?>

Comments

1

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 />';

        }
     ?>

Comments

0

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.

Comments

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.