Introduction to PHP
Amrish V. Chaubal
Data & Knowledge Engineering Division
amrish@ncst.ernet.in
A. Chaubal 1
Introduction to PHP
Introduction to PHP
• PHP Hypertext Preprocessor.
– Other Names : Personal Home Page, Professional Home
Page
• Is a server side scripting language.
– Capable of generating the HTML pages
• HTML generates the web page with the static text
and images.
• However the need evolved for dynamic web based
application, mostly involving database usage.
A. Chaubal 2
WEB Introduction to PHP
SERVER
Gets Page
<HTML>
<?php PHP code ?>
HTTP Request
</HTML>
(url)
A. Chaubal 3
Introduction to PHP
Why PHP?
• ..there are no. of server side scripting available like
ASP, SSJS, JSP…..
• PHP involves
– simplicity in scripting (..generally using the database)
– platform independence.
• PHP is
– primarily designed for web applications
– well optimized for the response times needed for web
applications
• Is an open source.
A. Chaubal 4
Introduction to PHP
A. Chaubal 5
Introduction to PHP
A. Chaubal 6
Introduction to PHP
PHP Block
• PHP code block is embedded within the <?php
and ?> tags.
• When the server encounters the PHP tags it
switches from the HTML to PHP mode.
• There are four different ways to embed the PHP
code
– <?php echo(“Some PHP code”); ?>
– <? echo(“Some PHP code”); ?>
– <SCRIPT Language=‘php’> echo(“Some PHP code”); </SCRIPT>
– <% echo(“Some PHP code”); %>
A. Chaubal 7
Introduction to PHP
PHP Constants
• ..values that never changes
• Constants are defined in PHP by using the
define() function.
– For e.g.
define(“NCST”, “National Centre for Software Technology”)
• defined() function says whether the constant exists
or not.
A. Chaubal 8
Introduction to PHP
PHP Variables
• The variables in PHP are declared by
appending the $ sign to the variable name.
– For e.g
$company = “NCST”;
$sum = 10.0;
• variable’s data type is changed by the value
that is assigned to the variable.
• Type casting allows to change the data type
explicitly.
A. Chaubal 9
Introduction to PHP
A. Chaubal 10
Introduction to PHP
PHP Operators
• All the operators such as arithmetic,
assignment, Comparison, and logical operators
are similar to the operators in C and C++.
• In PHP the string concatenation operator is
denoted by ‘.’.
– For e.g.
• $name = “My name is”.$myname;
A. Chaubal 11
Introduction to PHP
PHP Statements
• IF statement
if (<condition>) {
//php code goes here
}
else {
//php code goes here
}
• Alternative Syntax
if(<condition>) :
//html code goes here
else :
//html code goes here
endif;
A. Chaubal 12
Introduction to PHP
PHP Statements (cont.)
• For loop
for($i=0;$i < 10;$++i) {
echo(“the value is :”. $i);
}
– Alternative Syntax
for($i=0;$i < 10;$++i) :
// html code goes here
endfor;
• While loop
• Do-While loop
A. Chaubal 13
Introduction to PHP
Functions
• Function declaration in PHP
function my_func(<parameters>) {
//do something in the function
}
– for e.g.
function sayHello() {
echo(“<B>hello amrish<B><BR>”);
}
A. Chaubal 14
Introduction to PHP
Functions (cont.)
• Assigning functions to the variables
– for e.g
• $hello = “my_func”;
• to invoke the function my_func() through the variable
$hello( );
• When an argument is to be passed by reference,
an ampersand (&) is placed before the parameter
name
– for e.g.
my_func(&$my_refvar);
A. Chaubal 15
Introduction to PHP
Arrays
• ..contains value set
• each element has a value, data stored in the
element.
• And has a key by which the element can be
referred to.
A. Chaubal 16
Introduction to PHP
Initializing Arrays
• No of ways to initialize the array.
– For e.g.
• $ncststaff[] = “amrish”;
$ncststaff[] = “murali”;
$ncststaff[] = “narayan”;
• $ncststaff[123] = “amrish”;
$ncststaff[122] = “murali”;
$ncststaff[121] = “narayan”;
• $ncststaff = array (“amrish”, “murali”, “narayan”);
– to change the indices of the array use => operator.
A. Chaubal 17
Introduction to PHP
A. Chaubal 18
Introduction to PHP
A. Chaubal 19
Introduction to PHP
Arrays (cont.)
• $ncststaff = array (“dake” => array(“amrish”, “lakshana”, “venkat”),
“spc” => array(“narayan”, “murali”,“prasad”));
– creates a two dimensional array.
• Sorting Functions
– sort() : sorts the elements in the numeric and
alphabetical order.
– rsort() : sorts the elements in the reverse order.
– asort() : sorts the elements in the array without
changing the indices.
– ksort() : sorts the arrays by key.
A. Chaubal 20
Introduction to PHP
Classes
• Class is a template of an object and includes
the properties and methods that describe an
object and behavior.
• Class in PHP is defined using class statement.
A. Chaubal 21
Introduction to PHP
Classes (cont.)
• For e.g
<?
class company {
// define the properties
var $companyname;
// define the methods
function company($cname) {
$this->companyname = $cname;
}
function getnames($idcode) {
//return the name of the employee for the required idcode
}
}
?>
A. Chaubal 22
Introduction to PHP
PHP Richness
• PHP comes with myriad of options i.e.
supports several APIs and interfaces to other
programming tools such as
– Database connectivity.
– LDAP
– XML
– Mail protocols such as IMAP, SMTP
– Image functions
etc….
A. Chaubal 23
Introduction to PHP
A. Chaubal 24
Introduction to PHP
A. Chaubal 25
Introduction to PHP
A. Chaubal 26
Introduction to PHP
Mailing functions
• Sending E-Mails
– Mail()
• Used to send simple text messages.
• Depends on the local mail delivery system.
– Using SMTP
• Accepts the e-mail for every recipient and goes through trouble
of delivering the e-mails.
• Receiving E-Mails
– PHP works out well with the IMAP protocol.
– Rich set of support functions
• Imap_open, impa_delete, imap_close, imap_mail_copy,
imap_mail_move etc.
A. Chaubal 27
Introduction to PHP
PHP-Database Connectivity
• Supports APIs for accessing large number of
databases.
• ODBC is a standard API for accessing a
database that has PHP support.
A. Chaubal 28
Introduction to PHP
PHP-Database Connectivity
• Some of Oracle 8.x functions.
– OCILogon
– OCILogoff
– OCIParse
– OCIFetch; OCIFetchInto; OCIFetchStatement
– OCIExecute
– OCIFreeStatement
A. Chaubal 29
Introduction to PHP
A. Chaubal 30
Introduction to PHP
A. Chaubal 31
Introduction to PHP
References
• Professional PHP programming
– By Jesus Castagnetto, Chris Schollo et al
• www.phpbuilder.com
• www.php.net
A. Chaubal 32
Introduction to PHP
THANK YOU
A. Chaubal 33