Vous êtes sur la page 1sur 16

Introduction to PHP

Introduction to PHP

A. Chaubal

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

WEB
SERVER

Introduction to PHP

Gets Page
<HTML>
<?php PHP code ?>
</HTML>

HTTP Request
(url)

Interprets the PHP code

Server response

CLIENT

Hello

<HTML>
<B>Hello</B>
</HTML>

Browser creates
the web page

A. Chaubal

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

Introduction to PHP

PHP Language features


PHP language features such as control
structures, operators, variable types, function
declaration, class/object declaration are
almost similar to any compiled or interpreted
language such as C or C++.

A. Chaubal

Introduction to PHP

PHP Data Type


Three basic data types
Integer
Double
String

More data types


Array
Object

PHP is an untyped language


variables type can change on the fly.
A. Chaubal

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

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

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;

variables data type is changed by the value


that is assigned to the variable.
Type casting allows to change the data type
explicitly.
A. Chaubal

Introduction to PHP

PHP Variables (cont.)


Rich set of functions for working with
variable.
For e.g
gettype, settype, isset, unset, is_int, intval etc etc

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

PHP Statements

Introduction to PHP

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

Vous aimerez peut-être aussi