Monday, April 19, 2010

Workshop 2

ITC382 Client Server Applications
Tutorial 2

BEGINNING PHP CODING

1. All PHP files have an extension of .php
2. All PHP will start with the following code

<?php
code
?>


3. In order to comment a code, it begins with two forward slashes // or a # sign. PHP engine usually ignores these lines. Comments can be used to annotate codes to make them more readable

# This starts the comments
// This also starts the comments

Multi lines comment is also possible. It stars /* and ends with */.

/*
This is the start of the comment.
Anything within this lines will not
Be interpreted by the PHP engine
*/


4. Every line in PHP needs a terminator. The terminator is a semicolon ;

<?php
echo "This is my first PHP program " ;
?>




Variables

1. In PHP variables starts with $ sign followed by the name.

$variable1

<?php
// Adding two numbers together

// Assigning the variable with the value 88
$num1 = 88;
$num2 = 100;

echo "The first number is", $num1;
echo "  The second number is ",  $num2 ;

// Adding the two numbers
echo " The total is ", $num1 + $num2;
?>




For the list of logical and mathematical operators, please refer to the prescribed text.

Data Types

The six standard data types in PHP are

Integer - Whole number, 10
Double - Floating point number , 9.999
String - AlphaNumeric characters, " This is PHP "
Boolean - True/False values, TRUE
Object - Instance of class
Array - An ordered set of keys and values

Testing the variable types

<html>
<head>
<title>Listing 4.1 Testing the type of a variable</title>
</head>
<body>
<?php
$testing; // declare without assigning
print gettype($testing); // null
print "<br>";
$testing = 5;
print gettype($testing); // integer
print "<br>";
$testing = "five";
print gettype($testing); // string
print("<br>");
$testing = 5.0;
print gettype($testing); // double
print("<br>");
$testing = true;
print gettype($testing); // boolean
print "<br>";
?>
</body>
</html>


FLOW CONTROL


1. If Statements
<?php

$subject = "ITC382" ;
If ($subject == "ITC382") {
         Echo " I am in ITC382 class";
}
?>



2. If .. else statements

<?php

$subject = "ITC254" ;
If ($subject == "ITC382") {
         Echo " I am in ITC382 class";}
Else {echo " I am in the wrong class";) }  
?>


3. If..elseif ..else statements

<?php

$subject = "ITC254" ;
If ($subject == "ITC382") {
         Echo " I am in ITC382 class";}
Else if ($subject == "ITC254") {
         echo "I am in ITC254 class ";}
else { echo " I'm not too sure which class I should be in. Maybe I should be in $subject"; }  
?>




4. Switch statements
Switch is commonly used to execute different codes based on each expression used.

<?php

$subject = "ITC382";

switch ($subject) {
case "ITC254":
echo "I am in ITC254 class";
break;
case "ITC382":
echo "I am in ITC382 class";
break;
default:
print "I must have gotten lost again, I am suppose to be in $subject";
break;
}
?>


5. While statements
A continuos loop until a condition is reached.

<?php

$count = 1;

while ($count < 10) {
echo " This is line $count", "<br>";
$count++; // Increment count by 1
}
?>


6. Do..While statements
It is similar to the Do statement except that termination is at the end of the code.

<?php

$count = 0;

do {
echo " This is line $count", "<br>";
$count++; // Increment count by 1
} while ($count < 10);
?>


7. For statements
For statements are also similar to while statements, it is a more structured way to write looping statements with the initialization, testing and modification expression nested within the command.

<?php
for ($count =1; $count <10; $count++) {
echo "This is line $count", "<br>";
}
?>


FUNCTIONS

Functions are blocks of code that you can call from the main program.

<?php

function print2lines() {
         echo "This is line 1", "<br>";
         echo "This is line 2", "<br>";
}
print2lines();


You can also pass arguments into a function

<?php

function printlines($txt) {
         echo  " $txt <br>";
}
printlines("This is line 1");
printlines("This is line 2");
printlines("This is line 3");
?>


You can also get the function to return an argument

<?php

function addnums ($num1, $num2) {
         $result = $num1 + $num2;
         return $result;
}
echo addnums(5,9);
// print 14  

?>


Variables declared in a function remains only in the function. You can however use global variables should you wish to use variables within a function.

<?php

$value = 100;

function currentmoney() {
         global $value;
         echo " I currently have RM $value <br>";
}
currentmoney();
?>


ARRAYS

Creating arrays

In order to create an array, you would need to you the array function:

$lecturers = array("Yann", "Sam", "Ven Yu", "Tony", "Anitha");

You can also do this as well

$lecturers[] = "Yann" ;
$lecturers[] = "Sam";
$lecturers[] = "Ven Yu";
$lecturers[] = "Tony";
$lecturers[] = "Anitha";

Both will create an array called lecturers with 5 elements. By default the first element starts at position 0.

<?php
$lecturers = array("Yann", "Sam", "Ven Yu", "Tony", "Anitha");
echo $lecturers(1) ; // This will print Sams name
?>


Associative Arrays

Instead of using numbers, you can also use named keys to number the arrays instead as illustrated in the following example.

$lecturers = array (
"name" => "Yann",
"dept" => "VIGIM",
"subjects" => "ITC382"
);

If you write

Echo $lecturers['dept']; // this will return VIGIM

You can also make additions to an already existing associative array.

$lecturers['interests'] = "Video Games";


Multidimensional Arrays

This holds arrays of an array.

<?php

$lecturers = array (
                            array (
"name" => "Yann",
                                     "dept" => "VIGIM",
                                     "subjects" => "ITC382"
                                     ),
array (
"name" => "Sam",
                                     "dept" => "VIGIM",
                                     "subjects" => "ITC211"
),
array (
"name" => "Tony",
                                     "dept" => "MT",
                                     "subjects" => "ITC123"
                                       ),
                        );

                        echo $character[1]['subjects']; // this will print out ITC211

?>


OBJECTS

An object is typically a sort of container that consists of
variables
functions
etc
It is similar to a class ohject in JAVA

<?php

class lecturers {
         var $name = "Yann";
         var $subjectcode  = "ITC382" ;
         var $subjectname = "Client Server Applications";
         }

$mylecturer = new lecturers();
echo "My name is ".$mylecturer -> name. " and I teach " .$mylecturer -> subjectcode." " .$mylecturer -> subjectname;

?>


You can also change the properties of an object in the code as illustrated.

<?php

class lecturers {
         var $name = "Yann";
         var $subjectcode  = "ITC382" ;
         var $subjectname = "Client Server Applications";
         }

$mylecturer = new lecturers();
echo "My name is ".$mylecturer -> name. " and I teach " .$mylecturer -> subjectcode." " .$mylecturer -> subjectname;


// changing the object properties
echo "<br> This is to relfect the changes in object properties <br>";
$mylecturer -> name = "Sam";
$mylecturer -> subjectcode = "ITC211";
$mylecturer -> subjectname = "Multimedia Systems";

echo "My name is ".$mylecturer -> name. " and I teach " .$mylecturer -> subjectcode." " .$mylecturer -> subjectname;

?>

You can also add methods into your class objects as illustrated.

<?php

class displayname {
    function name() {
                  echo "My name is Yann " ;
                  }  
       }

$name = new displayname();
$name  -> name();
?>

No comments:

Post a Comment