Difference between revisions of "PHP code island"

From Wiki @ Karl Jones dot com
Jump to: navigation, search
(See also)
(External links)
Line 60: Line 60:
 
== External links ==
 
== External links ==
  
 +
* [http://php.net/manual/en/language.basic-syntax.phpmode.php Escaping from HTML] @ php.net
  
 
[[Category:PHP]]
 
[[Category:PHP]]

Revision as of 21:03, 20 September 2016

In PHP, a code island (or code block) contains PHP code.

Code island markers

A code island consists of a open marker and a close marker, like this:

<?php

?>

Or it could be expressed on a single line, like this:

<?php ?>

PHP code

All of your PHP code must be inside a code island. PHP code only functions as PHP code when it appears inside a code island.

PHP code islands must not include HTML, CSS or JavaScript; using these languages inside a code island will trigger a PHP error.

A PHP file commonly contains a mix of PHP code and HTML. The file might contain multiple code islands, divided by blocks of HTML.

Or, the PHP file might contain only PHP code, without any HTML.

Example

The example below shows a PHP code island inside a paragraph element:

<p>
<?php
  echo "Hello World";
?>
</p>

The above code sample is a mix of HTML (a paragraph tag), and a PHP code island (which will display "Hello World" for the user, inside the paragraph.

The echo keyword generates the text Hello World (the text inside the quotation marks -- not the quotes themselves).

Code island location

When mixing code islands and HTML, the location of the code island(s) depends upon what the PHP code does:

  • Code islands that generate text are located where the text needs to appear with respect to the HTML.
  • Some code islands appear at the top of the file, above the HTML.
  • Some code islands appear at the bottom of the page, after the HTML.
  • Some code islands can be positioned anywhere; it is good practice to use a consistent location, such as the top of the file.

See also

External links