Can You Have a Hyperlink in a PHP Bracket?
- A section of code embedded in the hypertext document of a PHP program is called a PHP block. A block is contained in brackets which tell the compiler that these are PHP instructions and not HTML. The opening bracket of a PHP block is either “<?php” or just “<?” and the closing bracket is “?>”. A program can contain many blocks. The blocks appear in the position where the program wants to insert the HTML that results from that block's execution.
- A problem with generating formatted text out of a system that has different formatting conventions is that the syntax of the two formats can sometimes clash. For example, the quote mark is a problem, because the PHP program may need to place a quote mark in the output of its run, but the quote mark itself is an opening and closing delimiter of values within PHP. Writing a function such as print “<a href= “http://www.somesite.com” /a>” would create a hyperlink in the output, except that the quote at the beginning on the link would close off the string passed to the print command. Quotes within quotes soon cause a confusion, because PHP counts each string as being enclosed by the first quote it finds and closed by the second quote it encounters. Thus the string passed to the PHP print would be interpreted as “<a href= ”. This string is then followed by a text the command does not know what to do with, and then the program encounters a second string, which is “ /a>”.
- Programming languages give programmers the ability to signal when a syntax character is meant to be treated as an instruction and when it is to be treated a just a character. This is called “escaping.” In the example “<a href= “http://www.somesite.com” /a>” the inner quotes need to be escaped so that the PHP ignores them and just copies them out with the rest of the text inside the outer quotes. The escape character in PHP is the backslash. So PHP will output a complete link in HTML format with: echo “<a href= \“http://www.somesite.com\” /a>”.
- Another method for outputting hyperlinks is to contain the line in single quotes and not double quotes: echo '<a href= “http://www.somesite.com” /a>'. However, if the link is assembled including a variable, this will appear as the variable name if single quotes are used, but will be replaced with the variable contents is the string is enclosed by double quotes.
PHP Block
Formatting
Escaping
Quotes
Source...