簡體   English   中英

PHP如何對待空白

[英]How PHP treats White Spaces

我想知道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是空白,在我看來,

實際上,在PHP腳本內白色沒有問題,以防萬一,如果您在php腳本前后使用空白,這意味着

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

?>

-----Space ---

將產生一個錯誤,稱為標題問題。 您將收到以下錯誤

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

有關更多詳細信息,請檢查以下鏈接: 如何修復PHP中的“已發送標頭”錯誤

PHP 忽略空格。 頁面源中的空格不是來自您的php文件。您丟失了某些東西

費耶

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

問:會減少頁面加載時間嗎? 答:不可以

問:會減少我的視圖源中的空白空間嗎? 答:不可以

問:會減少我的程序編譯時間嗎? 答:不可以

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM