probablybest
probablybest

Reputation: 1445

Editing php so I can insert HTML?

I have some php where i would like to insert html where it says //do stuff.

<?php $cat = 8; //8 is changed to be the category you need
    if (in_category($cat)){
        //do stuff
    }
?>

I would ideally like it be something like. But not sure where to close and reopen the php tags.

<?php $cat = 8; //8 is changed to be the category you need
    if (in_category($cat)){
        <div id="testing">Im some test content here</div>
    }
?>

Upvotes: 1

Views: 83

Answers (5)

domdomcodecode
domdomcodecode

Reputation: 2443

You don't necessarily have to close and open the php tags, you can use echo instead. Just make sure to escape the quotes with a \

<?php
    $cat = 8;
    if (in_category($cat)){
        echo "<div id=\"testing\">Im some test content here</div>";
    }
?>

Otherwise, you could close/reopen the php tags like so:

<?php
    // php
?>

<!-- HTML -->

<?php
    // more php
?>

The benefit of echoing, however is you can easily slip in some php variable's value.

<?php
    $name = "John";
    echo "<b>$name</b>";
?>

Upvotes: 4

bprayudha
bprayudha

Reputation: 1034

<?php
  $cat = 8;
  if (in_category($cat)) {
?>
<div id="testing">Im some test content here</div>
<?php } ?>

div#testing will be printed if the in_category($cat) return TRUE.

Upvotes: 1

rvandoni
rvandoni

Reputation: 3397

you can do it in two different ways.

1)

<?php
    $cat = 8; //8 is changed to be the category you need
    if (in_category($cat)){
?>
    <p>some html here</p>
<?php } ?>

2)

<?php
    $cat = 8; //8 is changed to be the category you need
    if (in_category($cat)){
        echo "<p>some html here</p>";
    }
?>

Upvotes: 1

DorianFM
DorianFM

Reputation: 4483

If you imagine your document is by default HTML the begin and end PHP blocks. So you can split your block into two blocks of php as follows, and it will still keep it's program login

<?php 
    $cat = 8; //8 is changed to be the category you need
    if (in_category($cat)){
       ?>
           <div id="testing">Im some test content here</div>
       <?php    
    }
?>

It's just stylistically kind of awkward, but that's the way PHP goes.

Upvotes: 1

92tonywills
92tonywills

Reputation: 859

The easiest way to achieve this is using the php echo function. More on the echo function here.

<?php $cat = 8; // 8 is changed to be the category you need
    if (in_category($cat)) {
      echo "<div id='testing'>I am some test content.</div>"
    } // if
?>

Upvotes: 2

Related Questions