Programming and PHP(January 2013)

Page 1

Programming and PHP PHP is what is called a server side programming language. This means it resides on servers and allows web pages to communicate with the databases on these servers. It is used by sites like YouTube and Facebook to generate pages that look different for different users. It is a bit easier to get to grips with than Java for example. There are less rules and structures to abide by and you can get going pretty quickly.

They key thing about learning to program is that it is a transferable skill, the langauge is almost irrelevant. The skill of programming is about a way of thinking and using the tools at your disposal to solve problems and achieve goals. The programming constructs that you will learn in this course can be found in virtually all other programming languages. So it is safe to say, that if you learn to program, you should be able to move to any language you want as you have understood “how” to program.

Variables Every program needs to give the programmer access to variables. Variables are the storage space that the programmer can use to store values required in a program, for example a person’s name or age. Here is how a variable is declared and assigned a value:

$first_name = ‘Alfred’;

This means that the storage location called $first_name contains the word ‘Alfred’. If the programmer then references that storage location then the word ‘Alfred’ would be substituted for the variable name $name. For example:

print $first_name;

Would not see ‘$first_name’ printed to the screen, but instead the contents of that storage location, in this case ‘Alfred’. Naming Variables Most programming languages have conventions around how variables should be named. It goes without saying that the variable name should be meaningful and make sense to someone else who might look at your code, for example $age is more meaningful than $a.


It may be that you want to use more than one word to create a meaningful variable name. If this is the case then you have 2 options. Option 1: use capital letter to delimit your words e.g $firstName or $ageInDays Option 2: use an underscore as the delimiter e.g. $first_name or $age_in_days

PHP restricts the characters that can be used as part of a variable name to letters and numbers, so things like ‘!’ or ‘@’ are unacceptable characters to use as part of a variable name. Variable types PHP does not require the user to specify the variable type, it is able to work this out from what is stored inside. You will find that this flexibility does not exist in other languages. It is important to know what type of variable you are dealing with, as this will have implications as to what you can do with that variable. $firstName = ‘Alfred’; (PHP knows that this is what is called a String) $age = 56; (PHP knows that this is a numeric value) $activeClubMember = TRUE; (PHP recognises this as a Boolean value) Strings A string is a variable type that can store combinations of letters, numbers and symbols. So could be used to store things like an address or an ID number. The operations that you might perform on a string will be distinctly different from that of a number. For example you might ask PHP to capitalise the string, this operation would not work with a number and might generate an error. Similarly, you would be unlikely to try to add to strings in an arithmetic fashion, e.g. $name1 = ‘Alfred’; $name2 = ‘Batman’; $newName= $name1 + $name2; The outcome of this operation is nothing, PHP won’t do it. However, if we had the following scenario: $name1 = “12mup”; $num1 = 45; $newValue = $name1 + $num1; In this case, the value in $newValue is actually 57, PHP can see the 2 numbers at the beginning of our string and quietly converts them to be able to take part in the addition.


Concatenating Strings Concatenation is just another word for joining two or more items together to make a new longer item. It might be necessary to join strings and numbers together to store new values or print them to the screen. To do this we use the ‘.’ symbol: $name = ‘Alfred’; $age = 45; echo “Welcome to the Tax Calculator “.$name; (what would this print?) echo “You look very young for “.$age; (how about this?)

or $name = ‘Alfred’; $age = 45; $password = $name.$age; (would create the password Alfred45 ) String Functions A function groups a number of program statements into a unit and gives it a name, it is reusable i.e. it can be executed from as many different points in a program as required. There are a number of functions that can be performed using strings. ucfirst($String variable) – makes the first letter uppercase $var1 = ‘leona’; $var2 = ucfirst($var1); will store ‘Leona’ in var2

strtolower($string variable) – convert all letters to lowercase $var1 = ‘AlEsSANDRo’; $var2 = strtolower($var1); will store ‘alessandro’ in var2


strtoupper($string variable) – convert all letters to uppercase $var1 = ‘chris’; $var2 = strtoupper($var1); will store CHRIS in var2

trim($string variable) – remove any blanks at the beginning or end of the string $var1 = ‘ pedro ‘; $var2 = trim($var1); stores ‘pedro’ in var2

ucwords($string variable) – capitalises the first letter of each word $var1 = ‘hiu chun’; $var2 = ucwords($var1); stores Hiu Chun in var2

strlen($string variable) – gives the length of the string $var1 = ‘Hong Kong’; $var2 = strlen($var2); stores the number 9 in var2, the space is counted as well

These are not all of the possibilities, just some useful ones. If you need any other functions you can find them here http://php.net/manual/en/ref.strings.php

Numbers Some programming languages have different types of number variables, mostly related to precision. For example Java would require 2 different types of number variables to store 34 and 34.2. The whole number is termed an integer and the number with the fractional part of type double. There is no such need to do this in PHP. So this is acceptable: $a = 3; $b = 4; $a = $a/$b; (the answer to this is of course 0.75, which can be stored in $a)


Number Operators The usual numerical operators can be used with regard to numbers in PHP. $a = 4; $b = 5; $c = $a + $b; (adds 4 and 5) $d = $a - $b; (takes 5 away from 4) $e = $a/$b; (divides 4 by 5) $f = $a*$b; (multiplies 4 by 5) $g = $a % $b;

(called modulo division where the answer is the remainder of the division, in this case 4, because 4/5 is 0 remainder 4)

There are some shorthand ways to write the same operations, only if you are storing the answer in one of the variables involved: $a = 3; $b = 2; $a += $b; (add a and b together and store the answer in a) $a -= $b: (take b away from a and store the answer in a) $a /= $b; (divide a by b and store the answer in a) $a *= $b; (multiply a by b and store in a); $a %= $b; (modulo division of a by b, storing the answer in a)

Numbers can also be concatenated using the same method as strings. $a = 4; $b = 5; $c = $a.$b; (will store the number 45 in the variable c)


Boolean Types Boolean variables can only have 2 values, TRUE or FALSE. $check = TRUE; $check = FALSE; Boolean types can be used to make decisions in programs and are usually used in conjunction with comparisons and If statements. If Statement An ‘If’ statement is used to create different pathways in a program. To represent this visually we could use a flowchart.

In PHP an If statement is used as follows: If ( comparison to be made){ Code to be executed if comparison is true; } Else { Code to be executed if comparison is false; } Note the curly brackets enclosing the code to be executed. There might be any number of lines of code to be executed, it is not restricted to only one.


The previous example will only work if we only have two options to choose between. If we need three outcomes then we can use a variation of the ‘If’ statement. If (comparison) { Code if comparison is true; } Else if( second comparison) { Code if second comparison is true; } Else { Code if none of the comparisons are true; } We can continue to add ‘Else If’s and Else’s as necessary, depending on the number of outcomes we need. However, after a while, it gets difficult to keep track of the different clauses and conditions. A more readable structure to solve this problem is the ‘Switch’ statement. Switch The Switch structure is used to overcome the problem of choosing between many alternatives. Have a look at the example below: switch($state){ case "CA": $tax = 0.09; break; case "NY": $tax = 0.11; break; case "WA": $tax = 0.12; break; case "PA": $tax = 0.05; break; default: $tax = 0.10; }


In the switch($variable) part, we include the variable we want to check the value of, in this case a state. Each possibility then becomes a ‘case’. So if it is the ‘case’ that the state is ‘NY’ then the tax value is set to 0.11. The break statement is included so that as soon as one case is true, no others are checked, the program leaves the switch statement when one of the ‘cases’ becomes true. The default value is added in the possibility that none of the ‘cases’ are true, it is a bit like the Else section of the If structure. Comparisons In many programs it will necessary to compare values so that it can be decided what next course of action is. For example Lionel will provide different access rights to users who have a staff log-in compared to those who have student usernames. It is important at this point to talk about assigning a value and a potential pitfall of confusing this with a comparison. Assigning a value looks like this: $var1 = 23; (the value 23 is assigned to the variable var1, note that there is a single = sign used here) Comparing 2 values is similar but has one crucial difference $var1 == 23; (does $var1 equal 23?) this would normally be included alongside an if statement or even a while statement e.g. If($var1 == 23){ Code if true;} Else{ Code if false;}


Other operators that could be substituted for the == could be: === is identical(including type e.g. string) > greater than < less than >= greater than or equal to <= less than or equal to != not equal to <> Not equal to These comparisons generate a boolean value, a comparison can either be true or false.

Logical Operators: Complex Comparisons On occasion it will be necessary to build more complex conditions to be used in the ‘if’ statement or while loop. For example you may want to execute certain code when 2 values are true e.g $x==6 AND $y==7, but how would we code this? If($x==6 && $y==7){ …;-

Alternatively If($x==6 AND $y==7){ …;-


It may also be the case that you need to have 1 of 2 conditions be true rather than both. E.g If($x==6 OR $y==7){ Code to be executed if one of the conditions is true;} Else,…;-

An alternative for OR is ||.

It is possible to combine ANDs and ORs to build increasingly complex conditions to satisfy the needs of the code you are writing. If($city == “Springfield” AND ($state = “MA” OR $state= “VT”)), This condition would be true if the city was equal to Springfield and the state was either MA or VT. It should also be pointed out that there is operator precedence. The symbols &&/|| are more important in terms of precedence than AND/OR so should not be mixed otherwise you could get unexpected results.

Repetition So far we have only written programs that we could call ‘linear’. They go through the instructions sequentially from start to finish. We need some way of ensuring repetition can take place and there are a couple of ways to do this. Conditional loop A conditional loop is one that uses a condition to decide whether to continue or not. While($userInput != “Stop”), Code to be executed; } The loop above would continue until the variable $userInput became equal to stop. Therefore, as a programmer you cannot be sure how long this loop might repeat. It might be once it could be 100 time. The condition above is simple, but you can use the comparison and logical operators to build a condition that suits your requirements.


There is a variant of the while loop that behaves in a slightly different manner. Look at this example: Do{ Code to be executed; -while($userInput != “stop”);

This loop, like the previous example, continues to execute until the variable $userInput becomes equal to ‘stop’. However, there is a subtle difference here. The loop above will execute the code at least once every time. The comparison below explains this difference: Example 1 $num = 15; While($num <10), //the condition is false, so the loop doesn’t get started Echo $num; //won’t get executed }

Example 2 $num = 15; Do{ Echo $num; //gets executed the first time through } while($num<10); // condition is false so loop stops

Fixed Loop The second type of loop is sometimes referred to as a fixed loop. This is because the programmer can control how many times it repeats. In most programming languages this is called a For loop, and PHP is no different. For($counter = 0; $counter < 10; $counter++){ Echo $counter; } This code would generate a list of numbers from 0-9.


Let’s break the parts of the ‘for statement’ down: $counter=0 – sets up a variable to be used to count the number of times the loop has been executed, initialise it to zero. $counter < 10 – the check to see whether the loop should continue. In this example the loop should stop when $counter is 10 or more e.g. it continues whilst $counter is less than 10. $counter++ - adding to the counter that takes place at the end of each time round the loop. In this example, only 1 is added each time but any value could be used.

Comments Commentary allows us to make programs readable. There are a number of reasons as to why it is necessary to make a program readable. As novice programmers, you can use comments to explain complex sections of your code, to help you understand it better when you return to it. In industry comments are used for similar reasons. Very often there will be more than one person involved in the creation of a piece of software and comments can be used again to explain complex sections of code to your colleagues. Adding comments is very simple: /* this section of code is used to work out a grade from a raw score * Raw score is compared with the cut off scores * And an appropriate grade is printed */ $rawScore = 63; If($rawScore >= 70 ) { echo “A”;Else if ($rawScore >= 60 AND <70) { Echo”B”;Else{ Echo “C”?> The example above shows how to make a block comment, if you simply want to explain a line of code the syntax is slightly different: If($rawScore >=60 AND $rawScore <=69) { // check to see if the raw score is between 60 and 69


Arrays So far the programs we have written have relied on a few variables to operate successfully. These variables are stored as separate entities and can cause some headaches with regard to the amount of code needed to manipulate them. A data structure that can overcome some of these problems is an array. An array can be visualised like so: [0] “Pete”

[1] [2] “Vivian” “Sahil”

[3] [4] “Bessie” “Alan”

[5] “Leia”

The diagram attempts to show that an array would hold consecutive space in memory, each with an index (the number in square brackets). This means the array can be treated as a whole and as individual elements giving it a lot of flexibility. It should also be noted that the first index in the array is 0, meaning that even though there are 6 elements in the array the last index is 5. The is always the case, if the array had n items in it, the index of the last element is n-1. It is also worth noting at this stage that strings can sometimes exhibit the behaviour of an array. Each of the individual characters can be thought of as taking up a position in an array like so: 0 C

1 A

2 L

3 I

4 F

5 O

6 R

7 N

8 I

9 A

Creating an array is very straightforward. $employees = array(“Pete”, “Vivian”, “Sahil”, “Bessie”, “Alan”, “Leia”); This line of code would create the array illustrated above. To demonstrate how an array behaves we will look at how you might print the contents of an array. for($counter = 0; $counter<count($employees); $counter++){ echo $employees[$counter]."\r"; } In this example we work out the number of elements in the array using the “count” function and then print each array element using $counter as an index. In other programming languages this would be the typical way to run through an array and use each element individually, the index appearing in square brackets. PHP affords another way to move through an array: foreach ($employees as $value){ echo $value."\n"; } In this loop $value becomes the substitute for each of the elements in the array. This loop achieves the exact same outcome as the previous example.


These examples use a small dataset but the ability to cycle through the array using 3 lines of code is unchanged no matter how big the data set becomes. This is very useful and time saving. Associative(2D) Arrays Let’s imagine that as well as the employee names we wanted to also store their position in the organisation. We could use the array as follows: $employees = array(“Pete”, “Treasurer”, “Trevor”, “President”, “Sally”, “CEO”, “Raghav”, “Director”) We would simply need to remember that every second element of the array contained a position and not a name. This is not an ideal situation as it would a layer of complexity to any code we wrote. We can make it easier for ourselves by using an associative array. In many other programming languages this type of array is called a 2D array. They can be visualised like so:

[0] [1]

[0] “Pete” “Treasurer”

[1] “Trevor” “President”

[2] “Sally” “CEO”

[3] “Raghav” “Director”

To access an individual element we would use the 2 indices: employees*0+*1+ is the word “Treasurer” for example. In this area PHP is slightly different. We have already established that they are called associative arrays in PHP. We declare them in a similar way to the declaration of a regular array. An associative array can be thought of as an array of arrays: $employees = array(array(name=> “Pete”, position=> “Treasurer), array(name=> “Trevor”, position=> “President”), array(name=>”Sally”, position=>”CEO”), array(name=>”Raghav”, position=>”Director”)); So in PHP it would actually look more like this:

name position

[0] “Pete” “Treasurer”

[1] “Trevor” “President”

[2] “Sally” “CEO”

[3] “Raghav” “Director”

To access an individual element of the associative array we would use the following: $employees *1+*‘position’+; This would give us access to the word “President”.


We could loop through the array using a similar method to before having a $counter variable to move through each array within the array. However, as the row indices are now text, this becomes difficult. Fortunately, PHP offers a solution to this. foreach($employees as $person){ echo ($person['name']." , ".$person['position']); } $person becomes a substitute for each individual array. We can use the “row” indices to access each individual element of the arrays within the array. Finally, a useful function to know about is the print_r( ) function. You can use it as follows: print_r( $employees); //$employees should be an array or associative array It will print the entire contents of $employees which may be useful for debugging purposes, to understand the structure of the array and whether you have implemented it the way you thought you had. Getting User Input Up until now our programs have relied upon you the programmer “hard-coding” the variable values into the program. This does not offer the flexibility that programs require to be of any use. PHP is a scripting language, more specifically a server side scripting language. This means the code communicates with database servers and is usually woven into the HTML of the web pages we use. We can use this HTML to gather the input of the user. There are many ways to do this but we are going to use a simple(if slightly out-dated) method called a form. The reason for this is that we want to concentrate on the PHP side of things for the time being. Forms can be easily created in Dreamweaver, using drag and drop. If you do not have Dreamweaver then you can access an example html version of a form on the coursepage. You can edit it in notepad and save it as html. Forms A form has a number of different elements. They are explained below. <form id="tax" name="tax" method="get" action="firstformtax.php"> The form has an ID and a name so that it can be referenced from other parts of the page or our PHP code. The method type refers to how the data is collected and processed and is explained below. The action section refers to where the data is sent, for example, you may want your data to be sent to a different page from the one that contains the form. All of the items used to collect data in the form should be included within the<form> html code. Using the method “get” produces the following result:


As you can see from the URL, when the form is submitted using the get method, the data submitted is appended to the URL of the page that it is sent to. This makes it very unsecure, as the data is visible publically. The second method associated with the submission of the form is the “post” method. It does the opposite of the “get” method. The data is collected and then hidden from view. In both cases, the data submitted is essentially added to an associative array given the name of the method. It is is “get”: $_GET*‘state’+; //would access the value stored for state in the example above. The syntax is exactly the same if the method has been collected using “post”: $_POST*‘state’+; Some additional requirements We do not want to collect any information until the user clicks on the submit button, so in PHP we need to program for this. if(isset($_GET['taxForm'] )== "Submit") //checks to see if the user clicks the Submit button within the taxForm


Functions Functions are very useful and flexible tool in programming. We have already used functions in the course such as count and substr. These are pre-written functions that carry out a particular task for us and give us a result. If we take substr as an example: $state = ‘California’; $short_code = substr($state, 0,2); // removes ‘Ca’ from the string and stores it in our variable This code would copy the first 2 letters of the string and store them in the $short_code variable. Let’s look more closely at how the substr function can do this. Some functions need what we call parameters to perform their tasks. Substr needs 3 parameters: Substr(a string, start value, end value) Substr needs a string to take a ‘chunk’ from, an index of where to begin the ‘chunk’ and then a value for the length of it. Remember that strings can exhibit behaviours of arrays and this is one of those instances. You can find lots more pre-written functions in php here: http://php.net/manual/en/language.functions.php As long as you know the name of a function, the parameters it requires and what the outcome of the executing the function is you can use any of them. This knowledge also helps when you want to create your own functions. This is where the real power and flexibility lies. You can write a function that does anything and re-use it any of your programs. Let’s say we wanted a function that will calculate the amount of sales tax on an item. We need to use a sensible name for our function, salesTax will do fine here. We also need to think what parameters will be needed for the function to do it’s job. In this case we need to know the value of the item being sold and the rate of tax being used. Here is how we might go about creating this function: Function salesTax($value, $rate){ $tax = $value * $rate; } You can see we have used the name salesTax and then allocated the 2 parameters for value and tax. To call the function we need to use it’s name and ensure we pass it 2 parameters in the order shown above: salesTax(5.99, 0.1);// would set the variable $tax to 0.59


Passing Parameters – By Value or By Reference There are 2 ways to pass a parameter, by value or by reference. The situation will dictate what method is used. When you pass by value, a copy is sent to the function to be used and then discarded when the function completes its execution. For example: $par = 0; byValDemo($par); Function byValDemo($par){ $par = $par +20; Echo $par; //prints 20 } Echo $par; //would print 0 as the changes made in the function do not affect the original variable as it was only a copy passed to the function. This is passing by value and is the default position in php. If you want to use by value then you do not have to include any additional symbols. By Reference, in contrast, sends a link or reference to the original variable. In essence it passes the memory address of the variable to the function so the original value of the variable can be manipulated. Some additional symbols are needed to pass by reference. $par = 0; byValDemo($par); Function byValDemo(&$par){//note the ampersand at the beginning of the parameter $par = $par +20; Echo $par; //prints 20 } Echo $par;// prints 20 this time as the variable was passed by reference and the function was given access to the original


Getting variables out of functions We have seen how to pass variables into functions, and by using by reference we can affect the value of variables outside of the function. However, what if we generate an entirely new variable in the function and then want to make it available outside of the function? We need to use the ability of functions to return variables. Function salesTax($value, $rate){ $tax = $value * $rate; Return $tax;//sends the variable tax outside of the function for use in the rest of the program } Scope Following on from the idea of returning a variable from a function to be used in the rest of the program it is now a good time to think about scope. Scope refers to where variables can be seen. salesTax(5.99,0.1); Function salesTax($value, $rate){ $tax = $value * $rate; Return $tax;//sends the variable tax outside of the function for use in the rest of the program } Echo $tax; No problem with the example above, $tax has been sent outside of the function, so it’s scope has gone beyond the function it was created in

salesTax(5.99,0.1); Function salesTax($value, $rate){ $tax = $value * $rate;

} Echo $tax; This would generate an error. The scope of tax is within the function only. It was created there and only exists there.

We would similarly encounter errors should we refer to variables inside functions that have not been passed as parameters: salesTax(5.99,0.1); $govAddedTax = 0.5; Function salesTax($value, $rate){ $tax = $value * $rate; $tax =$tax + $govAddedTax; // would cause an error as $govAddedTax was created outside the function and was not passed in, therefore it cannot be “seen” inside the function }


Local or Global The proper terms for the different types of scope are local and global. A variable can be local: salesTax(5.99,0.1); Function salesTax($value, $rate){ $tax = $value * $rate } Echo $tax; // cause an error because $tax is LOCAL to the salesTax function We can define a variable as global by doing the following: global $tax; It should be said that the process of making variables global is a treacherous one and the best practice is to pass variables by value or reference. Pseudocode As we write increasingly complex programs, we need to spend some time thinking about the design of the program. One way to do this is with the use of pseudocode. By writing pseudocode we can think about the logic of the program without actually writing the program. Pseudo means false or fake, so what we are writing is “fake-code”. This means pseudocode is a bit of a hybrid of programming code and regular English. The IB specifies their expected style of pseudocode, laid out below. •

Variable names are all capitals, for example, CITY

Pseudocode keywords are lower case, for example, loop, if …

Function/Method names are mixed case, for example, getRecord

Functions/Methods are invoked using the “dot notation” used in Java, C++, C#, and similar languages, for example, BIGARRAY.binarySearch( 27 ), the PHP equivalent of this would be BIGARRAY->binarySearch(27) Variables will be provided in exams and comments // used, for example: • N = 5 // the number of items in the array • SCOREHISTORY,getExam( NUM ) // get the student’s score on exam NUM When assigning values to variables = will be used: • N = 5 // indicates the array has 5 data items • VALUE[0] = 7 // assigns the first data item in the array a value of 7 Output—this term is sufficient to indicate the data is output to a printer, screen, for example: • output COUNT // display the count on the screen


Flowcharts Flowcharts are another possible design method. They are a more visual representation of the solution. Again, there are some conventions that are specified by IB. For sequential instructions:

Conditional Statements:

While loop:

For loop:


Input/Output: Get user input

Print score

Additional to the IB specification but has been seen in specimen papers is the symbol for pre-defined process: Activate or deactivate the fan as needed

Thinking Recursively A recursive problem in computer science is one where the solution depends on solutions to smaller instances of the same problem. In a programming sense, this is implemented by creating functions that then call themselves, almost like a snake swallowing its own tail. A common programming tactic is to take the problem and to divide it into smaller versions of the original problem and combine the results, often called the divide and conquer approach. Let’s take the example of factorials: 4! The conventional way of writing a solution to this problem would be: 4*3*2*1 However, remember a recursive solution defines the smaller instances of the same problem. The problem we started with is 4! How can we define a solution to this problem with a smaller instance of itself? 4*3! Now we have a partial solution that if we further divide using the same process we can arrive at an entire solution: 4 * 3! = 4 * 3 * 2! = 4 * 3 * 2 * 1! = 4 * 3 * 2 * 1 We need to think about the recursive element to this solution. What are we doing? If we were asked to define this for any number(n) what is the solution? n! = n * n-1! The last thing we need in a recursive solution is a BASE CASE, when should the recursion stop and unwind? This is the situation where the problem cannot be divided any further. In the case of the factorial problem, when we reach 1 then we cannot divide any more. So the base case for this problem is 1.


Programmed version of the factorial problem <?php echo factorial(10); function factorial ($n){ if($n == 1){ return 1;//base case } else{ return $n * factorial($n-1);//recursive case } } ?>

Tracing Tracing is the process of executing a program on paper. This might sound rather strange but it is useful to help both understand a programs execution and also to identify where and why bugs occur. To help demonstrate both tracing and the execution of the recursive Fibonacci solution above here is a trace of the call factorial(4) above. Factorial(4) N = 4, 4 * factorial(3) – 4 * 6 = 24 N= 3, 3* factorial(2) – 3*2 = 6 return 6 to factorial(3) above N=2, 2 * factorial(1) - 2 * 1 = 2 return 2 to factorial(2) above N= 1, return 1 to factorial(1) above


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.