Link to home
Start Free TrialLog in
Avatar of LB1234
LB1234

asked on

What is the best way to write this short and simple PHP/HTML code?

If the if statement is true, I want the HTML below displayed, but there's gotta be a better than echoing out each line of HTML, right?

<?php

if (!isset($_GET["id"])) {

// then display this HTML below
 <div id="menu">
        <ul>
            <li><a href="#">Pending Expenses</a></li>
            <li><a href="#">Open Report</a></li>
        </ul>
</div>
}

?>

Open in new window



As always, thanks!
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Loganathan Natarajan
This will also do the stuff,

<?php

if (!isset($_GET["id"])) {
?>

// then display this HTML below
 <div id="menu">
        <ul>
            <li><a href="#">Pending Expenses</a></li>
            <li><a href="#">Open Report</a></li>
        </ul>
</div>
<?php
}

?>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LB1234
LB1234

ASKER

Logu, that doesn't make sense why that would work!  Can you please explain the logic?
Avatar of LB1234

ASKER

Ray, ok so something like this?

<?php

if (!isset($_GET["id"])) {

// then display this HTML below

echo <<< EOT
 <div id="menu">
        <ul>
            <li><a href="#">Pending Expenses</a></li>
            <li><a href="#">Open Report</a></li>
        </ul>
</div>

EOT;

}

?>

Open in new window

The learning resources in this article will help you get a handle on this, and many other PHP-related questions.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
something like this?
Yes, but you'll find that the code is easier to maintain if you create your variables separately, away from the presentation (echo).  That's why I assigned a variable to the HEREDOC string then echo the variable, instead of echo the HEREDOC
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LB1234

ASKER

Chris, but isn't anything not in PHP blocks automatically displayed?  That's what's throwing me off here.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On the other hand, when you fix and maintain other people's code, you can get pretty good at reading code like Ray's last example where everything is all inter-mixed.
Dave :-)
I make most of my money doing that, Ray.  Very educational and profitable.
Yeah, I had a similar experience last year.  When I see a code smell, I go hourly immediately!
hourly?  Is there another way???

Actually, I will do new jobs or small projects on a fixed quote when I know how it's going to go.  I'm very good at the things I've already done 100 times...
Yeah me too.  It takes about 100 times to get proficiency at my age ;-)
But Ray!!  You're Still a Young Man....
Erm :-)