Applications Development & Emerging Technologies
Applications Development & Emerging Technologies
HISTORY
- 1994/1995 (PHP 1.0)
o Developed by Rasmus Lerdorf
o To know how many visitors were re adding his online resume based on PERL/CGI script
o Personal Home Page (PHP)
- 1997 (PHP 2.0)
o PHP is based on C rather than PERL
o Personal Home Page/Form Interpreter
- 1998 (PHP 3.0)
o 50,000 users were using PHP to enhance their Web pages
o Developers joined Lerdorf
- 1999 (PHP 4.0)
o With core developers Zeev Suraski and Andi Gutmans
o PHP makes the most popular scripting language with more than one million user base by
Netcraff
o Hundreds of functions being added
o Dubbed the Zend scripting engine
- May 22, 2000 (PHP 4.0)
o PHP: Hypertext Preprocessor (recursive acronym)
o 3.6 million domain PHP installed
o Enterprise development
▪ Improved resource handling (scalability)
▪ Object-oriented support (Classes and Objects)
▪ Native session-handling support (session)
▪ Encryption (encryption algorithms)
▪ ISAPI Support (for IIS)
▪ Native COM/DOM (Windows applications)
▪ Native Java Support: (binding to java objects)
▪ PERL Compatible Regular Expressions
o New features, power, and scalability
HISTORY (cont.)
- PHP 5 (July 13, 2004)
o Vastly improved object-oriented capabilities: OOP improvement
o Try/catch exception handling
o Improved XML and Web Services support (Simple XML Support, using SOAP)
o Native support for SQLite
o Installed on 19 million domains
o 54 percent on all Apache module
- PHP 6 (2007 not yet released)
o Unicode Support (Native Unicode support for multilingual applications
o Security improvements
o New language features and constructs (64-bit type integer, foreach looping)
- PHP Key categories
o Practicality
▪ PHP is a loosely type language (no explicitly create, typecast, or destroy a
variable)
o Power
▪ More libraries and thousands of functions
o Possibility
▪ Native support is offered for more than 25 database products, including Adabas
D, dBase, Empress, FilePro, FrontBase, Hyperwave, IBM DB2, Informix, Ingres,
InterBase, mSQL, Microsoft SQL Server, MySQL, Oracle, Ovrimos, PostgreSQL,
Solid, Sybase, Unix dbm, and Velocis.
▪ Both structured and Object Oriented approach
o Price
▪ Free of charge
WEB ARCHITECTURE
SOFTWARE REQUIREMENTS – OPERATING SYSTEM: LINUX
Closing tag for php script echo – is a language construct that use to output one or more string.
print – is a function also use for outputting strings.
PHP COMMENTS
PHP Variables
- Variables are used as a container for values that can be used and manipulate PHP scripts.
- Variables in PHP must begin with a $ symbol.
- Variable might contain a string, numbers, arrays and objects.
- Setting the identifier for a variable must follow some rules.
1. First character can be ‘_’ or a letter.
2. It can only contain alpha-numeric characters and underscore (a-z,A-Z,0-9,_).
3. Don’t use white space in naming a variable.
- Variables in PHP is not explicitly declared
- Variables are case sensitive
- Example
$age, $firstName, $totalSalary, $_myValue,
$str1, $tmp01
Using ‘ ‘ (pair single quote), “ “ (pair double quotes) and . (dot character) PHP
1. Strings inside the ‘ and ‘ are interpreted as literal strings
• To display a ‘ character (single quote) as string use the escape character \ (backslash).
2. Strings inside the “ and “ are interpreted as literal strings with some exceptions
• $ inside “ and “ are interpreted as variables thus it will display the value rather than the
string that starts with $.
• To display the string that starts with $ and to display the double quote as string use the
escape character \(backslash).
3. To concatenate string values use the . (dot or period) character thus “abc“.”def” yields to a
value “abcdef”
Using ‘ ‘ (pair single quote), “ “ (pair double quotes) and . (dot character) PHP
EXAMPLE: OUTPUT:
OUTPUTTING DATA
- echo syntax
o void echo(string argument1[,…string argumentN]
- print syntax
o int print(argument)
- printf syntax
o boolean printf(string format [, mixed args])
EXAMPLE: OUTPUT:
- Compound Datatypes
o allow for multiple items of the same type to be aggregated under a single
representative entity. (Array and Objects)
o EXAMPLE 1:
o EXAMPLE 2:
PHP TYPE CASTING OPERATORS
EXAMPLE:
TYPE FUNCTIONS
EXAMPLE:
SUMMARY:
- PHP was developed originally by Rasmus Lerdorf
- Seev Suraski and Andi Gutsman were the one who contribute most in PHP development.
- PHP key categories are practicality, power, possibility, and price.
- PHP is a embedded language that is commonly embedded on HTML
- PHP is a server-side scripting language that runs its application on a Web server.
- Much of the PHP syntax were derive from C programming Language.
- You can integrate database easily in PHP.
- Standard PHP script are enclosed by <?php and ?> tags.
- Each statement in PHP must be terminated with a semi-colon(;).
- All variable in PHP must have a dollar ($) character before its identifier
- PHP variables are not explicitly declared.
- You can use echo, print, or printf function for outputting strings to internet browser.
- You can enclosed string using a pair of single quote or pair of double quote.
SUMMARY: (cont.)
- String inside a pair of single quote interpreted as literal except for the single quote itself.
- String inside a pair of double quote interpreted as literal with some exception like using dollar
character.
- To explicitly display the character on the internet browser use backslash (\) character like \$,
\’, \” and etc.
- Datatypes in PHP can be categorized as scalar or compound datatypes.
- You can explicitly change the type of a variable in PHP.
- PHP support some predefined type function that can be used in manipulating type variables
OPERATOR ASSOCIATIVITY
- is a characteristic of an operator specifies how operations of the same precedence are
evaluated as they are executed.
ARITHMETIC OPERATOR
ASSIGNMENT OPERATOR
INCREMENT AND DECREMENT OPERATOR
COMPARISON OPERATOR
LOGICAL OPERATOR
EQUALITY OPERATOR
BITWISE OPERATOR
ESCAPE SEQUENCES
Nested if…else
EXAMPLE:
PHP CONTROL STRUCTURES: BREAK, CONTINUE, AND GOTO STATEMENT
Break
- break statement is placed within the code of a loop to cause the program to break out of the
loop statement.
Continue
- continue statement causes execution of the current loop iteration to end and commence at
the beginning of the next iteration.
goto … label:
- goto statement is used to jump to other section of the program to support labels.
EXAMPLE:
SUMMARY:
- Simplifying expression needs to follow a general precedence set by PHP scripts.
- An operator specifies a particular action in a given expression.
- Available PHP operators are arithmetic, conditional, assignment, logical and bitwise operator.
- Operator precedence characteristics determines the order in which they evaluate the
operands surrounding them.
- Operator associativity characteristic specifies how operations of the same precedence are
evaluated as they are executed.
SUMMARY: (cont.)
- Post increment or decrement always execute the statement first before incrementing or
decrementing the value of a given variable.
- Pre increment or decrement always increment or decrement its variable value before
executing the statement.
- Escape sequence has its own special task in executing the string as an output.
- Conditional statements are statement the will execute statement inside a block if a given
condition is true
- Use compound expression to minimize the process of coding using nested if statement.
- break statement caused the program to break if used.
- continue statement end the current loop and continue to next iteration.
- goto statement used to jump to other section of the program.
- Alternative enclosure syntax is available for if, while, for, foreach, and switch control
ASSIGNMENT OPERATORS
- it is used with numeric values to write a value to variable.
- The basic assignment operators in PHP is "=". It means that the left operand get set to the value
of the assignment expression on the right.
PHP INCREMENT AND DECREMENT OPERATOR
- Increment operators are used to increment variables value.
- Decrement operators are used to decrement variables value.
PHP IF STATEMENT
Syntax:
if(condition) {
code to be executed if condition is true;
}
PHP THE IF ELSE STATEMENT
- executes some codes if a condition is true and another code if that condition is false.
Syntax:
if(condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
PHP LOOPS – are used to execute the same block of codes again and again as along as a certain
condition is true.
• while loop – loops through a block of codes as long as the specified condition is true.
• do while – loops through a block of codde once, and then repeats the loop as long as the
specified condition is true.
• for loops – loops through a block of code a specified numbers of times
• for each loops – loops through a block of code for each element in an array.
WHILE LOOP
Syntax:
while (condition is true){
code to be executed;
}
DO WHILE LOOP – It will always execute the block of code once, it will check t the condition, and
repeat the loop while the specified condition is true.
Syntax:
do {
code to be executed;
} while (condition is true);
FOR LOOP
Syntax:
for(int counter; test counter ; increment counter) {
code to be executed for each iteration;
}
Parameters:
• int counter – Initialize the loop counter value.
• test counter – Evaluate for each loop iteration. if it evaluates to True, the loop continues if it
evaluates to FALSE, the loop ends.
• increment counter – Increases the loop counter value.
FOREACH LOOP
- for every loop iteration, the value of the current array element is assigned to $value and the
array pointer is moved by one, until it reaches the last array element.
Syntax:
foreach ($array as $value) {
code to be executed;
}
Subtopic 1: PHP Arrays and User Defined Functions
PHP ARRAYS
- ARRAYS – Is used to aggregate a series of similar items together, arranging and dereferencing them in
some specific way.
o Each member of the array index references a corresponding value and can be a simple
numerical reference to the value’s position in the series, or it could have some direct correlation
to the value.
o PHP array does not need to declare how many elements that the array variable have.
o Array index in PHP can be also called as array keys.
o Array can be used as ordinary array same as in C and C++ arrays
Output:
Example: Code 3
Output
Example: using var_dump()
:
Example 2
Output
Example: using var_dump()
:
ARRAY: MULTIDIMENSIONAL ARRAYS
ARRAYS: SORTING
FUNCTIONS DESCRIPTION
sort($array) Sorts by value; assign new numbers as the keys
rsort($array) Sorts by value in reverse order; assign new number
asort($array) Sorts by value; keeps the same key
arsort($array) Sorts by value in reverse order; keeps the same key
ksort($array) Sorts by key
krsort($array) Sorts by key in reverse order
usort($array, functionname) Sorts by a function
ARRAYS: SORTING (cont.)
INCLUDING FILES
- You can separate your PHP file and embed it to your html by using PHP include functions.
- Most of the developers used include functions for their header and footer. Also, some use this to write
their database connection and so on.
- You may write the file with an extension name of .inc rather than .php to serve as a fragment of your
program code.
- In some scripts, a file might be included more than once, causing function redefinitions, variable
reassignments, and other possible problems.
- Syntax:
o include(“filename.inc”);
o include_once(“filename.inc”);
o require(“filename.inc”);
o require_once(“filename.inc”);
INCLUDING FILES (cont.)
- Including Files: include() function (file not found)
MATHEMATICAL FUNCTION
- rand() function – used to generate random integers
o syntax:
▪ int rand(void)
▪ int rand(int $min, int $max)
- ceil() function – returns the next highest integer by rounding the value upwards
o syntax: float ceil(float $value)
- floor() function – returns the next lowest integer by rounding the value downwards
o syntax: float floor(float $value)
- min() function – Return the smallest value
o syntax:
▪ mixed min(array $values)
▪ mixed min(mixed $values1, mixed $values2[,mixed $...])
- max() function – Return the highest value
o syntax
▪ mixed max(array $values)
▪ mixed max(mixed $values1, mixed $values2[,mixed $...])
MATHEMATICAL FUNCTION (cont.)
- Mathematical Function: rand(), ceil(), floor(), min(), max()
- Function for String Manipulation: strlen(), strpos(), strrev(), strtolower(), strtoupper(), substr()
FUNCTION FOR STRING MANIPULATION (cont.)
- ucfirst() function
o Make a string’s first character uppercase
o syntax: string ucfirst( string $str )
- ucwords() function
o converts string to uppercase
o syntax: string ucwords ( string $str )
- trim() function
o stripped white spaces or other characters from the beginning and end of a string.
o syntax: string trim ( string $str [, string $charlist ]
- ltrim() function
o strip white spaces or other characters from the beginning of a string.
o syntax: string ltrim ( string $str [, string $charlist ] )
- rtrim() function
o strip white spaces or other characters from the end of a string.
o syntax: string rtrim ( string $str [, string $charlist ] )
- strip_tags() function
o strip HTML and PHP tags from a string.
o syntax: string strip_tags ( string $str [, string $allowable_tags ] )
- Function for String Manipulation: ucfirst(), ucwords(), trim(), ltrim(), rtrim(), strip_tags()
DATE MANIPULATION: DATE() FUNCTION
- used to format a local time or date
- returns a string formatted according to the given format string.
- syntax: string date ( string $format [, int $timestamp = time() ] )
DATE MANIPULATION: DATE() FUNCTION (cont.)
count () function – Is used to return the length (the number of elements)of an array.
Associative Array – are arrays that use named keys that you assign to them.
Ex: $age ['Peter'] = "35";
$age ['Jeffrey'] = 37";
$age ['Buban'] = "43";
• foreach() function - statement used to iterate or loop through the element in an array.
• $key - the name of the variable where you want yo store the key(optional)
• $value - the name of the variable where you want to store the value.
$fruits = array (
array("Mango" , 22, 18),
array("Apple" , 15, 13),
array("Banana" , 5, 2),
array("Guyabano" , 17, 15));
$fruits array contains four array and it has two indices: row and column
PHP Sorting Arrays – the elements in an arrray can be sorted in apphabetical or numerical order, descending or
ascending.
sort() function - sort arrays in ascending order.
rsort() function - sort arrays in descending order.
asort() function - sort associative arrays in ascending order accdg. to the value.
ksort() function - sort associative arrays in ascending order accdg the key.
arsort() function - sort associative arrays in descending order to the value
krsort() function - sort associative arrays in descending order accdg to the key.
PHP Functions
- is a group of statements that performs a specific task. Functions are design to allow you to reuse the same
code in different locations.
2 TYPES OF FUNCTIONS:
1. User defined functions – functions that are provided by the user of the program.
2. Predefined Functions – functions that are built in into PHP to perform some standard operations.
Sample Program:
<html>
<body> Explanation:
- We created function named writeMsg()
<?php - the opening curly brace indicated the
function writeMsg(){ beginning of the function code and the
echo "Hello Philippines!!!"; closing curly brace indicates the end of the
}
function
writeMsg(); // call of the function
- The function outputs the "Hello Philippines!!!"
?>
</body> - To call the function, just write its name
<html> followed by parenthesis ();
CCS0043LL
<html>
<body>
<?php
function familyName($fname){
echo "$fname Buban. <br>";
}
familyName("Michael");
familyName("Allah");
familyName("Jeffrey");
?>
</html>
</body>
strict function – it will throw a Fatal Error if the data type mismatched.
Syntax: (strict_types = 1);
Sample Program:
<html>
<body>
<?php declare (strict_type = 1);
function AddNumber (int $a, int $b) {
return $a + $b;
}
echo AddNumber(5,"5 days");
?>
</body>
</html>
PHP Functon returning Values – to let a function return a value, use the return statement
<html>
<body>
<?php
function sum(int $x, int $y){
$z = $x+$y;
return $z;
}
echo "5+10 =" . sum(5,10) . "<br>";
echo "10+3 =" . sum(10,3) . "<br>";
echo "4+8 =" . sum(4,8);
?>
</body>
</html>
Passing Arguments by References
- In Php, arguments are usually passed by value, which means that a copy of the value is used in the
function and the variable that was passed into function connot be changed.
- When a function argument is passed by reference, changes to the argument also change the variable
that was passed in. To turn a function argument into reference the & operator is used.
Sample Program:
<html>
<body>
<?php
function add_five(&$value){
$value += 5;
}
$num = 2;
add_five($num);
echo $num;
?>
</html>
</body>
OUTPUTS
before button was clicked submit get button was clicked submit post button was clicked
COOKIES
- are mechanism for storing data in the remote browser and thus tracking or identifying return users.
- small amount of information containing variable=value pair (user’s computer).
- users can refuse to accepts cookies.
- Managing cookies can be done using setcookie() function
- syntax: setcookie()
bool setcookie ( string $name [, string $value [, int
$expire = 0 [, string $path [, string $domain [, bool
$secure = false [, bool $httponly = false ]]]]]] )
Example: PHPSetCookies.php Output 1: cookies were set
COOKIES (cont.)
Example: PHPDeleteCookies.php
Output 3: delete cookies
Subtopic 2: Session
SESSION
- are mechanism for storing data on the server itself.
- is the time that a user spends at your Web site.
- more secure than cookies and can store much more information
- to open a session use session_start() function
o always set at the beginning of each Web page.
- to close the session use session_destroy() function
o gets rid of all the session variable information that’s stored in the session file.
o the statement does not affect the variables set on the current page
- to unset session variables use unset() function
o syntax: unset($_SESSION[ ‘Varna me’]);
Example: PHPUnsetSession.php
Example: PHPSetSession.php
Example: PHPDeleteSession.php
Example: PHPDisplaySession.php
REGULAR EXPRESSION
- were used to efficiently search for patterns in a given text.
- also known as regex or regexp.
- PHP implements Percl Compatible Regular Expression (PCRE)
- PCRE function starts with preg_
- preg_match() function
o Performs a regular expression match
o Syntax: int preg_match ( string $pattern , string $subject [, array &$matches
[, int $flags = 0 [, int $offset = 0 ]]] )
RELATIONAL DATABASE
- Stores information across multiple related tables.
- Composed of Primary Table and Related Table (child table)
• Primary key is a field that contains a unique identifier for each record in a primary table.
• Foreign key is a field in a related table that refers to the primary key in a primary table.
• Normalization is the process of breaking tables into multiple related tables to reduce redundant
information.
A BIT HISTORY
- improvement was done by a Swedish programmer Monty Widenius
- MySQL rapidly grew until, although it's still a fast, light database, it's also a pretty powerful one.
- MySQL is written in C++, compiles using GNU gcc, and is pretty portable -- it has been ported to OS/2,
Windows 95 and NT, as well as a variety of flavours of UNIX
FEATURES OF SQL
- According to the article of Ian Gilfillan in 2004, the following are the claimed features of MySQL:
• High availability
• High scalability
• High performance
MySQL Prompts
Example:
BASIC COMMANDS
- To clear command:
o mysql > \c
- Get status from the server:
o mysql > status
o mysql > \s
- Need help?:
o mysql > \h
o mysql > ?
o mysql > \?
o mysql > help
- To display the SQL version:
o mysql > select version();
EXAMPLE:
- To quit
o mysql > \q
o mysql > quit
o mysql > exit
Subtopic 2: MySQL
- Data manipulation Language (DML)
- Create, Select, Insert, Update, Delete
DATABASE CREATION
- SYNTAX: CREATE DATABASE <databasename>;
o mysql> CREATE DATABASE DBStudent;
- Showing and Using the Database:
o mysql> SHOW databases;
- SYNTAX: USE <database name>;
o mysql> USE DBStudent;
Note: It should display Query OK, 1 row affected as a result.
TABLE CREATION
- SYNTAX: CREATE TABLE <table name> (field name field type(size) condition...);
o mysql> CREATE TABLE tblStudent(id int(5) primary key not null auto_increment, name char(25));
- Display the structure of the table:
o SYNTAX: DESCRIBE <table name>;
- mysql > DESCRIBE tblStudent;
- Displaying the list of table: mysql> SHOW tables;
SAMPLE SCREEN
- Dropping column
o Syntax: ALTER TABLE <table name> DROP <column name>;
OTHER STRUCTURE
TO DELETE A TABLE
- Syntax: DROP TABLE <table name>;
o mysql> DROP TABLE tblStudent;
- * You remove a table from the database when it is not required. You use the DROP TABLE statement to remove
a table. When you remove a table, all the data from the table is also deleted.
TO DELETE DATABASE
- Syntax: DROP DATABASE <database name>;
- mysql> DROP DATABASE dbStudent;
- Example: MAX
- Example: MIN
mysql_affected_rows()
- mysql_affected_rows() get the number of affected rows by the last SELECT, INSERT, UPDATE, REPLACE or
DELETE query.
- Syntax: mysql_affected_rows()
- Example:
- mysql_num_rows() retrieves the number of rows from a result set. This command is only valid for
statements like SELECT or SHOW that return an actual result set.
- Syntax: mysql_num_rows()
- Example:
EXAMPLE