2012-07-28 125 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=""/>'); 
    } 
?>