1. Home
  2. Computing & Technology
  3. PHP / MySQL

Create Links in PHP
How to create links in your PHP documents

By Angela Bradley, About.com

Creating a link with PHP uses the same code as it dose in HTML. Depending on where we are getting our information, and where in our file it is we might present this HTML in a slightly different way.

If you are making a link in a PHP document, that is outside of the PHP brackets, you just use HTML. Here is an example:

<a href="https://twitter.com/angela_bradley">My Twitter</a>
<?php
----- My PHP Code----
?>
If the link needs to be inside of the PHP you have two options. Option one is to end the PHP and then reopen it. Here is an example:

<?php
----- My PHP Code----
?>
<a href="https://twitter.com/angela_bradley">My Twitter</a>
<?php
----- My PHP Code----
?>
The other option is to print or echo the HTML code inside of the PHP. Here is an example:
<?php
Echo "<a href=https://twitter.com/angela_bradley>My Twitter</a>"
?>
Another thing we can do is create a link from a variable. Let's say that the variable $url holds the URL for a website that someone has submitted, or that we have pulled from a database. We can use the variable in our HTML.

<a href="https://twitter.com/angela_bradley">My Twitter</a>
<?php
Echo "<a href=$url>$site_title</a>"
?>
More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Learn PHP
  5. Links in PHP - Links - Build Links in PHP

©2009 About.com, a part of The New York Times Company.

All rights reserved.