2

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?

1
  • 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. Commented May 16, 2012 at 18:11

5 Answers 5

2

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 ?>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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\" /> ?>

Comments

0

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

Comments

0

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.

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.