Вы находитесь на странице: 1из 52

PHP Basics

By: Ajay Rawat

University of Petroleum & Energy Studies


Email: arawat@ddn.upes.ac.in Room: CIT Contact: 9045957565
PHP Basics Ajay Rawat : arawat@ddn.upes.ac.in

Embedding PHP in HTML

PHP Basics

Ajay Rawat : arawat@ddn.upes.ac.in

Embedding PHP in HTML


We can embed PHP code alongside HTML.
For the code to do anything, the page must be

passed to the PHP engine for interpretation.


Web server doesnt just pass every page

rather, it passes only those pages identified by a specific file extension (.php)
Engine needs some means to immediately

determine which areas of the page are PHPenabled.


This is accomplished by delimiting the PHP

code.
PHP Basics 1.3 Ajay Rawat : arawat@ddn.upes.ac.in

Embedding PHP in HTML


There are four delimitation variants.
Default

Syntax

Short-Tags Script ASP

Style

PHP Basics

1.4

Ajay Rawat : arawat@ddn.upes.ac.in

Default Syntax
The default delimiter syntax opens with <?php and

concludes with ?>

PHP Basics

1.5

Ajay Rawat : arawat@ddn.upes.ac.in

Short-Tags
<? print "This is another PHP example."; ?>

To use this feature, we need to enable PHPs

short_open_tag directive.
Although short-tag delimiters are convenient,

keep in mind that they clash with XML, and thus XHTML, syntax.
Therefore, for conformance reasons we

shouldnt use short-tag syntax.


<?="This is another PHP example.";?> known

as short-circuit syntax.
PHP Basics 1.6 Ajay Rawat : arawat@ddn.upes.ac.in

Script
Certain Editor like Microsofts FrontPage editor

have problems dealing with escape syntax.


Therefore, support for another mainstream

delimiter variant, <script>, is offered.

PHP Basics

1.7

Ajay Rawat : arawat@ddn.upes.ac.in

ASP Style
<% print "This is another PHP example."; %>
ASP-style syntax was removed as of PHP 5.3. Keep in mind that just we can do something

doesn't means we should.


ASP style and script delimiting variants are

rarely used and should be avoided.

PHP Basics

1.8

Ajay Rawat : arawat@ddn.upes.ac.in

Embedding Multiple Code Blocks


Any variables declared in a prior code block

are remembered for later blocks.

PHP Basics

1.9

Ajay Rawat : arawat@ddn.upes.ac.in

Comments

PHP Basics

Ajay Rawat : arawat@ddn.upes.ac.in

Comments
Comments are user for our own benefit or for

that of a programmer later tasked with maintaining the code.


Single-Line C++ Syntax
Shell Syntax Multiple-Line C Syntax

PHP Basics

1.11

Ajay Rawat : arawat@ddn.upes.ac.in

Single-Line C++ Syntax


PHP supports C++ single-line comment

syntax, which is prefaced with a double slash (//)


There is no need to delimit the comments

conclusion.

PHP Basics

1.12

Ajay Rawat : arawat@ddn.upes.ac.in

Shell Syntax
PHP also supports an alternative to the C++-

style single-line syntax, known as shell syntax.


It is prefaced with a hash mark (#).

PHP Basics

1.13

Ajay Rawat : arawat@ddn.upes.ac.in

Multiple-Line C Syntax
It support more verbose functional description

or other explanatory notes within code which needs numerous lines.

PHP Basics

1.14

Ajay Rawat : arawat@ddn.upes.ac.in

HTML Comment
Keep in mind that if we want to place a

comment in HTML we need to use the open comment <!-- and close comment --> tags.

PHP Basics

1.15

Ajay Rawat : arawat@ddn.upes.ac.in

Outputting Data to the Browser

PHP Basics

Ajay Rawat : arawat@ddn.upes.ac.in

print() statement
The print() statement outputs data passed to it

to the browser.
int print(argument).

PHP Basics

1.17

Ajay Rawat : arawat@ddn.upes.ac.in

print() statement
print() is not actually a real function (it is a

language construct) so we are not required to use parentheses with its argument list.
The print() statement return value is

misleading it will always return 1 regard less of the outcome.

PHP Basics

1.18

Ajay Rawat : arawat@ddn.upes.ac.in

echo() statement
void echo ( string $arg1 [, string $... ] )
Outputs all parameters. echo() is not a function (it is a language

construct.
If we want to pass more than one parameter to

echo(), the parameters must not be enclosed within parentheses.


No value is returned.

PHP Basics

1.19

Ajay Rawat : arawat@ddn.upes.ac.in

echo() statement

PHP Basics

1.20

Ajay Rawat : arawat@ddn.upes.ac.in

printf() statement
printf() statement is used to when to blend of

static text and dynamic information stored within on e or several variable.


int printf ( string $format [, mixed $args])
Returns the length of the outputted string. printf( Result: %d percentage., 90);

PHP Basics

1.21

Ajay Rawat : arawat@ddn.upes.ac.in

sprintf() statement
The sprintf() statement is functionally identical

to printf() except that the output is assigned to a string rather than rendered to the browser.
string sprintf(string format [, mixed arguments])
An example follows: $cost = sprintf("$%.2f", 43.2); // $cost = $43.20

PHP Basics

1.22

Ajay Rawat : arawat@ddn.upes.ac.in

Supported Data types

PHP Basics

Ajay Rawat : arawat@ddn.upes.ac.in

Data types
A data type is the generic name assigned to

any data sharing a common set of characteristics.


Common data types include Boolean, integer,

float, string, and array.


Scalar Data types
Boolean,

integer, float, string

Compound Data types

PHP Basics

1.24

Ajay Rawat : arawat@ddn.upes.ac.in

Boolean
The Boolean datatype is named after George

Boole (18151864).
A Boolean variable represents truth,

supporting only two values: TRUE and FALSE (case insensitive).


Alternatively, we can use zero to represent

FALSE, and any nonzero value to represent TRUE.

PHP Basics

1.25

Ajay Rawat : arawat@ddn.upes.ac.in

Integer
An integer is representative of any whole

number or, a number that does not contain fractional parts.


PHP supports integer values represented in

base 10 (decimal), base 8 (octal), and base 16 (hexadecimal) numbering systems.


The maximum supported integer size is

platform-dependent.
PHP 5(earlier) introduced a 32-bit integer

value.
PHP 6 introduced a 64-bit integer value,
PHP Basics 1.26 Ajay Rawat : arawat@ddn.upes.ac.in

Float
Floating-point numbers, also referred as floats,

doubles, or real numbers.


It allow to specify numbers that contain

fractional parts.
Floats are used to represent monetary values,

weights, distances.
4.5678
4.0 8.7e4 1.23E+11
PHP Basics 1.27 Ajay Rawat : arawat@ddn.upes.ac.in

String
A string is a sequence of characters treated as

a contiguous group.
Strings are delimited by single or double

quotes.
"PHP is a great language "123$%^789.

PHP Basics

1.28

Ajay Rawat : arawat@ddn.upes.ac.in

Array
It is a aggregate a series of similar items

together, arranging and referencing them in some specific way.


It is formally defined as an indexed collection

of data values.
Each member of the array index (also known

as the key) references a corresponding value.


$state[0] $state[1] $state[2]
PHP Basics

= "Alabama"; = "Alaska"; = "Arizona";


1.29 Ajay Rawat : arawat@ddn.upes.ac.in

Object
The object is a central concept of the object-

oriented programming paradigm.


This declaration of an objects characteristics

and behavior takes place within something called a class.

PHP Basics

1.30

Ajay Rawat : arawat@ddn.upes.ac.in

Type Cast
Converting values from one datatype to

another is known as type casting.

PHP Basics

1.31

Ajay Rawat : arawat@ddn.upes.ac.in

Type-Related Functions
Retrieving Types
The

gettype() function returns the type of the variable specified by var.

In

total, eight possible return values are available: array, boolean, double, integer, object, resource, string, and unknown type. gettype (mixed var)

string

PHP Basics

1.32

Ajay Rawat : arawat@ddn.upes.ac.in

Type-Related Functions
Converting Types
The

settype() function converts a variable, specified by var, to the type specified by type. possible type values are available: array, boolean, float, integer, null, object, and string. the conversion is successful, TRUE is returned; otherwise, FALSE is returned.

Seven

If

boolean

settype(mixed var, string type)


1.33 Ajay Rawat : arawat@ddn.upes.ac.in

PHP Basics

Type Identifier Functions


A number of functions are available for

determining a variables type, including is_array(), is_bool(), is_float(), is_integer(), is_null(), is_numeric(), is_object(), is_resource(), is_scalar(), and is_string().
boolean is_name(mixed var)

PHP Basics

1.34

Ajay Rawat : arawat@ddn.upes.ac.in

Identifier
Identifier is a term applied to variables,

functions, and various other user definedobjects.


An identifier can consist of one or more

characters and must begin with a letter or _.


Identifiers can consist of only letters, numbers,

underscore characters

PHP Basics

1.35

Ajay Rawat : arawat@ddn.upes.ac.in

Variables
A variable is a symbol that can store different

values at different times.


A variable is a named memory location that

contains data and may be manipulated throughout the execution of the program.
Variable Declaration:
A variable

always begins with a dollar sign, $, which is then followed by the variable name.

$color,

$operating_system, $_some_variable, $model

PHP Basics

1.36

Ajay Rawat : arawat@ddn.upes.ac.in

Variables
Note that variables are case sensitive. $color, $Color, $COLOR

Value Assignment:
$color

= "red";

PHP Basics

1.37

Ajay Rawat : arawat@ddn.upes.ac.in

Variables
Reference Assignment:
We We

can create a variable that refers to the same content as another variable does. can assign variables by reference by appending an ampersand (&) to the equal sign. =& $value1; // $value1 and $value2 both equal "Hello

$value2

PHP Basics

1.38

Ajay Rawat : arawat@ddn.upes.ac.in

Variables Scope
We can declare variable anywhere in a PHP

script.
The location of the declaration influences the

realm in which a variable can be accessed.


This accessibility domain is known as its

scope.
PHP variables can be one of four scope types:
Local

variables parameters variables


1.39 Ajay Rawat : arawat@ddn.upes.ac.in

Function Global Static


PHP Basics

variables

Local Variable
A variable declared in a function is considered

local.
Means it can be referenced only in that

function.
Any assignment outside of that function will be

considered to be an entirely different variable.


When we exit the function of local variable that

variable and its corresponding value are destroyed.

PHP Basics

1.40

Ajay Rawat : arawat@ddn.upes.ac.in

Local Variable
They eliminate the possibility of unexpected

side effects, which can result from globally accessible variables that are modified, intentionally or not.

PHP Basics

1.41

Ajay Rawat : arawat@ddn.upes.ac.in

Function Variable
Function parameters are declared after the

function name and inside parentheses.

PHP Basics

1.42

Ajay Rawat : arawat@ddn.upes.ac.in

Global Variable
A global variable can be accessed in any part

of the program.
To modify it must be explicitly declared to be

global in the function in which it is to be modified.


This is accomplished by placing the keyword

GLOBAL in front of the variable that should be recognized as global.


Placing this keyword in front of an already

existing variable tells PHP to use the variable having that name.
PHP Basics 1.43 Ajay Rawat : arawat@ddn.upes.ac.in

Global Variable

PHP Basics

1.44

Ajay Rawat : arawat@ddn.upes.ac.in

Static Variables
A static variable does not lose its value when

the function exits.


It will hold the value if the function is called

again.
Static scoping is particularly useful for

recursive functions.

PHP Basics

1.45

Ajay Rawat : arawat@ddn.upes.ac.in

PHPs Superglobal Variables


PHP offers a number of predefined variables

that are accessible from anywhere and provide environment specific information.
With these variable we can get details about:
current
users local

user session

operating environment

operating environment

PHP creates some of the variables, while the

availability and value of other variables are specific to the O.S and Web server.
PHP Basics 1.46 Ajay Rawat : arawat@ddn.upes.ac.in

PHPs Superglobal Variables

PHP Basics

1.47

Ajay Rawat : arawat@ddn.upes.ac.in

Variable Variables
To use a variable whose content can be

treated dynamically as a variable in itself.


$recipe = pasta"; We can treat the value pasta as a variable by

placing a second dollar sign in front of the original variable name and again assigning another value:
$$recipe = "& meatballs";
This in effect assigns & meatballs to a variable

named pasta.
PHP Basics 1.48 Ajay Rawat : arawat@ddn.upes.ac.in

Variable Variables
The two snippets of code produce the same

result:
echo $recipe $pasta; echo $recipe ${$recipe};

The result of both is the string pasta &

meatballs.

PHP Basics

1.49

Ajay Rawat : arawat@ddn.upes.ac.in

Constants
A constant is a value that cannot be modified

throughout the execution of a program.


Once a constant has been defined, it cannot

be changed (or redefined) at any other point of the program.


Constants are defined using the define()

function.
define("PI", 3.141592);

PHP Basics

1.50

Ajay Rawat : arawat@ddn.upes.ac.in

Point to remember about Constants


Constant references are not prefaced with a

dollar sign.
We cant redefine or undefine the constant

once it has been defined (e.g., 2*PI).


If we need to produce a value based on the

constant, the value must be stored in another variable.


Finally, constants are global; they can be

referenced anywhere in the script.

PHP Basics

1.51

Ajay Rawat : arawat@ddn.upes.ac.in

End of Chapter

PHP Basics

Ajay Rawat : arawat@ddn.upes.ac.in