Web CH 5 - PHP Part 1
Web CH 5 - PHP Part 1
• History
• Introduction
• PHP Environment Setup
• Dynamic Webpage Request Response Procedure
• PHP Language Basics
• PHP comments
• Variable in PHP
• Data Types
• Type Casting
• Arithmetic operators
• Assignment operator
• Comparison operator
• Logical operators
• Control structures
• Forms & PHP
2
History
• PHP is a popular high-level scripting language used by a range of organizations and
developers.
• Originally developed as a small Perl project by Rasmus Lerdorf in 1994.
• PHP was intended as a means to assist in developing his home page, and as such he
named it Personal Home Page (PHP) Tools.
• When Lerdorf was contracted to work for the University of Toronto to build a dial-up
system for students to access the Internet, he had no means of connecting Web sites to
databases. To solve this problem, the enterprising Lerdorf replaced his Perl code with a C
wrapper that added the capability to connect his Web pages to a MySQL database.
• As his small project grew, he gave away his changes on the Internet as an Open Source
project and cordially received improvements from other programmers with an interest in
PHP.
• The language was later renamed to the current recursive acronym PHP: Hypertext
Preprocessor by Zeev Suraski and Andi Gutmans after they rewrote the parser in 1997.
• The software continued to develop and now forms the comprehensive PHP platform we
know today. 3
Introduction
• PHP is an acronym for "PHP: Hypertext Preprocessor“
• PHP is a robust, server-side, open source scripting language
• PHP is cross platform
• PHP is a server side scripting language that is embedded in HTML
• It is used to manage dynamic content, databases, session tracking, even
build entire e-commerce sites
• PHP provides a solid and well-defined programming language that includes
support for
⁕ Object-orientated programming,
⁕ Conditions,
⁕ File handling,
⁕ Arithmetic, and more 4
Conti..
• It is integrated with a number of Common uses of PHP
popular databases, including MySQL, • PHP performs system functions, i.e. it can create,
PostgreSQL, Oracle, Sybase, Informix, open, read, write, and close from files on a system
and Microsoft SQL Server • PHP can handle forms, i.e.
• PHP supports a large number of major • gather data from files, save data to a file, send
protocols data through email, and return data to the
• PHP4 added support for Java and user
distributed object architectures • PHP allows to add, delete, modify elements within
(DCOM and CORBA), making n-tier your database
development a possibility (N-tier architecture also • Access cookies variables and set cookies
called multi-tier architecture a is client–server architecture in which
presentation, application processing and data management functions are • restrict users to access some pages of your website
physically separated.)
• encrypt data
•DCOM
PHP Syntax is C-Like
(Distributed Component Object Model) and CORBA (Common Object Request Broker Architecture) are two middleware solutions for handling distributed
objects. Both DCOM and CORBA frameworks provide client-server type of communications. To request a service, a client invokes a method implemented by a remote
object, which acts as the server in the client-server model. 5
Conti..
• In order to develop and run PHP Web pages three vital components need to
be installed on your computer system.
♠ Web Server - PHP will work with virtually all Web Server software, including
Microsoft's Internet Information Server (IIS) but then most often used is freely
available Apache Server.
♠ Database - PHP will work with virtually all database software, including Oracle and
Sybase but most commonly used is freely available MySQL database.
♠ PHP Parser - In order to process PHP script instructions a parser must be installed
to generate HTML output that can be sent to the Web Browser.
7
Where to Start?
9
Conti…
1. You enter http://server.com into your 7. The PHP interpreter executes the PHP code.
browser’s address bar.
8. Some of the PHP contains MySQL statements,
2. Your browser looks up the IP address for which the PHP interpreter now passes to the
server.com. MySQL database engine.
3. Your browser issues a request to that address 9. The MySQL database returns the results of the
for the web server’s home page. statements back to the PHP interpreter.
4. The request crosses the Internet and arrives at 10. The PHP interpreter returns the results of the
the server.com web server. executed PHP code, along with the results
5. The web server, having received the request, from the MySQL database, to the web server.
fetches the home page from its hard disk. 11. The web server returns the page to the
6. With the home page now in memory, the web requesting client, which displays it.
server notices that it is a file incorporating PHP
scripting and passes the page to the PHP
10
interpreter.
PHP – Language Basics
• Example
<?php
$txt = "Hello World!";
$number = 16;
?>
16
Conti..
• Variable Naming Rules
♠ A variable name must start with a letter or an underscore "_"
♠ A variable name can only contain alpha-numeric characters and
underscores (a-Z, 0-9, and _ )
♠ A variable name should not contain spaces.
♠ If a variable name is more than one word,
⁕ Separated with underscore ($my_string), or
⁕ Capitalization ($myString)
17
Data types
• Boolean (bool or boolean)
⁕ Simplest of all
⁕ Can be either TRUE or FALSE
• Integer (int or integer)
⁕ Hold integer values (signed or unsigned)
• Floating point (float or double or real)
⁕ Hold floating point values
• String (string)
⁕ Hold strings of characters within either ‘ or ‘’
⁕ Escaping of special characters can be done using \
⁕ Ex. “this is a string”, ‘this is another string’, “yet \”another\” one”
18
Conti..
• Array
⁕ Collection of values of the same data type
• Object
⁕ Instance of a class
• Resource
⁕ Hold a reference to an external resource created by some
functions
• NULL
⁕ Represents that a variable has no value
⁕ A variable is considered to be NULL if
• it has been assigned the constant NULL.
• it has not been set to any value yet.
19
• it has been unset() (destroys the specified variables.)
Type Casting
• Type Casting is to use the value of a variable with
different data type.
• In other word typecasting is a way to utilize one data
type variable into the different data type.
• The casts allowed are:
⁕ (int), (integer) - cast to integer
⁕ (bool), (boolean) - cast to boolean
⁕ (float), (double), (real) - cast to float
⁕ (string) - cast to string
⁕ (array) - cast to array
⁕ (object) - cast to object
20
String in PHP
21
Conti…
• The concatenation operator (.) is used to put two string values together
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>
<?php
echo strpos("Hello world!","world");
?>
23
Arithmetic operators
Operator Description Example Result
+ Addition x=2 4
x+2
- Subtraction x=2 3
5-x
* Multiplication x=4 20
x*5
/ Division 15/5 3
5/2 2.5
% Modulus (division remainder) 5%2 1
10%8 2
10%2 0
++ Increment x=5 x=6
x++
-- Decrement x=5 x=4
x--
24
Assignment operator
Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y
25
Comparison operator
Operator Description Example
== is equal to 5==8 returns false
26
Logical operators
Operator Description Example
• Conditional constructs
⁕ If … else if ( condition1 )
{
statements
}elseif ( coditon2 )
{
statements
}
elseif( condition3 )
{
…
}else{
statements
}
28
Conti…
Conditional statement
( condition ) ? True_value :
False_value
Example:
<?php
$year = (int)date(“Y”);
echo ( $year % 4 == 0 ) ? “Leap Year” : “Not Leap Year”;
?>
29
Conti..
Switch Example <?php
$x=2;
switch ( expression ){ switch ($x)
case value 1: {
case 1:
statements
echo "Number 1";
break; break;
… case 2:
echo "Number 2";
case value n:
break;
statements case 3:
break; echo "Number 3";
break;
default: default:
statements echo "No number between 1 and 3";
} }
?>
30
Conti..
Looping constructs
• while - loops through a block of code if and as long as a specified
condition is true
• do...while - loops through a block of code once, and then
repeats the loop as long as a special condition is true
• for - loops through a block of code a specified number of times
• foreach - loops through a block of code for each element in an
array
31
Conti..
for loop
for( initialization; condition; increment ){
loop body <html>
<body>
} <?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br
/>";
}
?>
</body> 32
Conti…
foreach loop <html>
<body>
foreach( array as [key=>]value ){ <?php
loop body $x=array("one","two","three");
} foreach ($x as $value)
{
echo $value . "<br>";
}
?>
</body>
</html>
33
More… Example
$arr = array(“Name”=>”Abebe”, “Dept”=>” SE”, “Year”=>4,
“CGPA”=>3.5);
foreach( $arr as $key=>$value ){
echo $key . “ = “ . $value . “<br>”;
}
//output
Name = Abebe
Dept = CS
Year = 3
CGPA = 3.5
34
Conti..
POST GET
40
Any
Questions?
41