PHP Adding a Line Break

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

PHP Adding a Line Break

Post by techkid »

Hello,

This is how my PHP code is written:

Code: Select all

         echo '<li'; if($page == "index.html") echo ' class="selected"'; echo '><a href="' .HOME_URL. '">Home</a></li>';
         echo '<li'; if($page == "about.html") echo ' class="selected"'; echo '><a href="' .ABOUT_URL. '">About</a></li>';
         echo '<li'; if($page == "portfolio.html") echo ' class="selected"'; echo '><a href="' .PORTFOLIO_URL. '">Portfolio</a></li>';
         echo '<li'; if($page == "contact.php") echo ' class="selected"'; echo '><a href="' .CONTACT_URL. '">Contact</a></li>';
and Here is the source code of my HTML:

Code: Select all

<li><a href="home">Home</a></li><li><a href="about">About</a></li><li><a href="portfolio">Portfolio</a></li><li><a href="contact">Contact</a></li>
It appears as 1 line, I want each <lI> to appear in a new line.

Any suggestions?

Thank you.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: PHP Adding a Line Break

Post by litebearer »

simply add a <br/> after each </li> in your php coding.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: PHP Adding a Line Break

Post by internet-solution »

It seems you want to keep your HTMl source code tidy. Use line break (\n\r) in php echo's. Example

Code: Select all

echo '<li'; if($page == "index.html") echo ' class="selected"'; echo '><a href="' .HOME_URL. '">Home</a></li>\n\r';
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP Adding a Line Break

Post by John Cartwright »

It's actually \r\n, but even \n would suffice.

Better yet, use PHP's new line constant, PHP_EOL.
Post Reply