Adding HTML Code Inside PHP If Else Statement

brendan-amex
brendan-amex used Ask the Experts™
on
I have several if else statments that run some HTML and PHP code inside of them but it's not liking the < > symbols used in HTML. It makes sense that I should close the PHP before including the HTML code but then how can I run my If Else statement? Any help is greatly appreciated. The code is below and here's the error I get:


Parse error: syntax error, unexpected '<' in /home/content/b/m/c/brendan/html/test/mysql/everything.php on line 14
<?php
$select1 = $_POST['select1'];
$select12 = $_POST['select12'];

session_start();
$_SESSION['sessiontest'] = $select12;

$curdata = $_SESSION['sessiontest'];


if ($curdata=="all")
	{
	<a href="http://www.google.com">Click Here</a>
	}
	
elseif ($curdata=="podium")
	{
	<a href="http://www.yahoo.com">Click Here!</a>
	}
	
else 
	{
	echo "WRONG";
	}
	
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi brendan-amex,

You implementation of the code will trigger an error because PHP does not know how to interpret HTML tags. What you can do is like this:

1st Option: Add an echo command and enclose the HTML tags in a string:
if ($curdata=="all")
	{
	echo '<a href="http://www.google.com">Click Here</a>';
	}
	
elseif ($curdata=="podium")
	{
	echo '<a href="http://www.yahoo.com">Click Here!</a>';
	}

Open in new window


or

2nd Option: Close & reopen PHP tags.

if ($curdata=="all")
	{
?>
	<a href="http://www.google.com">Click Here</a>
<?php
	}
	
elseif ($curdata=="podium")
	{
?>
	<a href="http://www.yahoo.com">Click Here!</a>
<?php
	}

Open in new window


Both suggestions will work.

Hope that helps!

Cheers! =)
Commented:
Two common way:

1. print/echo html portion as string
2. end php code (by ?>) before html portion

Here the second one
<?php
$select1 = $_POST['select1'];
$select12 = $_POST['select12'];

session_start();
$_SESSION['sessiontest'] = $select12;

$curdata = $_SESSION['sessiontest'];


if ($curdata=="all")
        {
?>
        <a href="http://www.google.com">Click Here</a>
<?php
        }
        
elseif ($curdata=="podium")
        {
?>
        <a href="http://www.yahoo.com">Click Here!</a>
<?php
        }
        
else 
        {
        echo "WRONG";
        }
        
?>

Open in new window

Author

Commented:
Oh Lord, I feel silly. Close the PHP code, obviously. Thank you guys. You guys posted within 1 minute of each other, so I'm giving both points. Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial