5 Replies - 579 Views - Last Post: 22 October 2009 - 12:50 PM Rate Topic: -----

#1 jnthn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 09

why are spaces getting stripped?

Posted 21 October 2009 - 05:58 PM

Hi,

I'm trying to understand why some spaces in my output are being stripped and others are not. Here is my code:

index.php:

<?php include '/PATH/TO/header.php'; ?>
<html>
   <head>
	  <title>Testing</title>
   </head>
   <body>
	  this is a line from index.php
   </body>
</html>



header.php:

<div style="text-align:center"><font size=4> "Welcome" </font>
   <br/>
   <?php
	  echo 'Today is ';
	  echo date("l");
	  echo ', ';
	  echo date("F d");
	  echo ' ';
	  echo date("Y");
   ?>
</div>
<?php
   echo '<br/>';
   echo 'CWD	=	';
   echo getcwd();
   echo '<br/>';
?>



The output looks (something) like this:

"Welcome"
Today is Wednesday, October 21 2009

CWD=/PATH/TO/index.php
this is a line from index.php


So, why do the spaces in the output line, "Today is .....", show up while the spaces before and after the '=' in the output line, "CWD=..." get stripped? Also, (perhaps this should be a clue?) if I relocate the "echo '<br/>';" line from above "echo 'CWD = ';" to below "echo date("Y");" the line break doesn't get echoed.

-jnthn

Is This A Good Question/Topic? 0
  • +

Replies To: why are spaces getting stripped?

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: why are spaces getting stripped?

Posted 21 October 2009 - 06:03 PM

Because in HTML, whitespace is meaningless.

If you want the spaces to actually exist when you're outputting text, you should use &nbsp; instead of real spaces, or simply format everything using <pre> tags. Within <pre> (preformatted paragraph) everything is taken literally, so each space will be represented as a space in the browser. Same goes for tabs (\t) and line breaks (\n).

This post has been edited by RudiVisser: 21 October 2009 - 06:03 PM

Was This Post Helpful? 1
  • +
  • -

#3 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: why are spaces getting stripped?

Posted 21 October 2009 - 06:07 PM

View Postjnthn, on 21 Oct, 2009 - 04:58 PM, said:

Hi,

I'm trying to understand why some spaces in my output are being stripped and others are not. Here is my code:

index.php:

<?php include '/PATH/TO/header.php'; ?>
<html>
   <head>
	  <title>Testing</title>
   </head>
   <body>
	  this is a line from index.php
   </body>
</html>



header.php:

<div style="text-align:center"><font size=4> "Welcome" </font>
   <br/>
   <?php
	  echo 'Today is ';
	  echo date("l");
	  echo ', ';
	  echo date("F d");
	  echo ' ';
	  echo date("Y");
   ?>
</div>
<?php
   echo '<br/>';
   echo 'CWD	=	';
   echo getcwd();
   echo '<br/>';
?>



The output looks (something) like this:

"Welcome"
Today is Wednesday, October 21 2009

CWD=/PATH/TO/index.php
this is a line from index.php


So, why do the spaces in the output line, "Today is .....", show up while the spaces before and after the '=' in the output line, "CWD=..." get stripped? Also, (perhaps this should be a clue?) if I relocate the "echo '<br/>';" line from above "echo 'CWD = ';" to below "echo date("Y");" the line break doesn't get echoed.

-jnthn


I don't have an absolute answer to your question. First of all, echo sends the text to HTML and then HTML takes over to render it to the screen. HTML will only render 1 forced space at a time so even if you type:

echo "Hello				 World";



The output will be Hello World

To force spaces, use &nbsp;

Second, I don't know if you were testing something or what, but you really have many useless echo statements. Much of that can go into one statement.

<div style="text-align:center"><font size=4> "Welcome" </font><br />
<?php

echo "Today is " . date("l, F d Y");

?>
</div>
<?php

echo "<br />CWD = " . getcwd() . "<br />";

?>


Was This Post Helpful? 1
  • +
  • -

#4 jnthn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 09

Re: why are spaces getting stripped?

Posted 21 October 2009 - 07:55 PM

Thanks, folks. My whitespace question has been answered. Any clue about why, in header.php, placing a line break at the end of the first php block doesn't work but placing it at the top of the second php block does?

-jnthn

EDIT: I guess it has to do with the <div> tag.

This post has been edited by jnthn: 21 October 2009 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 bmoyles0117  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 27
  • Joined: 17-April 09

Re: why are spaces getting stripped?

Posted 22 October 2009 - 07:41 AM

View Postjnthn, on 21 Oct, 2009 - 06:55 PM, said:

Thanks, folks. My whitespace question has been answered. Any clue about why, in header.php, placing a line break at the end of the first php block doesn't work but placing it at the top of the second php block does?

-jnthn

EDIT: I guess it has to do with the <div> tag.


Not quite sure what you mean, but one note, learn to use HEREDOC

You can create much nicer output.

The date() function can also accept punctuations so that you can declare all of your dates within 1 call.

<?php 
$date = date('l, F d Y');
$cwd = getcwd();

echo <<<TEXT
<div style="text-align:center">
	<font size=4> "Welcome" </font>
	<br/>
	Today is $date
</div>
<br />
CWD   =   $cwd
<br />
TEXT;


Was This Post Helpful? 0
  • +
  • -

#6 jnthn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 09

Re: why are spaces getting stripped?

Posted 22 October 2009 - 12:50 PM

Quote

Not quite sure what you mean, but one note, learn to use HEREDOC


Untidy as it is, if I modify header.php as follows:

<div style="text-align:center"><font size=4> "Welcome" </font>
   <br/>
   <?php
	  echo 'Today is ';
	  echo date("l");
	  echo ', ';
	  echo date("F d");
	  echo ' ';
	  echo date("Y");
	  echo '<br/>';  // <---------- Add this line break
   ?>
</div>
<?php
   // echo '<br/>';   <---------- Remove this line break
   echo 'CWD	=	';
   echo getcwd();
   echo '<br/>';
?>




the output from "echo CWD..." gets shifted UP one line. i.e. The line break from the newly added "echo '<br/>" doesn't get displayed. Not that this matters much. I just like to understand why things behave the way they do.

I will look into HEREDOC.

Thanks.


-jnthn
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1