tags:

views:

585

answers:

8

I was wondering how can I add extra whitespace in php is it something like \s please help thanks.

Is there a tutorial that list these kind of things thanks.

+1  A: 

Like this:

 str_repeat(' ', 5); // adds 5 spaces
Sarfraz
Sarfraz, you might want to edit your answer to be ` ` :D
macek
@smotchkkiss: Thanks buddy, fixed :)
Sarfraz
+1  A: 

source

<?php
echo "<p>hello\n";
echo "world</p>";

echo "\n\n";

echo "<p>\n\tindented\n</p>\n";

echo "
<div>
  easy formatting<br />
  across multiple lines!
</div>
";
?>

output

<p>hello
world</p> 

<p> 
    indented
</p>

<div> 
  easy formatting<br /> 
  across multiple lines!
</div>
macek
+1  A: 

pre is your friend.

<pre> 
<?php // code goes here

?>
</pre>

Or you can "View Source" in your browser. (Ctrl+U for most browser.)

Yada
I thought it was <a href="http://htmldog.com/ptg/archives/000077.php"> not advisable </a> to use pre ....
SpikETidE
OP is a beginner. He should just pre when learning and debugging PHP.
Yada
+4  A: 

PHP (typically) generates HTML output for a web-site.

When displaying HTML, the browser (typically) collapses all whitespace in text into a single space. Sometimes, between tags, it even collapses whitespace to nothing.

In order to persuade the browser to display whitespace, you need to include special tags like &nbsp; or <br/> in your HTML to add non-breaking whitespace or new lines, respectively.

Oddthinking
+1  A: 

To render more than one whitespace on most web browsers use &nbsp; instead of normal white spaces.

echo "<p>Hello &nbsp;&nbsp;&nbsp; punt"; // This will render as Hello   Punt (with 4 white spaces)
echo "<p> Hello       punt"; // This will render as Hello punt (with one space)

For showing data in raw format (with exact number of spaces and "enters") use HTML <pre> tag.

echo "<pre>Hello        punt</pre>"; //Will render exactly as written here (8 white spaces)

Or you can use some CSS to style current block, not to break text or strip spaces (dunno bout this one)

Any way you do the output will be the same but the browser itself strips double white spaces and renders as one.

George
A: 

you can use the <pre> tag to prevent multiple spaces and linebreaks from being collapsed into one. Or you could use &nbsp; for a typical space (non-breaking space) and <br /> (or <br>) for line breaks.

But don't do <br><br><br><br> just use a <p> tag and adjust the margins with CSS.

<p style="margin-top: 20px;">Some copy...</p>

Although, you should define the styles globally, and not inline as I have done in this example.

When you are outputting strings from PHP you can use "\n" for a new line, and "\t" for a tab.

<?php echo "This is one line\nThis is another line"; ?>

Although, flags like \n or \t only work in double quotes (") not single wuotes (').

seventeen
A: 

is this for display purposes? if so you really should consider separating your display form your logic and use style sheets for formatting. being server side php should really allow providing and accepting data. while you could surely use php to do what you are asking I am a very firm believer in keeping display and logic with as much separation as possible. with styles you can do all of your typesetting.

give output class wrappers and style accordingly.

Matt
A: 

to make your code look better when viewing source

$variable = 'foo';
echo "this is my php variable $variable \n";
echo "this is another php echo here $variable\n";

your code when view source will look like, with nice line returns thanks to \n

this is my php variable foo
this is another php echo here foo
David Morrow