How to Use Echo for an Anchor in PHP

Understanding how to use HTML tags, such as the "anchor" (<a>) tag, within PHP code is an important skill to learn when you want to combine the two languages to create a Web page. The anchor tag can create a link to another URL with the "href" attribute or set a bookmark with the "name" attribute. You can use the "echo" command to display code on your Web page in PHP.

Instructions

    • 1

      Open the PHP file in a text editor such as Windows Notepad.

    • 2

      Add an anchor tag with an "href" attribute to create a hyperlink in the HTML body of your file, by typing the code:

      echo "<a href='http://www.myserver.com'>Click this link</a>";

      or:

      echo "<a href=\"http://www.myserver.com\">Click this link</a>";

      or:

      echo '<a href="http://www.myserver.com">Click this link</a>';

      The "href" attribute sets a destination URL to be opened when the user clicks on the hyperlink. A common mistake is to write it as:

      echo "<a href="http://www.myserver.com">Click this link</a>";

      This doesn't work because there are quotes (") inside of quotes. Escape the quotes with a backslash (\") or use a mixture of apostrophes (') and quotes.

    • 3

      Add an anchor tag with a "name" attribute by typing the code:

      echo '<a name="myspot"></a>';

      This acts as a bookmark that other hyperlinks can reference.

    • 4

      Create a hyperlink to an anchor by adding the code:

      echo '<a href=#myspot">Click to goto myspot</a>';

      or:

      echo '<a href="mypage.html#myspot">Click here</a>';

      When the user clicks the hyperlink, the Web page will scroll down automatically to the location of the anchor bookmark.

    • 5

      Save the PHP file and load it on your Web server to view the anchor tags.

Related Searches:

References

Comments

Related Ads

Featured