Learning Web Application Development With PHP and MySql
Recently, I enrolled in the Web Application Development with PHP and MySql course at diycomputerscience.com
The course is broken into four parts, where we start with an introduction to PHP, and end by making our own web application with PHP and MySql.
I just completed part 1, which is Introduction to PHP. I decided to pen my thoughts and notes below.
Part 1 of the course consisted of some learning material and 6 activities for practice. I started reading the book PHP and MySql Web Development, but soon realized that my natural style of learning is by doing and not by reading. So I decided to read just some basics from the PHP manual, and start doing the activities.
Activity 1:
The first activity was to create notes for PHP. I created this wiki page to record my PHP notes. I will continue to use this wiki page and add important facts, etc, which will make it a reference for me in the long run. More unorganized notes will be blogged on this blog.
Activity 2:
The second activity was to make a simple HelloWorld program in PHP. Here is the GIT commit for the activity.
My main learnings in this activity are:
- A PHP script must be enclosed within special characters <?php .. ?>
- We use the echo command to print something to the console or the browser
- PHP strings can be single quoted as well as double quoted. Single quoted strings can contain double quotes, and double quoted strings can contain single quotes. Double quoted strings also support string interpolation.
- We can run PHP scripts on the command line, but we need a utility for it.
Activity 3:
The third activity was to create a greeting page using PHP. Here is the GIT commit for the activity.
My main learnings in this activity are:
- PHP has global variables which are available to the programmer for various tasks. One of the global variables is $_POST, which is an array containing all the post data (if the user made a POST request). PHP also provides us with _GET, and $_REQUEST
- We use the '.' operator to concatenate Strings in PHP
Activity 4:
The fourth activity was to create a temperature conversion service, which will allow the user to enter temperature in either Fahrenheit, or Celcius, and the application would convert it to the other metric and display the result.
Here are two GIT commits I made for this activity. The first one accepts the temperature in Fahrenheit, and displays it in Celcius. The second one allows a user to enter the temperature in either metric, and displays it in the other.
My main learnings in this activity are:
- All variables in PHP are pre-pended with a $ sign
- PHP arrays are actually key => value pairs. If we query the array for a non-existent key, we will get a NULL
- PHP has support for the following types (Booleans, Integers, Floats, Strings, Arrays, Objects, Resources, and NULL)
Activity 5:
For the fifth activity we has to create a simple shopping cart using arrays. Here is the GIT commit.
My main learnings for this activity are:
- A PHP session has to be explicitly started with the session_start() command. I think there is a file setting which will create the session for us implicitly
- We can include external PHP files using the include 'filename.php' statement. When we include an external file, it gets evaluated. I believe we can also use the require statement. I don't know the difference between the two, Need to find out.
- session_unset will remove all the values which have been stored in the session
- Most non boolean values will automatically evaluate to boolean when used in a conditional statement (need to figure out the exact rules)
- In PHP we can iterate through an array using the foreach statement like this
- foreach($array as $key => $value) {}
- PHP supports the tertiary (cond ? something : something_else) operator
- Comparison is done with the == operator. However PHP also has a === operator. Need to find out what it means.
- PHP functions are declared thus:
- function func_name ($param1, $param2) {}
- We can have default parameters
- We can have var args
- Even though a PHP array consists of key value pairs, we can add an element to the array without specifying the key
- $array[] = "new element";
- In this case PHP will compute the key as 1 greater than the last numeric key
- The key must be either Integers, or Strings
- The gettype($var_name) function returns the type of a variable
- The var_dump($var_name) function dumps all the details of a variable
- The is_array($var_name) function will return a true if the specified variable is an array
- We can find out the number of elements in an array, by using the count() function
Activity 6:
For the sixth activity we had to recreate the simple shopping cart using classes and objects. Here is the GIT commit for the activity.
My main learnings in this activity are:
- PHP 5 onwards supports Object Oriented programming
- We can create a class thus:
- class ClassName {}
- Need to find out if classes can be public, private, etc
- Classes support public, private, and protected variables as expected. Need to find out, what is the default.
- The class constructor has to be a function named __construct()
- An object of a class is created using the new keyword.
- Objects have access to the $this variable which will refer to the calling object. In most cases it is the object to which the function belongs, but in certain cases, it could also mean the object which calls the function. If the function is called statically, then $this will not be set.
- Members of an object are accessed like $obj->member_name
