I have the following code, which works exactly as I expect it to:

<?php if ($a - $b < 1)
echo $text;
elseif ($a - $b >= 1)
echo $a - $b;
else
echo $a; ?>

However, now I also want to add a bit of HTML to the echo depending on the result of the IF statement.

<?php if ($a - $b < 1)
echo $text;
elseif ($a - $b >= 1)
echo $a - $b;
<input type="button" value="<?php echo $button_txt; ?>" id="button" class="button" />
else
echo $a;
<input type="button" value="<?php echo $button2_txt; ?>" id="button" class="button" /> ?>

When I do this, I get the following error: Parse error: syntax error, unexpected '<' on line 213 (line 213 is referring to the first <input> line in the code above).

Is there any way to include both the variables, and the HTML in the echo part of the IF statement?

share|improve this question
Are the HTML <input> tags supposed to be part of the if/else chain? if so you'll also need {} or you'll get a syntax error at the else. – Michael Berkowski May 16 '12 at 18:11

5 Answers

PHP and HTML are different languages which get interpreted differently. <input type="button"> has no meaning inside PHP and is actually invalid. PHP code goes between <?php and ?> tags, HTML goes outside those tags or between quotes (making it a string).

A solution could be:

<?php

if ($a - $b < 1)
    echo $text;
elseif ($a - $b >= 1)
{
    echo $a - $b;
    echo '<input type="button" value="' . $button_txt . '" id="button" class="button" />';
}
else
{
    echo $a;
    echo '<input type="button" value="' . $button2_txt . '" id="button" class="button" />';
}

?>

Note that the actual HTML code is now between ' and ' since within PHP it is seen as regular text. Also, I added the brackets { and }: when an if is followed by multiple instructions, the brackets are needed to specify the beginning and ending of the instructions that belong to the if. The same applies for the else.

share|improve this answer

Edit: Better formatting

<?php if ($a - $b < 1): ?>
  <?php echo $text ?>
<?php elseif ($a - $b >= 1): ?>
  <?php echo $a - $b ?>
  <input type="button" value="<?php echo $button_txt ?>" id="button" class="button" />
<?php else: ?>
  <input type="button" value="<?php echo $button2_txt ?>" id="button" class="button" />
<?php endif ?>
share|improve this answer
Thanks @samy this worked perfectly!! – scd1982 May 16 '12 at 18:31
Glad it helped. Can you accept my question please? – Samy Dindane May 16 '12 at 19:48

You need to close your php statements before you start a html code so your code should look something like this :

<?php if ($a - $b < 1)
echo $text;
elseif ($a - $b >= 1)
echo $a - $b;
?>
<input type="button" value="<?php echo $button_txt; ?>" id="button" class="button" />
<?php else
echo $a;
?>
<input type="button" value="<?php echo $button2_txt; ?>" id="button" class="button" /> ?>
share|improve this answer

you need to echo your lines of html:

<?php if ($a - $b < 1)
echo $text;
elseif ($a - $b >= 1)
echo $a - $b;
echo "<input type=\"button\" value=\"$button_txt\" id=\"button\" class=\"button\" />";
else
echo $a;
echo "<input type=\"button\" value=\"$button2_txt\" id=\"button\" class=\"button\" /> ?>
share|improve this answer

try this

<?php if ($a - $b < 1)
echo $text;
elseif ($a - $b >= 1)
{
echo $a - $b;
?>
<input type="button" value="<?php echo $button_txt; ?>" id="button" class="button" />
<?php
else
 {
echo $a;
?>
<input type="button" value="<?php echo $button2_txt; ?>" id="button" class="button" />
<?php } ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.