4.1 Introduction to PHP

  1. PHP basic
    • Read 'What is a PHP file?', 'What Can PHP Do?', 'Why PHP?' in PHP Introduction.
      • What is the default file extension for PHP files?
      • What receives the URL sent from the client?
      • What executes the PHP file specified by the URL sent from the client?
      • What does PHP code send to the client browser?
      • Anything but PHP code will be sent back to the client? Here is a test PHP program (test.php).
      • What happens when PHP code completes its tasks?
      • In a web browser, can you read the source code of a PHP program? This is a very important concept.
    • Read all in PHP Syntax.
      • What kind of tags are used for PHP code?
      • What are the three comment types?
      • Is PHP case sensitive? It is interesting.
      • How to declare PHP variables?
      • How to concatenate strings?
      • How to output strings to the client?
      • Trial 1: Let's try the next example.

    • Read all in PHP Variables and PHP Variables Scope.
      • How to declare PHP variables?
      • Is PHP loosely typed language or strongly typed language?
      • How is a variable within a string defined using single quotes interpreted? How is a variable within a string defined using double quotes interpreted?
      • What symbol is used for string concatenation?
      • What are the three different types of variable scope?
      • How to declare a function?
      • How are global variables different from local variables?
      • How to access global variables in a function?
      • How to declare static variables?
      • How are static variables different from other non-static local variables?
      • Trial 2: Let's try the next example.

    • Read all in PHP echo and print Statements.
      • Which statement is faster?
      • Which statement can output multiple strings?
      • How is "..." different from '...'?
        • Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
        • Double quote strings will display a host of escaped characters (including some regexes), and variables in a double quote string will be evaluated. An important point here is that you can use curly braces to isolate the name of a variable you want to evaluate. For example let's say you have the variable $type and you want to echo "The $types are" . That will look for the variable $types. To get around this use echo "The {$type}s are". You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
      • Trial 2.5: Let's try the next example.

    • Read all in PHP Data Types.
      • What are the two Boolean values?
      • How to obtain the quotient from an integer division? Note that there is no 'integer division' in PHP.
      • List 7 data types.
      • How to define an array? Always the same type values?
      • How to access an array element?
      • Use of var_dump() with an array.
      • How to define a function?
      • How to define a class?
      • How to create/instantiate an object?
      • How to access properties in an object?
      • We will revisit 'Classes and Objects' again.
      • Is NULL value an empty string?
      • Trial 3: Let's try to write the code to save some information into an array and print it using 'var_dump()'. You need to include three values, 'John', 23, and true, in the array, where true is a Boolean value, not a string.

    • Read all in PHP String.
      • How to get the length of a string? Are strings objects?
      • How to search a specified character or text within a string?
      • What value is returned from strpos() when a specific text is not found? Isn't it interesting?
      • PHP String Functions for full reference
      • How to get a substring of a string?
      • How to compare two strings?
      • Important functions - strlen, strcmp, implode and join, explode, trim, ...
      • Trial 4: Let's try the next example with some string functions, strcmp(), strlen() and trim().

      • Trial 5: Let's try to use implode(separator, array) and explode(separator, string) functions.

    • Read all in PHP Constants.
      • How to define a constant and use it?
      • Are constants global? What if a constant is defined in a function?
    • Read all in PHP Operators.
      • What is the output 'echo 6 / 4'?
      • How to concatenate two strings?
      • Identical operator and not identical operator?
      • Logical operators?
      • How to make the union of two arrays?
      • Integer division?
      • Trial 6: Let's try some operations. Integer division, or floating-point number division? Hint, floor().

    • Comparison to JavaScript
      • Tags
      • Declaration of variables
      • Access to global variables
      • String concatenation operator, exponentiation operator
      • The way to obtain the string length
      • The static keyword
      • Creation of arrays. Can you use [...]? From PHP 5.4
      • The way to obtain the number of elements in an array
      • Union of two arrays
      • echo
      • Trial 7: Let's try to create an array using [...], not array().

  2. PHP control statements
    • Read all in PHP if...else...elseif Statements.
    • Read all in PHP switch Statements.
      • Can you convert an if-else statement to a switch statement?
      • Can you convert a switch statement to an if-else statement?
      • When do you use a break statement?
    • Read all in PHP while Loops.
      • How is while loop different from do-while loop?
    • Read all in PHP for Loops and PHP foreach Loops.
      • How is foreach-as loop different from for loop?
      • How is foreach-as loop different from for-in loop in JavaScript?
      • Trial 8: Let's try to write the code to save some information into an array an print it using a 'foreach' loop. You need to include 'John', 23, true in the array.

    • Read all in PHP Arrays, PHP Indexed Arrays, and PHP Associative Arrays.
      • How to count the data in an array?
      • How to use associative arrays?
      • How is foreach-as loop different from for-in loop in JavaScript?
      • Trial 9: Let's try to write the code to save some information into an associative array and print it using a 'foreach' loop. You need to include Huyndai:Sonata, VW:Golf, Honday:CR-V in the array.

      • Trial 9.5: Let's try to write the code that sends the <table> code to the client. The <table> needs to be constructed within a function. The table cells need to include values from an associative array using a 'foreach' loop. You need to include Hyundai:Sonata, VW:Golf, Honda:CR-V in the array.

    • Comparison to JavaScript
      • Creation of arrays
      • foreach-as vs for-in
  3. PHP functions
    • Read all in PHP Functions.
      • How to create a function?
      • How to use default argument values?
      • How to use default argument values in JavaScript?
        function defaultftexample(a, b){
            if(typeof a == 'undefined')  // Or typeof(a)
                a = 10;
            if(b == undefined) 
                b = 20;
            ...
        }
        function anotherexample(a = 10, b) {
            ...
        }
        
    • Comparison to JavaScript
      • Default argument values
      • Call by reference
  4. PHP arrays and superglobals
    • Read all in PHP Arrays.
      • How to create an array?
      • List the three types of array.
      • How to count the number of elements in an array? .length()?
      • Which loop fits indexed (linear) arrays?
      • How to create an associative array?
      • How to access an element in an associative array?
      • Which loop fits associative arrays?
      • How to use foreach-as with associative arrays?
    • Read all in PHP Sorting Arrays. You may read JavaScript Sorting Arrays for JavaScript.
      • How to sort an array in ascending/descending order?
      • How to sort an associative array, according to the value/key?
      • Study closely the return value of the sorting functions.
    • PHP Array Functions for full reference
      • Can you make a stack?
      • Can you make a queue?
      • Can you search a value in an array?
      • Trial 10: Let's try to make a stack and a queue. You may use in_array(), array_push(), array_pop(), array_shift().

    • Read all in PHP Global Variables - Superglobals.
      • What are superglobal variables? Are they visible in user-defined functions without using the 'global' keyword?
      • What superglobal variable is used to access any global variables?
      • What information is stored in $_SERVER?
      • How $_POST is different from $_GET?
      • "http://stackoverflow.com/questions/1924939/php-request-vs-get-and-post": $_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE. But it's only a default, which depends on variables_order ; and not sure you want to work with cookies. If I had to choose, I would probably not use $_REQUEST, and I would choose $_GET or $_POST
      • For what do you need super global arrays?    Data transfer from web server to PHP code
      • Trial 11: Let's try to print all the values in $_GET and $_POST.

    • Comparison to JavaScript
      • Creation of associative arrays
      • Call by reference
      • Super global arrays
  5. How to send and receive data
    • Read all in PHP Form Handling.
      • Which superglobal variables are used to collect form data?
      • When do you use 'get'?
      • When do you use 'post'?
      • Is the value in a hidden-type input element also submitted?
      • Note that the server-side action script sends data, i.e., web contents, back and they will be displayed in the new window, meaning a new web application, not the current window.
      • You may review 3.12.
    • How to send multipe values with one name?
      • The names of input-like elements should be attached with [], like an array.
      • Here is an example.
        <form action='//cs.tru.ca/~mlee/comp3540/Software/test_display_inputs.php' method='get'>
            <select name='cars[]' multiple>
                <option value='VW'>VW</option>
                <option value='Hyundai'>Hyundai</option>
                <option value='Ford'>Ford</option>
                <option value='Lexus'>Lexus</option>
            </select>
            <input type='submit'>
        </form>
        
        <?php echo "GET data: <br>"; foreach ($_GET as $name => $value) if (gettype($value) == 'array') { echo $name . '='; for ($i = 0; $i < count($value); $i++) echo $value[$i] . ' '; echo '<br>'; } else echo $name . '=' . $value . '<br>'; ?>
      • Trial 12: Let's try the form in the above example. What if [] is omitted?




  6. Some review questions and learning outcomes
    • Understand what receives the URL from the client and executes the PHP code specified by the URL?
    • What happens to the PHP code after the code is executed?
    • Use of PHP for computer programming.
    • Use of superglobal variables.
    • Use of associative arrays with the 'foreach' loop.
    • How to receive data from the client using the HTTP get and post methods?
    • How to send data to the client from the client?
    • How to display a message sent from the client?