Some simple PHP examples.
- A PHP is interpreted by the server.
- The PHP file is stored on the server with extension .php.
- The server sends the contents
of the file, except the portions wrapped in <?php ?>.
- Those contents are executed, and the output is sent.
- The un-wrapped portions are HTML, and the PHP should generate HTML.
- The example below runs code that prints a string constant.
[ Hello, World! Source ]
- HTML and CSS are declarative languages, but PHP is procedural.
- Performs steps in order.
- Updates values may change over time.
- The point is to create pages which vary and can respond to inputs from
the user, or display data stored on the server.
[ PHP Basics Source ]
- Superglobals
- Variables provided by the system when the script runs.
- These are string-subscripted arrays, having names that are
all-caps except they start with underscore.
- The Phpinfo shows you what is available.
- Be very carful when printing things that come from the user.
- Hackers may be able to inject Javascript that will be run by
the browser, or possibly PHP that will run on the server.
- Use htmlentities to prevent this.
[ Varible Hello, World! Source ]
- Arrays
- Collections of variables.
- Each designated by a name or number.
[PHP ArraysSource]
- The phpinfo function. Generates a complete HTML page.
[ PHP Info Source ]
- Queries. An important source of input to a PHP script is query information
in a URL. ?name1=value1&name2=value2
[ Send to info page: phpinfo.php?count=10&joe=boss ]
[ Send to query echo page: query1.php?joe=17&alice=sick&bill=199 Source ]
[ Or this: query1.php?joe=missing&bill=0&alice=out+of+the+country ]