Chapter Five
Server side Scripting Language (PHP)
OUTLINE
Introduction to PHP? Form Processing
Basics to PHP Uploading Files
PHP output Statement Cookies and session
Data types and Variables in PHP Working with Database
Arithmetic and Logical operators File Input output
Conditional and looping statements Date and Time
Arrays Mathematical function
Function PHP OOP
PREPARED BY: ALEKA TESFIE. DEPARTMENT OF COMPUTER SCIENCE @UOG 1
What is PHP?
Client side scripting language executed before the data is sent to the server.
Server-side processing is used to interact with permanent storage like databases or
files.
The acronym PHP refers to Hypertext Preprocessor :- used to create dynamic and interactive websites.
it can contain text, HTML, CSS, JavaScript, and PHP code executed on the server and only the result is
turn back to the end users.
It can create, open, read, write, delete, and close files on the server.
It can collect data from form, and it can send and receive cookies.
It can add, delete, modify data in your database and supports a wide range of databases.
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 2
Basics of PHP
syntax
<?php
echo "Hello World!";
?>
HTML pages can be viewed on any computer, directly in a browser where as
PHP scripts must be run on a PHP-enabled Web server.
When we use PHP code together with HTML, PHP codes can be any where.
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 3
Comments in PHP
1. Single line comment
2. Multiple line comment
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 4
Predefined and User Variables in PHP
A variable starts with the $ sign, followed by the name of the variable.
A variable name must start with a letter or the underscore character.
A variable name cannot start with a number.
A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, _ ).
Variable names are case sensitive ($y and $Y are two different variables).
PHP has no command for declaring a variable.
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 5
Variable scopes
1. Global Variables
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside
a function.
2. Local variables
A variable declared within a function has a LOCAL SCOPE and can only be accessed within
that function.
3. Static variables
Normally, when a function is completed or executed, all of its variables are deleted. However,
sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use
the static keyword when you first declare the variable.
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 6
Examples :
When we output the values of the two
variables inside the myTest() function, it
prints the value of $y as it is the locally
declared, but cannot print the value of $x
since it is created outside the function.
When we output the values of the two
variables outside the myTest() function, it
prints the value of $x, but cannot print the
value of $y since it is a local variable and
it is created inside the myTest() function.
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 7
global Keyword
The global keyword is used to access a global variable from within a
function. To do this, use the global keyword before the variables (inside
the function).
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 8
static Keyword
each time the function is called, that variable will
still have the information it contained from the
last time the function was called. The variable is
still local to the function.
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 9
PHP Output Statements
1. Echo: - can output one or more strings.
It is a language construct, and used with or without parentheses: echo or echo().
It is faster compared to print as echo does not return any value.
2. Print: - can only output one string, and returns always 1.
It is can be used with or without parentheses: print or print().
Just type the word print, followed by what you want to display: a simple message, the value of a
variable, the result of a calculation, and so forth.
To be clear, print doesn’t actually print anything; it just outputs data. When a PHP script is run
through a Web browser, that PHP output is received by the browser itself.
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 10
Data Types and Variables in PHP
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 11
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 12
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 13
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 14
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 15
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 16
PREPARED BY: ALEKA TESFIE DEPARTMENT OF COMPUTER SCIENCE @UOG 17