Tuesday, April 20, 2010

Workshop 2 PHP Basics

Exercise 5 PHP exercises

As we have chosen to solve Case A in Exercise 5, we will now cover some PHP exercises to provide practice in development skills that are needed in developing eSystems.

In this second workshop we will begin on practicing simple codes for PHP development.
W will start with all the basics first, here we go!

Remember that all PHP files will have .php extension

Commenting codes in PHP works similarly to other programming languages, here's an example:


# This starts the comments

// This also starts the comments

Multi line comments are also possible like this:

/*

This is the start of the comment.

Anything within this lines will not

Be interpreted by the PHP engine

*/ 
Also remember that inside everly line of a PHP code, we need to include a terminator with a semicolon  ;
example:

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


Variables

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; 
?> 

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>

0 comments:

Post a Comment