html链接中的php函数
我试图通过在HTML链接中使用PHP函数来调用网站根目录 我已经创建了下面的bloginfo函数,正确的链接输出是 调用函数的两种方法相似,但方法1不起作用,方法2起作用 请有人解释为什么方法1不起作用,并提出解决方案。谢谢html链接中的php函数,php,html,function,hyperlink,Php,Html,Function,Hyperlink,我试图通过在HTML链接中使用PHP函数来调用网站根目录 我已经创建了下面的bloginfo函数,正确的链接输出是 调用函数的两种方法相似,但方法1不起作用,方法2起作用 请有人解释为什么方法1不起作用,并提出解决方案。谢谢 <?php function bloginfo($show) { switch ($show) { case 'template_url': echo "www.example.co.uk"; break; } } // Me
<?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>
更新
谢谢大家的帮助。不幸的是,由于某种原因,我无法得到代码行上方的常见答案,因为“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 PHP Include method.
define("URL", "www.example.com", true);
// Method 1 - works!
echo "<a href=\"http://".URL."/subdirectory/file.php\">test link 1</a>";
?>
<html>
<body>
<!-- Method 2 - works! -->
<a href="http://<?php echo URL; ?>/subdirectory/file.php">test link 2</a>
</body>
</html>
双引号字符串使第一个方法中的php块成为纯文本字符串。试试这个:
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">test link 1</a>";
双引号字符串使第一个方法中的php块成为纯文本字符串。试试这个:
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">test link 1</a>";
您已经嵌套了一些php标记
echo "<a href=\"http://" .bloginfo('template_url') . "/subdirectory/file.php\">test link 1</a>";
您已经嵌套了一些php标记
echo "<a href=\"http://" .bloginfo('template_url') . "/subdirectory/file.php\">test link 1</a>";
或者这个:
echo "<a href=\"http://{bloginfo('template_url')}/subdirectory/file.php\">test link 1</a>";
或者这个:
echo "<a href=\"http://{bloginfo('template_url')}/subdirectory/file.php\">test link 1</a>";
这是因为您使用的是内部标记,而没有告诉它也要回显。下面的代码将工作-我已经注释掉你的旧行,并添加了一个新的行将工作
<?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>";
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">test link 1</a>"; <--- changed the above line to this... now both this and the lower line work
?>
<html>
<body>
<!-- Method 2 - works! -->
<a href="http://<?php bloginfo('template_url'); ?>/subdirectory/file.php">test link 2</a>
</body>
</html>
这是因为您使用的是内部标记,而没有告诉它也要回显。下面的代码将工作-我已经注释掉你的旧行,并添加了一个新的行将工作
<?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>";
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">test link 1</a>"; <--- changed the above line to this... now both this and the lower line work
?>
<html>
<body>
<!-- Method 2 - works! -->
<a href="http://<?php bloginfo('template_url'); ?>/subdirectory/file.php">test link 2</a>
</body>
</html>
哎呀!对不起!我错了!哎呀!对不起!我错了!