1

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 Answers 4

6

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>";
Sign up to request clarification or add additional context in comments.

Comments

1

You've nested some php tags

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

Comments

0

or this:

echo "<a href=\"http://{bloginfo('template_url')}/subdirectory/file.php\">test link 1</a>";

1 Comment

oops! sorry then! my mistake!
0

It was because you were using within tags and not telling it to echo either. The below code will work - I have commented out your old line and added a new line that will work

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.