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>