Raaga
Raaga

Reputation: 599

How PHP treats White Spaces

I would like to know how php treats white spaces. Reducing a white spaces in my php coding

Example of my code

<?php 
include('head.php');
-- Here is white space --
include('body.php');
-- Here is white space --
include('footer.php');
?>

inside body.php

<?php 

echo $lhs = '<div class="lhs">my content</div>';



   white

   spaces..


echo $main = '<div class="main">my content</div>';

   white

   spaces..


echo $footer ='<div class="footer">my content </div>';

?>

My live Coding Part

<?php include('config.php'); ?>
<!DOCTYPE HTML> 
<html>
<?php include(ROOT.'head.php'); ?>
<body>  
<div id="container">        
    <?php
    // header items => logo , searchbar , compare , myaccount/login
    include(ROOT.'headeritems.php');
    //WEBSITE MENUS
    include(ROOT.'sitemenus.php');    
    if ($page!="index"){ include(ROOT.'otherpagenotification.php'); }
    ?>        
        <div id="breadO" <?php echo ($page!="index")? 'class="bannerShd2"' :''; ?> >
        <div id="bread">
            <?php include(BREADCRUMBS); ?>
        </div>
        </div>
</div>

HTML View Source

1.
2.
3.       
4.    
5.  <!DOCTYPE HTML> 
6.  <html>
7.  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Samsung Galaxy Note 3</title>
    <link rel="shortcut icon" href="favicon.ico" />
    <meta name="description" content="" />
    <meta name="author" content="name" />

<meta name="rating" content="General" />

line number 1 to 4 is the white space like this many are in my view source

Upvotes: 0

Views: 1621

Answers (5)

theking2
theking2

Reputation: 2823

A number of things to consider. If the white spaces consern you or result in errors you should write your sameple code like:

<?php

include('config.php');

?><!DOCTYPE HTML> 
<html><?php

include(ROOT.'head.php');

?><body>  
<div id="container"><?php

    // header items => logo , searchbar , compare , myaccount/login
    include(ROOT.'headeritems.php');
    //WEBSITE MENUS
    include(ROOT.'sitemenus.php');    
    if ($page!="index"){ include(ROOT.'otherpagenotification.php'); }

    ?><div id="breadO" <?php echo ($page!="index")? 'class="bannerShd2"' :''; ?> >
        <div id="bread"><?php include(BREADCRUMBS); ?></div>
        </div>
</div>

I'm exagerating a bit here and the result is not so readable but that would be it: line breaks inside a php block are not send to the browser everything else is.

Use shorthand for mere output

If the only thing a PHP block does consider the shorthand <?=...?> like:

<h1><?=$title?></h1> or
<h1><?= $title ?></h1>

It will make reading to code so much easier on the (programmer's) eyes.

Ommit trailing close tags

But more importantly: make it a habit to ommit a trailing ?> if no html code is appearing after that!. There is no need for a trailing ?> at the end of a php document (or class file or include file) as the PHP interpreter will very well be able to read and interpret a EOF token. Not doing so will send needless empty lines to the browser.

Upvotes: 0

Patel Yatin
Patel Yatin

Reputation: 149

This Is Space After Calling Html Code File.
Like Inside Function.php Or Our Custome Function File Included(widget File, Meta Box File.etc BackEnd File) Space.
Please Remove Between Two Function Space Like As
WRONG :
<?php
function 1....
?>
[consider As A HTML Space]---------Remove This Part 
<?php
function 2....
?> 
Right :
<?php
function 1....

function 2....
?> No Break Between Two Function

<?php
Code ....
?>
End Check Last Of File Remove Extra Spaces

Upvotes: 0

Dinesh G
Dinesh G

Reputation: 242

Actually inside the PHP script white there is no problem, in case if you use white space before and after php script which means

-------Space-----
<?php

?>

-----Space ---

will make an error called Header Problems. You will get an error like follwing

Warning: Cannot modify header information - headers already sent by (output 
started at /some/file.php:12) in /some/file.php on line 23

Try to avoid white space like

1.
2.
3.       
4.    
5.  <!DOCTYPE HTML> 
6.  <html>
7.  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Samsung Galaxy Note 3</title>
    <link rel="shortcut icon" href="favicon.ico" />
    <meta name="description" content="" />
    <meta name="author" content="name" />

<meta name="rating" content="General" />

For more details please check the following link: How to fix "Headers already sent" error in PHP

Upvotes: 2

vimal1083
vimal1083

Reputation: 8671

PHP ignores white spaces. The White spaces in your page source is not from your php file.You are missing out of some thing

FYI

<html>



<?php
echo 1;
/*

These white space will be ignored by php , It won't affect your HTML code



*/
echo 2;
?>



<!-- But this white spaces above or below PHP tag will be reflected in HTML source code -->


</html>

Upvotes: 1

Sultan
Sultan

Reputation: 647

Q: Will reduce the page loading time ? A: No

Q: Will reduce empty spaces in my view source ? A: No

Q: Will reduce my program compiling time ? A: No

Upvotes: 0

Related Questions