Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-24-2012, 12:01 AM   PM User | #1
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Question PHP Whitespace

I'm sure that I'm just overlooking something, but I keep getting whitespace when I execute my PHP. Here are a few examples of what causes the problem:

PHP Code:
<p>This is a random paragraph.</p>
<?php
if($active){
    echo 
trim(file_get_contents('../misc/footer.php'));
    echo 
'</div>';
}
?>
(Causes whitespace before the first echo.)


PHP Code:
<?php
echo '<div id="header"><img alt="My Wesbite Logo" src="http://www.example.com/images/logo.png" draggable="false" />';
include 
'../misc/menu.php';
echo 
'<div id="message">'.$msg.'</div></div>';
?>
(Causes whitespace after the first echo, but before the included content)



So what's the fix? Thanks!!!
Mooseman is offline   Reply With Quote
Old 11-24-2012, 12:51 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
As any whitespace in the HTML is ignored by the browser (treated as if it were a single space which then only makes a difference when it is between inline tags) the whitespace is not coming from the HTML/PHP you have posted (which is using block tags where whitespace is completely ignored). Check the CSS associated with the resulting HTML tags as something there is adding padding or margins that appear as whitespace on the page.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 11-24-2012, 01:39 AM   PM User | #3
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
Check the CSS associated...
I can assure you that the PHP is causing the whitespace. I can see in the Chrome dev tools a "" where the whitespace appears.
__________________
Ask me anything about CSS! Learning PHP.
Mooseman is offline   Reply With Quote
Old 11-24-2012, 01:53 AM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
PHP doesn't cause whitespace as it is finished and done with before the page displays in the browser. It is possible for the whitespace to be in the HTML between inline elements but generally it comes from CSS.

To be able to determine if it comes from the HTML that the PHP is generating we'd need to see the HTML - not the PHP.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 11-24-2012, 02:32 AM   PM User | #5
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
PHP doesn't cause whitespace
See the attached image. This is from Chrome dev tools. Thanks!

Note that the <ul... comes from the include. The include is a SINGLE LINE file.
Attached Thumbnails
Click image for larger version

Name:	whitespace.gif
Views:	16
Size:	7.1 KB
ID:	11723  
__________________
Ask me anything about CSS! Learning PHP.
Mooseman is offline   Reply With Quote
Old 11-24-2012, 02:58 AM   PM User | #6
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
You can get whitespace if you indent <?php. You've probably already checked for that, but no harm in bringing it up.

Also, is this a problem outside of the Chrome dev-tool? Those double-quotes seem strange to me, since a whitespace should be more like " ". It strikes me more as a newline, but I have no idea why the Chrome dev-tool would quote nothing.
Custard7A is offline   Reply With Quote
Old 11-24-2012, 04:12 AM   PM User | #7
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
Isn't there white space between the closing paragraph tag and the opening php tag?
KULP is offline   Reply With Quote
Old 11-24-2012, 05:03 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You are seeing double quotations there, not whitespace. Whitespace is ignored both by PHP and by HTML unless its in a preformatted block, or the CSS dictates it should be rendered.
Those are caused by the included files or the file output by the read. Check for the relevant lines in footer and menu.php files for the quotes. It can be caused by any output that you have, but if you can track it to where it lies relative to this (looks/sounds like the first lines of the code), check the files for the first lines of output to see what's in them.
Fou-Lu is offline   Reply With Quote
Old 11-24-2012, 09:47 PM   PM User | #9
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Question

Quote:
Originally Posted by Custard7A View Post
Also, is this a problem outside of the Chrome dev-tool?
Quote:
Originally Posted by Fou-Lu View Post
You are seeing double quotations there, not whitespace. Whitespace is ignored both by PHP and by HTML unless its in a preformatted block, or the CSS dictates it should be rendered.
Those are caused by the included files or the file output by the read. Check for the relevant lines in footer and menu.php files for the quotes. It can be caused by any output that you have, but if you can track it to where it lies relative to this (looks/sounds like the first lines of the code), check the files for the first lines of output to see what's in them.
It renders the same as <br /><br />. If the PHP echo has text inside it, (e.g., echo '<div id="hello">Paragraph:'; instead of echo '<div id="hello">';) "Paragraph:" is inside those quotes, instead of nothing. I have tried everything with menu.php and footer.php: they are not the problem.
__________________
Ask me anything about CSS! Learning PHP.
Mooseman is offline   Reply With Quote
Old 11-24-2012, 10:37 PM   PM User | #10
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by Mooseman View Post
If the PHP echo has text inside it, (e.g., echo '<div id="hello">Paragraph:'; instead of echo '<div id="hello">';) "Paragraph:" is inside those quotes, instead of nothing.
Unless I'm misunderstanding you, that is not whitespace at all but rather how Chrome formats the string to make it easier to edit in dev tools. If you view the source of the page(CTRL-U) instead of viewing it in the dev tools you should see no extra whitespace inside of the tags. As others have stated, echo doesn't output any extra spaces with the default PHP configuration or any standard configuration that I've seen.
Inigoesdr is offline   Reply With Quote
Old 11-26-2012, 12:30 AM   PM User | #11
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Quote:
Originally Posted by Inigoesdr View Post
If you view the source of the page instead of viewing it in the dev tools you should see no extra whitespace inside of the tags.
I do not see any whitespace in the source of the page, but it IS rendered in the page by Chrome, FF, IE, etc.
__________________
Ask me anything about CSS! Learning PHP.
Mooseman is offline   Reply With Quote
Reply

Bookmarks

Tags
include, php, whitespace

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:44 AM.


Advertisement
Log in to turn off these ads.