PHP Basics

This is material from Chapter 3 in the text. I've left a few things out.

  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.
    Hello, World That Pays Attention To It
  4. Basic Syntax.
    1. Comments, /* */ and //.
    2. Variables hold values. Marked with $
    3. Instructions are “statements,” and end with ;.
    4. The = makes an assignment, does not test for equality.
    5. Variables may be used in expressions.
    6. Values have types.
    7. Strings
      1. ' and " each create strings.
      2. Double quotes “interpolate” embedded variables; singles don't.
      3. The multi-line form starts with <<<ANY
        1. Use the three less-thans, then any marker you like.
        2. Ends when it sees the marker again.
      4. The . operator concatenates string. (Java programmers beware!)
    8. PHP is dynamically-typed, and variables need not be declared.
    9. Type conversion is very fluid, and explicit casting is rarely needed.
    [ PHP Basics Source ]
  5. Arrays
    1. Collections of variables.
    2. Each designated by a name or number.
    [ PHP Arrays Source ]
  6. The phpinfo function. Generates a complete HTML page. [ PHP Info Source ]
  7. 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 Echo Source ]
  8. 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 ]