PHP Basics

Some simple PHP examples.

  1. A PHP is interpreted by the server.
    1. The PHP file is stored on the server with extension .php.
    2. The server sends the contents of the file, except the portions wrapped in <?php ?>.
    3. Those contents are executed, and the output is sent.
    4. The un-wrapped portions are HTML, and the PHP should generate HTML.
    5. The example below runs code that prints a string constant.
      [ Hello, World! Source ]
  2. HTML and CSS are declarative languages, but PHP is procedural.
    1. Performs steps in order.
    2. Updates values may change over time.
  3. 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 ]
  4. Superglobals
    1. Variables provided by the system when the script runs.
    2. These are string-subscripted arrays, having names that are all-caps except they start with underscore.
    3. The Phpinfo shows you what is available.
    4. Be very carful when printing things that come from the user.
      1. Hackers may be able to inject Javascript that will be run by the browser, or possibly PHP that will run on the server.
      2. Use htmlentities to prevent this.
    [ Varible Hello, World! Source ]
  5. Arrays
    1. Collections of variables.
    2. Each designated by a name or number.
    [PHP ArraysSource]
  6. The phpinfo function. Generates a complete HTML page. [ PHP Info Source ]
  7. 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 ]