Home » Php » How to add extra whitespace in PHP?

How to add extra whitespace in PHP?

Posted by: admin April 23, 2020 Leave a comment


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.

How to&Answers:

Like this:

 str_repeat(' ', 5); // adds 5 spaces


To render more than one whitespace on most web browsers use   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.


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.


for adding space character you can use

echo "\x20\x20\x20"; 


use this one. it will provide 60 spaces. that is your second parameter.

 echo str_repeat("&nbsp;", 60); 


Use str_pad function. It is very easy to use. One example is below:


$input = "Alien";
echo str_pad($input, 10);                      // produces "Alien     "



pre is your friend.

<?php // code goes here


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



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

echo "\n\n";

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

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




  easy formatting<br /> 
  across multiple lines!


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.


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 (‘).


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


You can also use this

str_repeat("\x20", $numberOfRepeats);


when you add more space between double quotes or single quotes PHP takes only one white space ,so if you want to add more white space between words or strings use tab ‘\t’ sequence

echo "\t";