我试图通过在HTML链接中使用PHP函数来调用网站root.
我在下面创建了函数bloginfo(),正确的链接输出是http://www.example.com/subdirectory/file.php.
调用函数的两种方法类似,但方法1不起作用,方法2也起作用.
请有人解释为什么方法1不起作用并提出解决方案.谢谢.
<?php
function bloginfo($show) {
switch ($show) {
case 'template_url':
echo "www.example.co.uk";
break;
}
}
// Method 1 - does not work
echo "<a href=\"http://<?php bloginfo('template_url'); ?>/subdirectory/file.php\">test link 1</a>";
?>
<html>
<body>
<!-- Method 2 - works! -->
<a href="http://<?php bloginfo('template_url'); ?>/subdirectory/file.php">test link 2</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
更新
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">
Run Code Online (Sandbox Code Playgroud)
谢谢大家的帮助.不幸的是我无法得到共同的答案(上面的代码行),因为某些原因'www.example.com'将被打印但不作为链接,链接方向只是变成'/subdirectory/file.php' .
为了解决这个问题,我放弃了合并函数,并决定简单地使用下面的PHP Define方法,该方法适用于这两种方法.
<?php
//this line of code can be put into an external PHP file and called using the …Run Code Online (Sandbox Code Playgroud)