PHP Language
  1. PHP is a server-side language for creating dynamic pages.
    1. Embedded in a server file with the .php extension.
    2. PHP is embedded in HTML
    3. The server sends the file, with the HTML part literally, and the PHP section replaced by the output from running the PHP.
    4. Server stores hello1.php:
      <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello, PHP</title> <link rel="stylesheet" href="../lit/genstyle.css" type="text/css"> </head> <body> <h1>Hello, PHP</h1> <p>By Federal statute, all instruction in a programming language must start with Hello, World!.</p> <?php echo "<p>Hello, World!</p>"; /* Notice the <?php ?> escape brackets. */ ?> </body> </html>
    5. The browser receives:
      <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello, PHP</title> <link rel="stylesheet" href="../lit/genstyle.css" type="text/css"> </head> <body> <h1>Hello, PHP</h1> <p>By Federal statute, all instruction in a programming language must start with Hello, World!.</p> <p>Hello, World!</p> </body> </html>
  2. Using PHP
    1. You can post .php files in your account on Sandbox.
    2. You can “try it out” from the Sandbox command line. Run the PHP command, type input, and say control-d to end the input:
      [bennet@sandbox ~]$ php This will be returned literally. But now, we will enter PHP. <?php $n = 10; echo "I'm PHP, and \$n is $n!\n"; ?> Bye now. ^D This will be returned literally. But now, we will enter PHP. I'm PHP, and $n is 10! Bye now. [bennet@sandbox ~]$
    3. Perhaps more practically, you can run PHP on a file:
      [bennet@sandbox php]$ php hello1.php <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello, PHP</title> <link rel="stylesheet" href="../lit/genstyle.css" type="text/css"> </head> <body> <h1>Hello, PHP</h1> <p>By Federal statute, all instruction in a programming language must start with Hello, World!.<p> <p>Hello, World!</p> </body> </html> [bennet@sandbox php]$
  3. Not everyone likes it. (They are probably right.)
  4. Language Reference.
  5. Same comments as C, C++ Java, Javascript and all those other Unix-infested languages.
  6. Semincolons are separators, not terminators (like Javascript, unlike C and Java), but line breaks are not alternate separators (like Javascript). So you can omit the semicolon after the last statement in a block, but usually not done.
  7. Variables
    1. Dynmaic and need not be declared.
    2. Variable names. Start with letter or _ and contain letter, digit or _ as usual.
    3. But always start with $. (This simplifies parsing and error detection, and creates some of the ungliest code imaginable.)
    4. Case sensitive.
    5. C-like scoping rules: Variables may be global, or belong to a function body. dnf
    6. A variable inside a function is local to it, but may be declared dnf global to give global scope.
  8. Basic types
    1. null
    2. bool
    3. integer
    4. float
    5. string
    6. array
    7. object
    8. resource (an opaque type delivered by library calls).
    9. Some more obscure ones.
  9. Numeric operators
    1. Usual numeric operators, +, -, *, / and %, and update forms. The / produces a floating-point division even between integer values.
    2. Also ** for raising to a power. Python-like.
    3. Also ++ and --, C-like.
  10. Conversions
    1. Since types are dynamic, no conversion on assignment.
    2. C- and Java-like casts, including casting int to string or string to int.
    3. Neary anything can be converted to bool.
    4. False values: 0, 0.0, "", "0", NULL, empty array, some internal objects.
    5. True values: anything else, including -1, "false", "0.0", NAN, and all resources.
  11. Comparison operators.
    1. Usual: ==, !=, <, >, <=, etc.
    2. <> same as !=. Pascal-like. (Anything more recent? Don't know.)
    3. <=> Produces negative, zero or positive, similar to what Java's compareTo produces. Perl-like.
    4. PHP has the same convert-before-compare problem that Javascript has (only worse), and also provices a === operator to compare type and value.
  12. Control flow.
    1. Curly braces for blocks, as Java.
    2. if else
    3. while, do ... while, for
    4. foreach, with a distinctive (read “strange”) syntax.
      1. For each value: foreach (collection as $v)
      2. For each key/value pair: foreach (collection as $k => $v)
      3. The as is literal, collection is something with parts.
    5. break, continue.
    6. switch, using break, like Java. Unlimited value type.
    7. Alternative sort of Python-like (colon, not indents) control structure:
      if ($a == 5): echo "a is five!\n" endif;
      And likewise for other constructs.
  13. Strings
    1. Constants with 'single quotes' or "double quotes". End with the same start.
    2. Usual escape sequences in "", but literal in ''.
    3. Everything else about strings is strange.
      1. Strings are not objects. No methods; send to operations.
      2. Concatenate with . instead of +. Perl-like.
      3. Variables inside double-quoted strings are substituted with their values. Perl-ish.
        [tom@server ~]$ php <?php $fred = 99; echo "\$fred = $fred\n"; echo '$fred is $fred\n'; ?> $fred = 99 $fred is $fred\n[tom@server ~]$
      4. No subscripting characters in a string; use substring. No distinction between string and character. substr($string,$offset,$length)
      5. Multi-line string constants (which interpolate). Unix shell-ish.
        1. Called here documents. Unix shell-like.
          <<<ENDER Contents of string Variables $v interpolated ENDER
        2. The end may be marked with any identifier. EOF is popular.
  14. Arrays.
    1. Arrays are a combination of array and map. JS-like.
    2. $arr = [ 4, 5, 17 23 ];
    3. $arr = [ "bill" => "frank", "fooble" => "snark", "a" => "b" ];
    4. $arr = [ "albert", 100, "bill" => "frank", "fooble" => "snark" ];
    5. Items without keys are given numbered keys from zero.
  15. Functions
    1. function name($arg1, $arg2, ...) { body }
    2. Return statement.
    3. Default parameter values.
    4. Variables inside the body are local.
    5. Say global $x; for $x to be the global $x.
    6. Without name, function($arg1, $arg2, ...) { body } , to create a closure.
    7. There is a syntax for variadic functions, which you can look up if you're interested.
  16. Libraries
    1. There is a very large collection of built-in functions.
    2. These are quirky and inconsistent, with odd names, strange semantics, and peculiar parameter order.
    3. It's got pretty much anything you could want.
  17. Etc.