2012-07-28 108 views
0

我試圖實現下面的腳本來打印所謂的頁面上的圖像,但它相反導致問題的頁面。PHP打印腳本錯誤

<?php 
    if ($currentpage == '/services/') { 
     print("<img src="path/to/services.png" alt=""/>"); 
    }, 
     if ($currentpage == 'contact.php') { 
     print("<img src="path/to/image.png" alt=""/>") 
    }, 
    else { 
     print("<img src="path/to/image.png" alt=""/>") 
    } 
    ?> 

的錯誤信息是:

Parse error: syntax error, unexpected T_STRING in /home/master/public_html/wp-content/themes/siteripe-001/othe-rpages.php on line 173

回答

1

如果塊中只有一行代碼,則不需要括號。
在你的情況下,更有效地使用一個if塊與elseifelse,而不是幾個if塊:你總是測試相同的變量($當前頁),所以只有一個街區將被執行。
我也用單引號替換了一些雙引號。

<?php 
    if ($currentpage == '/services/') 
     print('<img src="path/to/services.png" alt=""/>'); 
    elseif ($currentpage == 'contact.php') 
     print('<img src="path/to/image.png" alt=""/>'); 
    else 
     print('<img src="path/to/image.png" alt=""/>'); 
?> 

不應該第二圖像是contact.png而不是image.png

+0

按照你的建議使用它,但它仍然不打印圖像,它顯示它是一個死鏈接。 – 2012-07-29 01:23:12

+0

檢查圖像路徑和文件名。 – Jocelyn 2012-07-29 08:03:55

3

你已經錯過了分號在最後兩個打印語句的結束。 而且還需要轉義雙引號。您可以在doube引號之前使用反斜槓字符來轉義它。

0

你忘記了一些分號,而且你有太多的雙引號。您需要嵌套報價,如下所示:

<?php 
    if ($currentpage == '/services/') { 
     print('<img src="path/to/services.png" alt=""/>'); 
    }, 
     if ($currentpage == 'contact.php') { 
     print('<img src="path/to/image.png" alt=""/>'); 
    }, 
    else { 
     print('<img src="path/to/image.png" alt=""/>'); 
    } 
?>