How to Create a Hyperlink in PHP

by Max Power

The PHP scripting language provides Web developera with an open-source (free to use) method for supplying server-side data to website visitors. PHP allows developers to create a dynamic Web browsing experience for users based on their needs and preferences. Developers can use PHP to hard code HTML hyperlinks for users to click on to visit other pages and to generate dynamic links that are retrieved from a database or file.

Creating a Static or Hard-Coded Link

1

Create a new instance of PHP's print statement. This is the command that will direct the HTMLl to render the link and accompanying text onto the users browser screen: <?php print ""; ?>

2

Place an HTML anchor tag inside of the print statement. This is the same anchor tag that is used in traditional HTML coding. Include the target website as well as the link text that will accompany the link: <?php print "<a href = "yourDestitnationPage.html">Click here to visit the destination page.</a>"; ?>

3

Escape the quotation marks with the backslash character. In the previous example, the PHP would have broken because the quotation marks that surround the destination page address would be interpreted as a command to stop the print statement. The backslash character tells PHP to render the quotation mark as part of the anchor tag and continue the print statement. The backslash will not be printed and for practical purposes is invisible to the Web user: <?php print "<a href = \"yourDestitnationPage.html\">Click here to visit the destination page.</a>"; ?>

Creating Dynamic Hyperlinks with PHP and MySQL

1

Connect to your MySQL database using the PHP mysql_connect and mysql_select_db functions: mysql_connect("addressOfDatabase", "yourUsername", "yourPassword") or die(mysql_error()); mysql_select_db("yourDatabaseName") or die(mysql_error());

2

Create a variable to retrieve the links from the MySQL database using the PHP mysql_query function. This example assigns the variable $data with a mysql_query function that will search the database tablenamed links and return all of the links: $data = mysql_query("SELECT * FROM links") or die(mysql_error('Error, no links were found.'));

3

Extract the links using the mysql_fetch_array function and print them out for the user. The example creates a new array named $info. It assigns the array with the information from the $data variable that was created in the previous step. It then loops through the data using the PHP "while" command. For each piece of data, a new variable known as $link is created. The link from each MySQL link table's linkName field is assigned to the new $link variable. The $link variable is inserted into a PHP print statement and HTML anchor tag using the PHP concatenation rule: while($info = mysql_fetch_array( $data )) { $link=$info['linkName']; print "<a href = \"" . $link . "\">Click here to visit the destination page.</a>"; }

Tips

  • The complete PHP code from the dynamic link example is as follows:
  • mysql_connect("addressOfDatabase", "yourUsername", "yourPassword") or die(mysql_error());
  • mysql_select_db("yourDatabaseName") or die(mysql_error());
  • $data = mysql_query("SELECT * FROM links") or die(mysql_error('Error, no links were found.'));
  • while($info = mysql_fetch_array( $data ))
  • {
  • $link=$info['linkName'];
  • print "Click here to visit the destination page.";
  • }
  • ?>

About the Author

Max Power started writing in 1996. Power was responsible for providing coverage of local and state governmental affairs for a web-boom-era news and civic-affairs news website. This experience provided him with a range of in-depth knowledge about legal, civic, political and governmental affairs. Power holds a Bachelor of Arts degree with a concentration in history.

More Articles

Photo Credits