Вы находитесь на странице: 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

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

Embedding PHP in HTML

There are four delimitation variants.


Short-Tags Script ASP


PHP Basics


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

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

concludes with ?>

PHP Basics


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

<? 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

Certain Editor like Microsofts FrontPage editor

have problems dealing with escape syntax.

Therefore, support for another mainstream

delimiter variant, <script>, is offered.

PHP Basics


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


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


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


PHP Basics

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

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


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


PHP Basics


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


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


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


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


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


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

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


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

echo() statement

PHP Basics


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


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


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

integer, float, string

Compound Data types

PHP Basics


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

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


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

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

PHP 5(earlier) introduced a 32-bit integer

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

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.0 8.7e4 1.23E+11
PHP Basics 1.27 Ajay Rawat : arawat@ddn.upes.ac.in

A string is a sequence of characters treated as

a contiguous group.
Strings are delimited by single or double

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

PHP Basics


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

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

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


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

Type Cast
Converting values from one datatype to

another is known as type casting.

PHP Basics


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

Type-Related Functions
Retrieving Types

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


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


PHP Basics


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

Type-Related Functions
Converting Types

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.




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


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

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


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

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.


$operating_system, $_some_variable, $model

PHP Basics


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

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

Value Assignment:

= "red";

PHP Basics


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

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


PHP Basics


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

Variables Scope
We can declare variable anywhere in a PHP

The location of the declaration influences the

realm in which a variable can be accessed.

This accessibility domain is known as its

PHP variables can be one of four scope types:

variables parameters variables

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

Function Global Static

PHP Basics


Local Variable
A variable declared in a function is considered

Means it can be referenced only in that

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


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


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

Function Variable
Function parameters are declared after the

function name and inside parentheses.

PHP Basics


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


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

Static scoping is particularly useful for

recursive functions.

PHP Basics


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:
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


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

echo $recipe $pasta; echo $recipe ${$recipe};

The result of both is the string pasta &


PHP Basics


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

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()

define("PI", 3.141592);

PHP Basics


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


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

End of Chapter

PHP Basics

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