Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: PHP Whitespace

  1. #1
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    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!!!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    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.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    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 Attached Thumbnails PHP Whitespace-whitespace.gif  

  • #6
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Isn't there white space between the closing paragraph tag and the opening php tag?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #9
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    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.

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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.

  • #11
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •