Saturday, February 25, 2012

Individual Tutorial & Reflection #2 - PHP

This week, I completed two different tutorials, so this reflection blog will contain two distinct sections.

PART I  - Finishing the PHP Guide from PHP Learn It!

Last week, I completed 9 out of the 19 steps in the PHP Guide from PHP Learn It! Lean PHP by Examples.  This week, I began with PHP Arrays.

PHP Arrays... 

"An array is a mean[s] to store [a] collection of values in a single variable."

The two types of arrays dealt with in PHP are numeric arrays and associative arrays. Elements in numeric arrays are stored with a numeric ID key. "When we want to store elements in array with some meaningful association other than numbers, we use associative array."

Multidimensional arrays are used when we want to "associate multiple keys to an element."

PHP Loops...

In PHP, loops are used to repeat a code for as many times that you want without having to rewrite it.  There are three types of loops used in PHP programming: foreach loop, for loop, and while loop.

"Foreach loop is most often used to print elements in a array...We can use foreach loop to print the keys and the values from a multidimensional array."


"A for loop is used when we want to execute a block of code for certain number of times."

"For loop has three statements.
  • In the first statement, we initialize a counter variable to an number value.
  • In the second statement, we set a condition (a max/min number value) until the counter is reached.
  • In the third statement, we set a value by how much we want the counter variable to incremented by."

"The while loop is used to execute a block of code for certain number of times until the condition in the while evaluate to true."

A break statement can be used to "break the loop when a certain condition is true."

PHP Functions...

"A function is a group of instructions to perform a task....A function is a block of code with a name which can be used in our code whenever we need it."

"A typical function has three components to it
  • Starts with the word function
  • Has a function name followed by parentheses (). A function name can only start with a letter or a underscore.
  • The code block in the function always goes inside curly brackets."
"A function can be used to pass arguments (values or information) into it through parameters. This is useful when we want to add more functionality to our functions."

PHP functions can also return a value like this:

PHP Comments...

"Comments are used to describe your code." The two ways to write comments are single line comments and multiple line comments. Single line comments are written with forward slashes "//", and multiple line comments start with "/*" and end with "*/". Because PHP codes can get complicated, it is important to write comments.  Once you write a lot of code, you will need some way to remember what your code does so you can use it again later.

PHP E-mail...

In this section of the PHP guide, codes for the mail function were provided and explained.  Examples of how to send both plain text and HTML emails were given.

PHP Date...

With the date function, there are two "arguments": format and timestamp. The format must always be specified so the time or date will display in the desired output, but the timestamp is optional. A complete list of day/time formats are available here at

PHP Cookies...

A "cookie is a small flat file which sits on user’s computer. Each time that user requests a page or goes to a webpage, all cookie information is sent too. This is used to identify who you are."

This is an explanation of the cookie function (creating a cookie), which I have copied directly from the guide:
setcookie($name, $value, $expire, $path, $domain, $secure) 
  • $name - name of the cookie. Example: "username"
  • $value- value of the cookie. Example: "john"
  • $expire- time (in UNIX timestamp) when the cookie will expire. Example: time()+"3600". Cookie is set to expire after one hour.
  • $path- path on the server where cookie will be available.
  • For example, if the path is set to "/", the cookie will be available through out the whole site. If the cookie is set to say "/news/", the cookie will only be available under /news/ and all its sub-directories. If no path is given, cookie in created under the current directory.
  • $domain- domain where cookie will be available. Instead of path you can use domain settings.
  • For example, if the domian is set to "", the cookie will be available within the domain nd all its sub-domains, example If the cookie is set say "" the cookie will be available under all www sub-domains, example ""
  • $secure - true if cookie is being set over a secure "https" server, false otherwise, Default value is false.
The guide also explains how to retrieve and delete cookies.

PHP Sessions...

The last section of the guide explained PHP sessions, which are "used to store information which can be used through-out the application for the entire time the user is logged in or running the application." The guide walked me through how to start a session, store information in the session, retrieve stored information, and delete session information.

PART II - PHP 101: PHP For the Absolute Beginner

This week, I also spent time working on a tutorial I found on a website called Zend Developer Zone: Advancing the art of PHP.  The tutorial was written by Vikram Vaswani and seemed to have positive reviews, according to the comment thread at the bottom of the tutorial's home page.

This particular tutorial involved quite a bit of reading, but it was extremely thorough and well-written. Now that I have been reading about PHP for the past several weeks, some of the information was review.  I have to admit, it felt nice to find myself nodding my head while reading through the tutorial because I was actually recognizing some of the terms and concepts needed for having a basic understanding of PHP.

Part 1 of the tutorial gave a general overview of what PHP is and how it works.  Again, this was review, and I was thrilled that it all made sense to me.  Just like in the last tutorial I completed, this tutorial provided a definition for variables, since they are so important for PHP and any other programming language. There was also was an explanation of how the "=" sign is used to assign values to variables. The basic types of variables supported by PHP are Boolean, integer, floating point, and string.  Finally, this part of the tutorial describes string operators and concatenation operators, which I had also learned about in the first tutorial I completed.

Part 2 of the tutorial was called "Calling All Operators." This part explained how PHP can "automatically receive user input from a Web form and convert it into PHP variables." I really liked how this part of the tutorial was laid out because he gave the php code, then he pointed out the most critical line of the code containing the php function, which he later provided the html script for in order to explain the function in great detail.  After he was finished describing the basics of forms, he then talked about how to add "intelligence" to scripts by constructing conditional statements. (Side note: I thought "intelligence" was an interesting choice of word.)


The next section moved from comparison operators on to logical operators.

I went ahead and tried the example code for the four logical operators: logical AND, logical OR, logical XOR and logical NOT.

The tutorial went on to explain conditional statements, if-else statements, and if-elseif-else statements

I will continue with this comprehensive tutorial next week - I really like the structure and the explanations are phenomenal yet easy to understand.


My friend Jay in the computer science department sent me one of his students, who is willing to help me brainstorm some ideas for my final project for this course.  We will be meeting on Tuesday for lunch.  I'm really looking forward to it!  In the meantime, he gave me this code to play with...


$array1 = array("scott", "laura", "php", "work");
//that's an array
//to access different items you start at 0 and count up

echo $array1[0]."<br/>";
//this will print the word "scott"
//notice how you access it through 0 not 1

echo $array1[2]."<br/>";
//this will print the work "php"
//the <br/> is html for line break
//notice how this is accessed through 2 and not 3

$array2 = array(array("me", "you"), array(3, 5));
//this is a nested array and allows you to have something that resembles a table

echo $array2[0][0]."<br/>";
//prints "me"

echo $array2[1][0]."<br/>";
//prints 3


... so I tried it out!

I was so happy to have learned about comments today, because being able to identify those within the code really helped me to understand the function of the script.  I was also happy he showed me this code, because now I really see how you can retrieve information stored in an array.I'm really starting to understand PHP, and I'm looking forward to next week when I can continue the new tutorial and report on my meeting with Scott.

No comments:

Post a Comment