PHP如何处理白色空间

时间:2014-07-02 05:16:42

标签: php

我想知道php如何处理空格。减少我的PHP编码中的空格

  • 会减少页面加载时间吗?
  • 会减少视图来源中的空白区域吗?
  • 会减少我的程序编译时间吗?

我的代码示例

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

在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>';

?>

我的实时编码部分

<?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查看来源

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" />

行号1到4是这样的空白区域,很多都在我的视图源中

4 个答案:

答案 0 :(得分:1)

实际上PHP脚本中的白色没有问题,如果你在php脚本之前和之后使用空格,这意味着

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

?>

-----Space ---

会出现一个名为Header Problems的错误。您将收到如下所示的错误

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

尽量避免像

这样的空白区域
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" />

有关详细信息,请查看以下链接: How to fix "Headers already sent" error in PHP

答案 1 :(得分:0)

PHP 忽略空格。页面来源中的空白区域不是来自您的php文件。您错过了某些内容

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>

答案 2 :(得分:0)

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

答案 3 :(得分:-1)

问:会减少页面加载时间吗? 答:没有

问:我的视图源会减少空格吗? 答:没有

问:会减少我的程序编译时间吗? 答:没有