2013-12-12 66 views
-1

我困住了一個問題。如果條件內部的PHP變量

我在PHP變量裏面有HTML元素。現在我想如果條件內的HTML元素。

我的代碼是這樣的。

<?php 
    $x = 1; 
    $html = 
    '<style> 
    b 
    { 
     paading:0; 
     margin:0; 
    } 
    </style> 

    <table width="790px" style="font-family:Open Sans;"> 

      <!--Want If Condition Here--> 

      if($x != 1) 
      { 

       <tr> 
       <td colspan="2"> 
       My Separate Portion 
       </td> 
       </tr> 

      } 

    <strong></strong> 

    <tr> <td colspan="2"> <hr style="background-color:#E44510; height:10px;"> </td> </tr>'; 

    echo $html; 
    ?> 

如果任何人都可以幫助我,它非常適合。

+0

'$字符串= '富'。 ($ condition?'if true':'if false')。 '酒吧';'。 – h2ooooooo

回答

1
<?php 
    $x = 1; 
    $html = 
    '<style> 
    b 
    { 
     paading:0; 
     margin:0; 
    } 
    </style> 

    <table width="790px" style="font-family:Open Sans;">';  
      if($x != 1) 
      { 

       $html .= '<tr> 
       <td colspan="2"> 
       My Separate Portion 
       </td> 
       </tr>'; 

      } 

    $html .= '<strong></strong> 

    <tr> <td colspan="2"> <hr style="background-color:#E44510; height:10px;"> </td> </tr>'; 

    echo $html; 
    ?> 
+0

謝謝,我不知道爲什麼我沒有這樣想。任何方式非常感謝... – user3065100

+0

歡迎@ user3065100 –

2

否則你可以輕鬆地使用這個..

<?php 
$x = 1; 
?> 
    <style> 
    b 
    { 
     paading:0; 
     margin:0; 
    } 
    </style> 

    <table width="790px" style="font-family:Open Sans;"> 

      <!--Want If Condition Here--> 

<?php   
     if($x != 1) 
      { 
?> 
       <tr> 
       <td colspan="2"> 
       My Separate Portion 
       </td> 
       </tr> 
<?php 
      } 
?> 
    <strong></strong> 

    <tr> <td colspan="2"> <hr style="background-color:#E44510; height:10px;"> </td> </tr> 
+0

我認爲這是最好的方式去!只需關閉您的PHP標籤以顯示HTML併爲if語句打開新的PHP標籤即可! – iSenne