Jump to content

Line break in PHP?


Roy766

Recommended Posts

Let me give an example with my previously corrected script:

<?php
  $sfcost = $_REQUEST['swordfootmen'] * 3;
  $lfcost = $_REQUEST['lancefootmen'] * 3;
  $mswcost = $_REQUEST['mountedswordsmen'] * 5;
  $mlwcost = $_REQUEST['mountedlancemen'] * 5;
  $hcost = $_REQUEST['horses'];
  $totalcost = $sfcost + $lfcost + $mswcost + $mlwcost;
  $sfatk = $_REQUEST['swordfootmen'] * 2;
  $lfatk = $_REQUEST['lancefootment'] * 2;
  $mswatk = $_REQUEST['mountedswordsmen'] * 4;
  $mlwatk = $_REQUEST['mountedlancemen'] * 4;
  $hatk = $_REQUEST['horses'];
  $totalatk = $sfatk + $lfatk + $mswatk + $mlwatk + $hatk;
  $compatk = rand(10, 150);
  if ( $totalatk > 150 )
    {
    echo "You spent too much money!";
    }
  else
    {
    echo "Your total attack force is " . $totalatk;
    }
   echo 'The computers total attack force is ' . $compatk;
  if ( $compatk > $totalatk )
    {
    echo 'Sorry, the computer wins...';
    }
  else
    {
    echo 'You win! Congratulations!';
    }
?>

(Don't try just executing that. Requires the HTML to function...)

When you win, you get:

Your total attack force is ---The computers total attack force is ---You win! Congratulations!

Exactly like that. All on one line.

I'd like it with some line breaks inbetween there.

Link to comment
https://forums.phpfreaks.com/topic/107031-line-break-in-php/#findComment-548664
Share on other sites

You should be able to put it in the echo, just like any other HTML code.

 

E.G.


else
    {
    echo "Your total attack force is " . $totalatk . "<br />";
    }
   echo 'The computers total attack force is ' . $compatk . "<br />";
  if ( $compatk > $totalatk )
    {
    echo 'Sorry, the computer wins... <br />';
    }
  else
    {
    echo 'You win! Congratulations! <br />';
    }

Link to comment
https://forums.phpfreaks.com/topic/107031-line-break-in-php/#findComment-548669
Share on other sites

:P also you could add

else
    {
    echo "Your total attack force is <strong> " . $totalatk . "</strong><br />";
    }
   echo 'The computers total attack force is ' . $compatk . "<br />";
  if ( $compatk > $totalatk )
    {
    echo '<font color ="FF0000">Sorry, the computer wins... </font><br />';
    }
  else
    {
    echo '<font color="0000FF">You win! Congratulations!</font> <br />';
    }

just to add some color in there :P

Link to comment
https://forums.phpfreaks.com/topic/107031-line-break-in-php/#findComment-548671
Share on other sites

If you want a linebreak in the display that you see in the browser, as others have mentioned, you want to use

 

<br />

 

But if you want a linebreak in your source-code output, you can use

 

PHP_EOL

 

Using this, it will add a line break into the source code, making your source code easier to read.

Link to comment
https://forums.phpfreaks.com/topic/107031-line-break-in-php/#findComment-548677
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.