Download as pdf or txt
Download as pdf or txt
You are on page 1of 80

PHP Introduction

• PHP code is executed on the server.

What You Should Already Know


Before you continue you should have a basic understanding of the following:
• HTML
• CSS
• JavaScript

What is PHP?
• PHP is an acronym for "PHP: Hypertext Preprocessor"
• PHP is a widely-used, open source scripting language
• PHP scripts are executed on the server
• PHP is free to download and use

PHP is an amazing and popular language!


• It is powerful enough to be at the core of the biggest blogging system on the web (WordPress)!
• It is deep enough to run the largest social network (Facebook)!
• It is also easy enough to be a beginner's first server-side language!

What is a PHP File?


• PHP files can contain text, HTML, CSS, JavaScript, and PHP code
• PHP code is executed on the server, and the result is returned to the browser as plain HTML
• PHP files have extension ".php"

What Can PHP Do?


• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, and close files on the server
• PHP can collect form data
• PHP can send and receive cookies
• PHP can add, delete, modify data in your database
• PHP can be used to control user-access
• PHP can encrypt data
• With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash
movies. You can also output any text, such as XHTML and XML.

Why PHP?
• PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
• PHP is compatible with almost all servers used today (Apache, IIS, etc.)
• PHP supports a wide range of databases
• PHP is free. Download it from the official PHP resource: www.php.net
• PHP is easy to learn and runs efficiently on the server side
Introduction to PHP
PHP is one of the most widely used server-side scripting language for web development. Popular websites
like Facebook, Yahoo, Wikipedia etc., and our very own Study tonight, are developed using PHP.

PHP is so popular because it's very simple to learn, code and deploy on server, hence it has been the first
choice for beginners since decades.

In this tutorial series we will be covering all the important concepts of Php language from basics to
advanced and will also share some ready-to-use, useful code snippets for beginners to kickstart their web
development project.

What is PHP?
PHP stands for Hypertext Pre-Processor. PHP is a scripting language used to develop static and dynamic
webpages and web applications. Here are a few important things you must know about PHP:

1. PHP is an Interpreted language, hence it doesn't need a compiler.


2. To run and execute PHP code, we need a Web server on which PHP must be installed.
3. PHP is a server side scripting language, which means that PHP is executed on the server and the
result is sent to the browser in plain HTML.
4. PHP is open source and free.

Prerequisite for starting with PHP


Although we will cover all the concepts related to PHP in this tutorial series, but you must have a basic
understanding of HTML before starting with this tutorial.

Is PHP the right language?


If you are still confused about whether you should learn PHP or is PHP the right language for your web
project, then here we have listed down some of the features and use cases of PHP language, which will
help you understand how simple yet powerful PHP scripting language is and why you should learn it.

1. PHP is open source and free, hence you can freely download, install and start developing using it.

2. PHP has a very simple and easy to understand syntax, hence the learning curve is smaller as

compared to other scripting languages like JSP, ASP etc.

3. PHP is cross platform, hence you can easily develop and move/deploy your PHP code/project to

almost all the major operating systems like Windows, Linux, Mac OSX etc.

4. All the popular web hosting services support PHP. Also the web hosting plans for PHP are

generally the amongst the cheapest plans because of its popularity.

5. Popular Content Management Systems like Joomla, Drupal etc. are developed in PHP and if you

want to start your own website like Study tonight, you can easily do that with PHP.

6. With PHP, you can create static and dynamic webpages, perform file handling operations, send

emails, access and modify browser cookies, and almost everything else that you might want to

implement in your web project.

7. PHP is fast as compared to other scripting languages like JSP and ASP.
8. PHP has in-built support for MySQL, which is one of the most widely used Database management

system.

These are some of the main features of PHP, while as you will learn the language you will realize that apart
from these features,

Uses of PHP
To further fortify your trust in PHP, here are a few applications of this amazing scripting language:

1. It can be used to create Web applications like Social Networks(Facebook, Digg),

Blogs(Wordpress, Joomla), eCommerce websites(OpenCart, Magento etc.) etc.

2. Command Line Scripting. You can write PHP scripts to perform different operations on any

machine, all you need is a PHP parser for this.

3. Create Facebook applications and easily integrate Facebook plugins in your website, using

Facebook's PHP SDK.

4. Sending Emails or building email applications because PHP provides with a robust email sending

function.

5. Wordpress is one of the most used blogging (CMS) platform in the World, and if you know PHP,

you can try a hand in Wordpress plugin development.

PHP started out as a small open source project that evolved as more and more people found out how
useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.
• PHP is a recursive acronym for "PHP: Hypertext Preprocessor".

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

• It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle,


Sybase, Informix, and Microsoft SQL Server.

• PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the
Unix side. The MySQL server, once started, executes even very complex queries with huge result
sets in record-setting time.

• PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added
support for Java and distributed object architectures (COM and CORBA), making n-tier
development a possibility for the first time.

• PHP is forgiving: PHP language tries to be as forgiving as possible.

• PHP Syntax is C-Like.

Common Uses of PHP


PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
The other uses of PHP are:

• PHP can handle forms, i.e. gather data from files, save data to a file, thru email you can send data,
return data to the user.

• You add, delete, modify elements within your database thru PHP.

• Access cookies variables and set cookies.

• Using PHP, you can restrict users to access some pages of your website.

• It can encrypt data.

Characteristics of PHP
Five important characteristics make PHP's practical nature possible:
• Simplicity

• Efficiency

• Security

• Flexibility

• Familiarity
PHP in the Web Environment

PHP programs are written using a text editor, such as Notepad, Simple Text, or vi, just like
HTML pages. However, unlike HTML, PHP files end with a .php extension. This extension
signifies to the server that it needs to parse the PHP code before sending the resulting HTML
code to the viewer’s web browser. In PHP, on the fly method is adopted to publish the
document. Hence, the PHP developer can generate not only web pages, but also other web
embedding documents like PDF, PNG, GIF, etc. The PHP web environment is usually set
with AMP (Apache, MySQL, and PHP/Perl/Python), which are linked together.

How PHP Fits with HTML


PHP not only allows HTML pages to be created on the fly, but it is invisible to your web site
visitors. The only thing they see when they view the source of your code is the resulting
HTML output. In this respect, PHP gives you a bit more security by hiding your
programming logic.

HTML can also be written inside the PHP code of your page, which allows you to format text
while keeping blocks of code together. This will also help you write organized, efficient
code, and the browser (and, more importantly, the person viewing the site) won’t know the
difference.

PHP can also be written as a standalone program with no HTML at all. This is helpful for
storing your connection variables, redirecting your visitors to another page of your site, or
performing other functions.

How PHP Fits with MySQL


MySQL easily fits into server-side programming languages, through a domain. Before your
MySQL functions will be recognizable, make sure to enable MySQL in your php.ini file.
You can use MySQL commands within PHP code almost as seamlessly as you do with
HTML. Numerous PHP functions work specifically with MySQL to make your life easier.

Some of the more commonly used functions are:


 mysql_connect([$host[, $username[, $password]]]): Connects to the MySQL
server and returns a resource which is used to reference the connection.
 mysql_select_db($database[, $resource]): Equivalent to the MySQL command
USE and sets the active database.
 mysql_query($query[, $resource]): Used to send any MySQL command to the
database server. In the case of SELECT queries, a reference to the result set will be
returned.
 mysql_fetch_array($result): Return a row of data from the query’s result set as an
associative array, numeric array or both.

1
PHP in the Web Environment

 mysql_fetch_assoc($result): Return a row of data from the query’s result set as an


associative array.
 mysql_error([$resource]): Shows the error message generated by the previous query.

Connecting to the MySQL Server


Before you can do anything with MySQL, you must first connect to the MySQL server using
your specific connection values. Connection variables consist of the following parameters:

o Hostname: In our case, this is localhost because everything has been installed locally.
You will need to change this to whatever host is acting as your MySQL server, if MySQL
is not on the same server.
o Username and password: This is to authenticate securely over server end.

You issue this connection command with the PHP function called mysql_connect(). As
with all of your PHP/MySQL statements, you can either put the information into variables or
leave it as text in your MySQL query.

Here’s how you would do it with variables:


$host = ‘localhost’;
$user = ‘user_name’;
$pass = ‘password’;
$db = mysql_connect($host, $user, $pass);

The following statement has the same effect:


$db = mysql_connect(‘localhost’, ‘user_name’, ‘password’);

For the most part, your specific needs and the way you are designing your table will dictate
what piece of code you use. Most people use the first method for security ‘sake and put the
variables in a different file. Then they include them wherever they need to make a connection
to the database.

Web Interface with Apache, MySQL, and PHP


phpMyAdmin is a tool written in PHP intended to handle the administration of MySQL over
the Web. Currently it can create and drop databases, create/drop/alter tables, delete/edit/add
fields, execute any SQL statement, manage keys on fields.

Features provided by the program include:

1. Web interface
2. MySQL database management
3. Import data from CSV and SQL
4. Export data to various formats: CSV, SQL, XML, PDF (via the TCPDF library), ISO/IEC
26300 - OpenDocument Text and Spreadsheet, Word, Excel, LaTeX and others

2
PHP in the Web Environment

5. Administering multiple servers


6. Creating PDF graphics of the database layout
7. Creating complex queries using Query-by-example (QBE)
8. Searching globally in a database or a subset of it
9. Transforming stored data into any format using a set of predefined functions, like
displaying BLOB-data as image or download-link
10. Active query monitor (Processes)

AMP Bundles
There are many AMP bundles such as LAMP, XAMP, PHPTriad, WAMP, MAMP, FoxServ,
etc. consists of AMP build in itself. So, there is no need to configure AMPs manually. Some
of the AMPs which are available for various operating systems are LAMP (for Linux);
WAMP (for Windows); MAMP (for Macintosh); SAMP (for Solaris); and FAMP (for
FreeBSD).

3
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. Download
Apache for free here − https://httpd.apache.org/download.cgi
• Database − PHP will work with virtually all database software, including Oracle and Sybase but
most commonly used is freely available MySQL database. Download MySQL for free here −
https://www.mysql.com/downloads/
• 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. This tutorial will guide you how to install
PHP parser on your computer.

Environment setup For PHP project


Tools Needed

1. Web Server: (XAMPP server/ WAMP server)


2. Editor : (Notepad, Notepad++ or Dreamweaver editor)
3. Database : ( MySQL, Oracle)

In this tutorial we are using XAMPP server, notepad++ and MySQL


database. Now I am showing you how to install and start XAMPP server

Step1:
Download the XAMPP server click on above link.

Step 2:
Install the exe file which is downloaded.

Step 3:
Now to go C drive (or where you install the server) and open XAMPP folder
and find XAMPP control file and double click on that.
Step 4:
Click on start button on Apache and MYSQL both.

Step 5:
Now open web browser like Google chrome and type "localhost" and check
XAMPP server welcome page is appeared or not. If appeared then our server
is started.

Now create your own folder under htdocs , Example name it as test or what
ever.

Step 6:
Now write your first PHP code and name it as “index.php” and save it under
test folder.

<!DOCTYPE html>
<html>
<body>

<?php
echo "Hello World!";
?>

</body>
</html>

Step 7:
Now open your web browser and type localhost/test/index.php.
Applications Development and
Emerging Technologies
MODULE 1
Web Architecture, Tools, and
Introduction to PHP
An introduction to PHP web programming

Objectives
• To understand the history of PHP
• To know the advantages of PHP as a server side scripting
language
• To know what are the software's needed in developing web
application using PHP.
• To run PHP application on a web browser.
• To know the basic syntax of PHP for outputting to browser.
• To know the datatypes that are available on PHP and on
how these datatypes are being used.
• To know some predefined type function for data
manipulation.
• To know the proper casting values of a given variables.
An introduction to PHP web programming

HISTORY
• 1994/1995 (PHP 1.0)
• Developed by Rasmus Lerdorf
• To know how many visitors were reading his online
resume based on PERL/CGI script
• Personal Home Page (PHP)
• 1997 (PHP 2.0)
• PHP is based on C rather than PERL
• Personal Home Page/Form Interpreter
• 1998 (PHP 3.0)
• 50,000 users were using PHP to enhance their Web pages
• Developers joined Lerdorf
• 1999 (PHP 4.0)
• With core developers Zeev Suraski and Andi Gutmans
• PHP makes the most popular scripting language with
morethan one million user base by Netcraff
An introduction to PHP web programming

HISTORY (Con’t.)
• Hundreds of functions being added
• Dubbed the Zend scripting engine
• May 22, 2000 (PHP 4.0)
• PHP: Hypertext Preprocessor (recursive acronym)
• 3.6 million domain PHP installed
• Enterprise development
• Improved resource handling (scalability)
• Object-oriented support (Classes and Objects)
• Native session-handling support (session)
• Encryption (encryption algoritms)
• ISAPI Support (for IIS)
• Native COM/DOM (Windows applications)
• Native Java Support: (binding to java objects)
• PERL Compatible Regular Expressions
• New features, power, and scalabilty
An introduction to PHP web programming

HISTORY (Con’t.)
• PHP 5 (July 13, 2004)
• Vastly improved object-oriented capabilities: OOP
improvement
• Try/catch exception handling
• Improved XML and Web Services support (Simple XML
Support, using SOAP)
• Native support for SQLite
• Installed on 19 million domains
• 54 percent on all Apache module
• PHP 6 (2007 not yet released)
• Unicode Support (Native Unicode support for multilingual
applications
• Security improvements
• New language features and constructs (64-bit type integer,
foreach looping)
http://en.wikipedia.org/wiki/PHP
An introduction to PHP web programming

HISTORY (Con’t.)
PHP Key categories
• Practicality
• PHP is a loosely type language (no explicitly create,
typecast, or destroy a variable)
• Power
• More libraries and thousands of functions
• 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
• Price
• Free of charge
An introduction to PHP web programming

Web Architecture Page Request

html
Internet

scripts

database
An introduction to PHP web programming

Software Requirements
Operating System: Linux
An introduction to PHP web programming

Software Requirements
Operating System: Windows
An introduction to PHP web programming

Software Requirements
Operating System: Mac
Cheetah

Panther

Puma

Jaguar Tiger

Leopard Snow leopard


Lion
An introduction to PHP web programming

Software Requirements
Internet browser

Opera
Mozilla
Internet Safari
Google Firefox
Chrome Explorer

Deepnet
Rockmelt Explorer Avant
Maxthon Browser
SeaMonkey
An introduction to PHP web programming

Software Requirements
Web Server

Apache

Google Web Server (GWS)


An introduction to PHP web programming

Software Requirements
Server side scripting language

Java Active Server Pages


(JavaServer Pages: JSP) (ASP)
Ruby (Ruby on Rails)

Python
PERL CGI
(Django)
An introduction to PHP web programming

Software Requirements
Database

Oracle
An introduction to PHP web programming

Software Requirements
Code editor

Bluefish

Dreamweaver
An introduction to PHP web programming

Software Requirements
Other
An introduction to PHP web programming

Software Requirements
Other
Operating System
Windows 7 / Windows 8

Web Server
Apache

Language Script XAMPP / WAMP


PHP

Database
MySQL

Code Editor
Notepad++ / Eclipse / Netbeans / Dreamweaver
An introduction to PHP web programming

Running PHP Scripts


XAMPP
1. Download XAMPP
http://www.apachefriends.org/en/xampp.html
2. Install XAMPP
3. Run XAMPP Control
• c:\xampp (by default)
• Start the Apache and MySQL(for database) Service
4. Create a folder under c:\xampp\htdocs\ (by default)
Note: All php files must be save on that folder
5. Test PHP script sample file.
5. Open some internet browser
6. Type localhost/[folder name] (by default)
7. Select file from the directory list (if there’s
An introduction to PHP web programming

Running PHP Scripts


All php scripts must enclosed
with
<?php … ?> (standard tag)
<? … ?> (short open tag)
<script language=“php”>
….
</script> (script tag)
<% … %> (asp style tag)

OUTPUT:
An introduction to PHP web programming

Running PHP Scripts


opening tag for php script Statement terminator

echo – is a language construct


that use to output one or more
closing tag for php script string.

print – is a function also use for


outputting strings.
An introduction to PHP web programming

PHP Comments
An introduction to PHP web programming

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
An introduction to PHP web programming

Using ‘ ‘ (pair single quote), “ “ (pair double


quotes) and . (dot character) PHP
1. Strings inside the ‘ and ‘ are interpreted as literal strings
1. 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
1. $ inside “ and “ are interpreted as variables thus it will
display the value rather than the string that starts with $.
2. 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”
An introduction to PHP web programming

Using ‘ ‘ (pair single quote), “ “ (pair double


quotes) and . (dot character) PHP
Example: Output:
An introduction to PHP web programming

Outputting Data
echo syntax
void echo(string argument1[,…string argumentN]

print syntax
int print(argument)

printf syntax
boolean printf(string format [, mixed args])
An introduction to PHP web programming

Outputting Data
Example:

Output:
An introduction to PHP web programming

Commonly Used Type Specifiers for printf function

Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.100
An introduction to PHP web programming

PHP Datatypes

Datatypes
is the generic name assigned to any data sharing a common set of
characteristics.

Scalar Datatypes
Capable of containing a single item of information
(Boolean, Integer, Float (float, double or real numbers), String)

Compound Datatypes
allow for multiple items of the same type to be aggregated under a
single representative entity.
(Array and Objects)
An introduction to PHP web programming

Scalar Datatypes
Eample:
An introduction to PHP web programming

Compound Datatypes

Example:

Output:

Output:
An introduction to PHP web programming

PHP Type Casting Operators

Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.105
An introduction to PHP web programming

PHP Type Casting Operators

Example: Output:
An introduction to PHP web programming

PHP Type Juggling

Example: Output:
An introduction to PHP web programming

Predefined Type Functions


gettype() function
returns the type of the variable. Possible return values are
integer, double, boolean, string, array, object, resource,
unknown types.
prototype:
string gettype(mixed var)

settype() function
converts a given variable to a specific type.
prototype:
boolean settype(mixed var, string type)

Predefined Type Identifier Functions


is_array(), is_bool(), is_float(), is_integer(),
is_null(), is_numeric(), is_object(), is_resource(),
is_scalar(), and is_string()
An introduction to PHP web programming

Type Functions

Example:

Output:
An introduction to PHP web programming

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.
An introduction to PHP web programming

Summary (Continue)
• 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.
• 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.
HISTORY

• 1994/1995 (PHP 1.0)


o Developed by Rasmus Lerdorf
o To know how many visitors were reading 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 morethan 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 algoritms
▪ 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 scalabilty
• 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

• Practicality
o PHP is a loosely type language (no explicitly create, typecast, or destroy a variable)
• Power
o More libraries and thousands of functions
• Possibility
o 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
• Price
o Free of charge

Software Requirements Operating System: Linux

• Ubuntu
• Debian
• Linux
• Suse
• Fedora
• Kubuntu
• Centos

Software Requirements Operating System: Windows

• Windows XP
• Windows Me
• Windows Server 2008
• Windows 7
• Windows 8
• Windows 2000

Software Requirements Operating System: Mac

• Cheetah
• Puma
• Jaguar
• Leopard
• Snow Leopard
• Panther
• Tiger
• Lion

Software Requirements Internet browser

• Google Chrome
• Mozilla Firefox
• Internet Explorer
• Opera
• Safari
• Maxthon
• Rockmelt
• SeaMonkey
• Deepnet Explorer
• Avant Browser

Software Requirements Web Server

• Apache
• IIS7 (Internet Information Services)
• NGINX
• Google Web Server

Software Requirements Server side scripting language

• Java
• Python
• Ruby
• ASP (Active Server Pages)
• PERL CGI
• PHP

Software Requirements Database

• IBMDB2
• SQLite
• Postgre
• MySQL
• SQL Server
• Oracle

Software Requirements Code editor

• Aptana
• Eclipse
• Bluefish
• Dreamweaver
• Notepad++
• Netbeans

Software Requirements Other

• HTML
• CCS
• AJAX
• JavaScript
• JQuery

Running PHP Scripts XAMPP

1. Download XAMPP http://www.apachefriends.org/en/xampp.html


2. Install XAMPP
3. Run XAMPP Control
a. c: xampp (by default)
b. Start the Apache and MySQL (for database) Service
4. Create a folder under c:\xampp\htdocs\(by default)
a. Note: All php files must be save on that folder
5. Test PHP script sample file.
a. Open some internet browser
b. Type localhost /[folder name] (by default)
c. Select file from the directory list (if there’s

All php scripts must enclosed

with <? php … ?> (standard tag

<? … ?> (short open <script language=”php”

….

</script>(script tag)

<% … %>(asp style tag)

echo–is a language construct that use to output one or more string.

print–is a function also use for outputting strings.

PHP comments

//single line comment

/* Multiline comment */

PHP Variables
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

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

1.$ inside “ and “ are interpreted as variables thus it will display the value rather than the string that
starts with $.

2.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”

Outputting Data

echo syntax

void echo(string argument1[,…string argumentN

print syntax

int print(argument)

printf syntax
Boolean printf (string format [, mixed args])

PHP Datatypes

Datatypes

is the generic name assigned to any data sharing a common set of characteristics.

Scalar Datatypes

Capable of containing a single item of information

(Boolean, Integer, Float (float, double or real numbers), String)

Compound Datatypes

allow for multiple items of the same type to be aggregated under a single representative entity.

(Array and Objects)

PHP Type Casting Operators

Predefined Type Functions

gettype()function

returns the type of the variable. Possible return values are integer, double, boolean, string, array, object,
resource, unknown types.

prototype:

string gettype(mixed var)

settype()function

converts a given variable to a specific type.

prototype:

booleansettype(mixed var, string type)

is_array(), is_bool(), is_float(), is_integer(), is_null(), is_numeric(), is_object(), is_resource(), is_scalar(),


and is_string()
Applications Development and
Emerging Technologies
MODULE 1 SUBTOPIC 2
PHP Environment
An introduction to PHP web programming

Objectives
• To run PHP application on a web browser.
• To know the basic syntax of PHP for outputting to browser.
• To know the datatypes that are available on PHP and on
how these datatypes are being used.
• To know some predefined type function for data
manipulation.
• To know the proper casting values of a given variables.
An introduction to PHP web programming

Running PHP Scripts


XAMPP
1. Download XAMPP
http://www.apachefriends.org/en/xampp.html
2. Install XAMPP
3. Run XAMPP Control
• c:\xampp (by default)
• Start the Apache and MySQL(for database) Service
4. Create a folder under c:\xampp\htdocs\ (by default)
Note: All php files must be save on that folder
5. Test PHP script sample file.
5. Open some internet browser
6. Type localhost/[folder name] (by default)
7. Select file from the directory list (if there’s
An introduction to PHP web programming

Running PHP Scripts


All php scripts must enclosed
with
<?php … ?> (standard tag)
<? … ?> (short open tag)
<script language=“php”>
….
</script> (script tag)
<% … %> (asp style tag)

OUTPUT:
An introduction to PHP web programming

Running PHP Scripts


opening tag for php script Statement terminator

echo – is a language construct


that use to output one or more
closing tag for php script string.

print – is a function also use for


outputting strings.
An introduction to PHP web programming

PHP Comments
An introduction to PHP web programming

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
An introduction to PHP web programming

Using ‘ ‘ (pair single quote), “ “ (pair double


quotes) and . (dot character) PHP
1. Strings inside the ‘ and ‘ are interpreted as literal strings
1. 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
1. $ inside “ and “ are interpreted as variables thus it will
display the value rather than the string that starts with $.
2. 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”
An introduction to PHP web programming

Using ‘ ‘ (pair single quote), “ “ (pair double


quotes) and . (dot character) PHP
Example: Output:
An introduction to PHP web programming

Outputting Data
echo syntax
void echo(string argument1[,…string argumentN])

print syntax
int print(argument)

printf syntax
boolean printf(string format [, mixed args])
An introduction to PHP web programming

Outputting Data
Example:

Output:
An introduction to PHP web programming

Commonly Used Type Specifiers for printf function

Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.100
An introduction to PHP web programming

PHP Datatypes

Datatypes
is the generic name assigned to any data sharing a common set of
characteristics.

Scalar Datatypes
Capable of containing a single item of information
(Boolean, Integer, Float (float, double or real numbers), String)

Compound Datatypes
allow for multiple items of the same type to be aggregated under a
single representative entity.
(Array and Objects)
An introduction to PHP web programming

Scalar Datatypes
Example:
An introduction to PHP web programming

Compound Datatypes

Example:

Output:

Output:
An introduction to PHP web programming

PHP Type Casting Operators

Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.105
An introduction to PHP web programming

PHP Type Casting Operators

Example: Output:
An introduction to PHP web programming

PHP Type Juggling

Example: Output:
An introduction to PHP web programming

Predefined Type Functions


gettype() function
returns the type of the variable. Possible return values are
integer, double, boolean, string, array, object, resource,
unknown types.
prototype:
string gettype(mixed var)

settype() function
converts a given variable to a specific type.
prototype:
boolean settype(mixed var, string type)

Predefined Type Identifier Functions


is_array(), is_bool(), is_float(), is_integer(),
is_null(), is_numeric(), is_object(), is_resource(),
is_scalar(), and is_string()
An introduction to PHP web programming

Type Functions

Example:

Output:
An introduction to PHP web programming

Summary
• 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.
• String inside a pair of single quote interpreted as literal except for
the single quote itself.
An introduction to PHP web programming

Summary (Continue)
• 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.

You might also like