You are on page 1of 17

Server Side Programming: Programming in PHP

Part 1
Course Title: Web Systems and Technologies
Course Code: CS-3548
Instructor: Nauman Ahmed
PAKISTAN INSTITUTE OF ENGINEERING & 1
TECHNOLOGY
OUTLINE

• Server-Side Dynamic Web Programming


• CGI Programming
– PHP

• Incorporating PHP Within HTML


• PHP tags
– <?php ?>

PAKISTAN INSTITUTE OF ENGINEERING & 2


TECHNOLOGY
OUTLINE

• The Structure of PHP


• Comments
• Basic Syntax
• Variables and Data Types
• Naming Conventions
• Operators and Variable Assignment
• Multiple-Line Commands
• Constants
• Functions

PAKISTAN INSTITUTE OF ENGINEERING & 3


TECHNOLOGY
Flow of Information

Input B Data
r HTML&
o PHP
User Data Web
w Program
Server
s (CGI)
e HTML
Output Document Data
r
HTML Flow
Document
Database
Management
System

PAKISTAN INSTITUTE OF ENGINEERING & 4


TECHNOLOGY
Server-Side Dynamic Web Programming

• CGI is one of the most common approaches to server-side


programming
• "CGI" stands for "Common Gateway Interface.
• Method by which a web server can obtain data from (or send data to)
databases, documents, and other programs, and present that data to
viewers via the web

PAKISTAN INSTITUTE OF ENGINEERING & 5


TECHNOLOGY
Server-Side Dynamic Web Programming

• Open-source, server-side scripting language


• PHP (Hypertext Preprocessor)
• Interpreted language, scripts are parsed at run-time rather than
compiled beforehand
• 1. Executed on the server-side.
• 2. Source-code not visible by client.
• View Source’ in browsers does not display the PHP code
• All PHP statements end with a semi-colon

PAKISTAN INSTITUTE OF ENGINEERING & 6


TECHNOLOGY
Incorporating PHP Within HTML
– Each PHP script must be enclosed in the reserved PHP tag
Example 3-1. Invoking PHP
<?php
echo "Hello world";
?>
– PHP supports many databases (MySQL, Informix, Oracle,
Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
– PHP is open source software
– PHP is free to download and use

PAKISTAN INSTITUTE OF ENGINEERING & 7


TECHNOLOGY
The Structure of PHP
– Using Comments
• In PHP, we use // to make a
single-line comment or /* and
*/ to make a large comment
block.

PAKISTAN INSTITUTE OF ENGINEERING & 8


TECHNOLOGY
The Structure of PHP

• Basic Syntax
– Semicolons
– The $ symbol
• Variables
– String variables
– Numeric variables
– Arrays $team = array('Bill', 'Mary', 'Mike', 'Chris', 'Anne');
– Two-dimensional arrays

PAKISTAN INSTITUTE OF ENGINEERING & 9


TECHNOLOGY
The Structure of PHP

• Naming Conventions
• Variable names must start with a letter of the alphabet or the _
(underscore) character.
• Variable names can contain only the characters a–z, A–Z, 0–9, and _
(underscore).
• Variable names may not contain spaces. If a variable must comprise
more than one
• word, it should be separated with the _ (underscore) character (e.g.,
$user_name).
• Variable names are case-sensitive. The variable $High_Score is not
the same as the variable $high_score.

PAKISTAN INSTITUTE OF ENGINEERING & 10


TECHNOLOGY
The Structure of PHP

• Operators
– Arithmetic operators
– Assignment operators
– Comparison operators
– Logical operators
• Variable Assignment
– Variable incrementing and decrementing
• += and −= operators
– String concatenation
• Escaping characters
PAKISTAN INSTITUTE OF ENGINEERING & 11
TECHNOLOGY
The Structure of PHP

• Multiple-Line Commands

PAKISTAN INSTITUTE OF ENGINEERING & 12


TECHNOLOGY
The Structure of PHP

• Variable Typing
– PHP is a very loosely typed language.
Example 3-10. Automatic conversion from a number to a string
<?php
$number = 12345 * 67890;
echo substr($number, 3, 1);
?>

PAKISTAN INSTITUTE OF ENGINEERING & 13


TECHNOLOGY
The Structure of PHP

• Constants
– define("ROOT_LOCATION", "/usr/local/www/");
» Holds the location of your server root.
• $directory = ROOT_LOCATION;
• The Difference Between the echo and print Commands
– The differences are small: echo has no return value while print has a
return value of 1 so it can be used in expressions. echo can take
multiple parameters (although such usage is rare) while print can
take one argument. echo is marginally faster than print.
» The echo statement can be used with or without parentheses:
echo or echo().
PAKISTAN INSTITUTE OF ENGINEERING & 14
TECHNOLOGY
The Structure of PHP

• The Difference Between the echo and print Commands


– Echo has no return value while print has a return value of 1 so it can
be used in expressions. echo can take multiple parameters
(although such usage is rare) while print can take one argument.
echo is marginally faster than print.
» The echo statement can be used with or without parentheses: echo or
echo().
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
PAKISTAN INSTITUTE OF ENGINEERING & 15
TECHNOLOGY
The Structure of PHP
» The print statement can be used with or without parentheses: print or print().
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

PAKISTAN INSTITUTE OF ENGINEERING & 16


TECHNOLOGY
The Structure of PHP

• Functions
– function test($timestamp){ return date("l F jS Y", $timestamp);}
• Variable Scope
– Local variables
– Global variables
» global $is_logged_in;
– Static variables
» static $count = 0; (keep its value for the next time the function is
called)
– Superglobal variables
PAKISTAN INSTITUTE OF ENGINEERING & 17
TECHNOLOGY

You might also like