dou7851 2012-10-31 23:11
浏览 49
已采纳

html链接中的php函数

I am attempting to call a website root by using a PHP function within a HTML link.

I have created the function bloginfo() below and the correct link output is http://www.example.com/subdirectory/file.php.

The two methods of calling the function are similar but method 1 does not work and method 2 works.

Please can somebody explain why method 1 does not work and suggest a solution. Thanks.

<?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>  

Update

echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">

Thanks for your help everyone. Unfortunately I could not get the common answer (above line of code) to work as for some reason 'www.example.com' would be printed but not as a link, and the link direction simply became '/subdirectory/file.php'.

To solve the problem I gave up on incorporating a function and decided to simply use the PHP Define method below which works for both methods.

<?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>  
  • 写回答

4条回答 默认 最新

  • duanjiao6730 2012-10-31 23:13
    关注

    The double quoted string is making your php block in the first method just a plain old text string. Try this:

      echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">test link 1</a>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?