0% found this document useful (0 votes)
30 views

Web CH 5 - PHP Part 1

This document provides an overview of PHP, including: 1. PHP is a popular scripting language originally created in 1994 to build dynamic web pages and connect to databases. 2. PHP code is embedded within HTML and executed on the server to create dynamic web page content. It allows integration with many databases and supports object-oriented programming. 3. To run PHP, a web server like Apache that supports PHP, a PHP parser, and a database like MySQL need to be installed. When a request is made for a PHP file, the server passes it to the PHP interpreter to execute the code and return the results as HTML to the browser.

Uploaded by

Nasis Dereje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Web CH 5 - PHP Part 1

This document provides an overview of PHP, including: 1. PHP is a popular scripting language originally created in 1994 to build dynamic web pages and connect to databases. 2. PHP code is embedded within HTML and executed on the server to create dynamic web page content. It allows integration with many databases and supports object-oriented programming. 3. To run PHP, a web server like Apache that supports PHP, a PHP parser, and a database like MySQL need to be installed. When a request is made for a PHP file, the server passes it to the PHP interpreter to execute the code and return the results as HTML to the browser.

Uploaded by

Nasis Dereje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Contents

• 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..

✓ What is a PHP File?


⁕ PHP files may contain text, HTML tags and scripts
⁕ PHP files are returned to the browser as plain HTML
⁕ PHP files have a file extension of “.php”
✓ Why PHP?
⁕ PHP runs on different platforms (Windows, Linux, Unix, etc.)
⁕ PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
⁕ PHP is FREE to download from the official PHP resource:
www.php.net
⁕ PHP is easy to learn and runs efficiently on the server side 6
PHP Environment Setup

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

To get access to a web server with PHP support, you can:


⁕ Install Apache (or IIS) on your own server, install PHP, and MySQL
⁕ Or find a web hosting plan with PHP and MySQL support
____________________________________________________________________________________
____________________________
• Install an Apache server
• Install PHP
• Install MySQL
________________________________________________________________
______________________
⁕ WampServer
⁕ EasyPhp 8
Dynamic Webpage Request Response Procedure

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

• A PHP script always


⁕ starts with <?php and
⁕ ends with ?>
• A PHP script can be placed anywhere in the document.
• Syntax
⁕ PHP code should enclosed within:
⁕ <?php and ?> So that it is distinguished from HTML.
⁕ Hence, the PHP parser only parses code which is in between <?php and
?>
• PHP code can be embedded in HTML 11
Conti…
• A PHP file normally contains HTML tags, and some PHP scripting code.
• Below, we have an example of a simple PHP script that sends the text "Hello
World" back to the browser:<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
• Each code line in PHP must end with a semicolon.
• The semicolon is a separator and is used to distinguish one set of
instructions from another.
12
Conti…
A more complex example:
<?php if ($expression) { ?>
<strong>This is true.</strong>
<?php
} else {
?>
<strong>This is false.</strong>
<?php
}
?>
13
Conti…
• Comments in PHP
• In PHP, we use // to make a one-line comment or /* and */ to make a
comment block: <html>
<body>
<?php
// This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
14
Variables in PHP

• Variables are used for storing values, such as numbers,


strings or function results, so that they can be used many
times in a script.
⁕ Variables are used for storing a values, like text strings,
numbers or arrays.
⁕ When a variable is set it can be used over and over
again in your script
⁕ All variables in PHP start with a $ sign symbol.
15
Conti…
✓ Declaring PHP variable (loosely typed)
• Syntax
$var_name = value;

• 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

• Variable is used to store and manipulate text.


<?php
$txt="Hello World";
echo $txt;
?>

21
Conti…

• The concatenation operator (.) is used to put two string values together
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>

• The strlen() function is used to find the length of a string.


<?php
echo strlen("Hello world!");
?>
22
Conti…

• The strpos() function is used to search for a string or


character within a string

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

!= is not equal 5!=8 returns true

> is greater than 5>8 returns false

< is less than 5<8 returns true

>= is greater than or equal to 5>=8 returns false

<= is less than or equal to 5<=8 returns true

26
Logical operators
Operator Description Example

&& and x=6


y=3
(x < 10 && y > 1) returns true
|| or x=6
y=3
(x==5 || y==5) returns false
! not x=6
y=3
!(x==y) returns true
27
Control structures

• 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..

While loop <html>


while( condition ){ <body>
<?php
loop body
$i=1;
} while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
35
Conti..
Do-while loop <html>
<body>
do{ <?php
$i=0;
loop body do
{
}while( condition ); $i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?>
</body>
</html>
36
Conti..
• break
⁕ ends execution of the current for, foreach, while, do-while or switch structure.
⁕ accepts an optional numeric argument which tells it how many nested enclosing
structures are to be broken out of.
• continue
⁕ used within looping structures to skip the rest of the current loop iteration
⁕ execution continues at the condition evaluation and then the beginning of the
next iteration
• return
⁕ If called from within a function, the return statement immediately ends execution
of the current function, and returns its argument as the value of the function call
⁕ If return is called from within the main script file, then script execution ends. 37
Form and PHP
• PHP Forms and User Input
⁕ The PHP $_GET and $_POST variables are used to
retrieve information from forms, like user input.

POST GET

• PHP Form Handling


⁕ The most important thing to notice when dealing with
HTML forms and PHP is that any form element in an
HTML page will automatically be available to your PHP
38
scripts.
Conti.. (POST)
<html>
<body>
<form action=“register.php"
method="post">
Name: <input type="text" name="name"
/>
Age: <input type="text" name="age" />
<input type="submit" />
</form> <html>
</body> <body>
</html> Welcome <?php echo $_POST[‘name’]; ?>.<br />
You are <?php echo $_POST[‘age’]; ?> years old.
</body>
</html> 39
Conti.. (GET)

<form action=“register.php" method="get">


salary: <input type="text" name=“salary" />
<input type="submit" />
</form>
Welcome <?php echo $_GET[‘name’]; ?>.<br />
You are <?php echo $_GET[‘age’]; ?> years old!

40
Any
Questions?

41

You might also like