Introduction to PHP.

Introduction to PHP.

Syntactic Characteristics.

PHP has:





  1. Variables

    • The type of a variable is set every time it is assigned a value


    • An unassigned variable (unbound variable) has the value of NULL


    • Unbound variables appearing on an expression have their value coerced

      • To 0 if the context specifies an integer


      • To the empty string if the context specifies a string


    • The function IsSet takes a variable's name as its parameter and returns a Boolean

      • TRUE if the variable has a non-NULL value


      • FALSE otherwise


    • The function unSet sets a variable back to the unassigned state


    • error_reporting(15);
      
      Changes the error reporting level of the PHP interpreter

      so the user can be informed when an unbound variable is referenced

      The default level is 7


  2. Integer Type

    PHP has a single integer type integer

    The size of a word size on the machine


  3. Double Type

    Double literals can have a decimal point or an exponent or both


  4. String Type

    • Characters are single bytes -- there is not character type

      Single characters are represented by strings of length one


    • String literals just like Perl's are delimited by single (') or double quotes (")


    • Double quoted literals can include embedded newline characters created by the Return key of the keyboard


  5. Boolean Type

    • An integer expression used in Boolean context evaluates to

      • FALSE if the expression value is 0,


      • TRUE if the expression value is NOT 0


    • A string expression used in a Boolean context evaluates to

      • FALSE if the expression value is "0", or the empty string


      • TRUE otherwise


    • The string "0.0" evaluates to TRUE


  6. Arithmetic Operators/Expressions

    • Arithmetic Operators: +, -, *, /, %, ++, and --

      • +, -, * produce a double(integer) for double (integer) operands


      • If the result of integer division is not an integer, a double is returned


      • The operands of the modulus operator % are coerced to integers if they are not.


    • Large number of predefined functions to operate on numeric values


  7. String Operations

    • The only string operator is the catenation operator, a period (.)


    • The individual characters in a string can be accessed by their position on the string

      $str = "Yellow";  # $str{4} is "o"
      	


    • Large number of predefined functions to operate on strings


  8. Scalar Type conversions

    • Implicit type conversions are coercions

      • Numeric values in a string context are coerced to a string


      • String values appearing in a numeric context are coerced to numeric values

        • If the string contains only digits, it is converted to an integer


        • If the string contains only digits and a period or an e/E, it is converted to an double


        • If the string does not begin period, a sign or a digit, it is converted to zero


      • When a double is converted to an integer the fractional part is dropped


    • Explicit type conversions:

      1. An expression can be cast to a different type

        $sum = 4.777;
        $res = (int)$sum;  # value of $res is now 4
                        


      2. Use type conversion functions: intval, doubleval, or strval

        $res = intval(4sum);  # value of $res is now 4
                        


      3. Use the settype functions which takes two parameters: a variable and a string specifying type name

        settype($sum, "integer");  # value of $sum is now 4
                        


    • The type of a variable can be determined

      • The gettype function takes on the variable's name as a parameter

        Returns a string with the name of the type of the variable

        It may return "unknown"


      • The testing type functions:is_integer, is_double, is_string, is_bool, etc.


  9. Assignment operators just like C and Perl

| Origins and Uses of PHP | | Overview of PHP | | General Syntactic Characteristics |
| Primitives, Operations, and Expressions | | Output | | Control Statements |
| Arrays | | Functions | | Pattern Matching |
| Form Handling | | Files | | Cookies | | Session Tracking |