Академический Документы
Профессиональный Документы
Культура Документы
Click to addMohanty
Prithviraj Text
Asst.Prof. Dept of CSIT
02/11/16
Introduction
The PHP Hypertext Preprocessor (PHP) is a programming
language that allows web developers to create dynamic content
that interacts with databases.
PHP is basically used for developing web based software
applications.
PHP is a server scripting language, and a powerful tool for
making dynamic and interactive Web pages.
PHP is a widely-used, free, and efficient alternative to
competitors such as Microsoft's ASP.
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.
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? What Can PHP Do?
PHP files can contain text,
PHP can generate dynamic
HTML, CSS, JavaScript, and page content.
PHP code.
PHP can create, open, read,
PHP code are executed on the write, delete, and close files
server, and the result is returned on the server.
to the browser as plain HTML.
PHP can collect form data
PHP files have extension ".php"
PHP can send and receive
Note: With PHP you are not cookies
limited to output HTML. You
PHP can add, delete, modify
can output images, PDF files,
data in your database
and even Flash movies. You can
also output any text, such as
PHP can be used to control
XHTML and XML. user-access
PHP can encrypt data
Why PHP? Characteristics of PHP
PHP runs on various platforms Five important characteristics
(Windows, Linux, Unix, Mac OS make PHP's practical nature
X, etc.)
possible −
PHP is compatible with almost all
servers used today (Apache, IIS,
Simplicity
etc.)
Efficiency
PHP supports a wide range of
Security
databases
Flexibility
PHP is free. Download it from the
official PHP resource:
Familiarity
www.php.net
PHP is easy to learn and runs
efficiently on the server side
How to work with PHP?
To start using PHP, you can: Set Up PHP on Your Own PC
Find a web host with PHP and MySQL
support
However, if your server does not
Install a web server on your own PC,
support PHP, you must:
and then install PHP and MySQL
install a web server
Use a Web Host With PHP Support
install PHP
If your server has activated support for
PHP you do not need to do anything.
install a database, such as
Just create some .php files, place them MySQL
in your web directory, and the server
The official PHP website
will automatically parse them for you.
(PHP.net) has installation
You do not need to compile anything
or install any extra tools. instructions for PHP:
Because PHP is free, most web hosts http://php.net/manual/en/install.
offer PHP support. php
PHP Syntax
A PHP script is executed on the server, and the plain HTML
result is sent back to the browser.
Basic PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP
scripting code.
First PHP Program
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
My first PHP page
<?php
Hello World!
echo "Hello World!";
?>
</body> My first PHP page
</html> Hello World!
Note: PHP statements end with a semicolon (;).
Comments in PHP
Example:
A comment in PHP code is a line that <html><body>
is not read/executed as part of the
<?php
program.
// This is a single-line comment
Its only purpose is to be read by
# This is also a single-line comment
someone who is looking at the code.
/*
Comments can be used to:
This is a multiple-lines comment block
Let others understand what you are
that spans over multiple
doing
lines
Remind yourself of what you did -
*/
Most programmers have experienced
coming back to their own work a // You can also use comments to leave out
year or two later and having to re- parts of a code line
figure out what they did. Comments $x = 5 /* + 15 */ + 5;
can remind you of what you were echo $x;
thinking when you wrote the code ?>
</body></html>
PHP Case Sensitivity
In PHP, all keywords (e.g. if, else, However; all variable names are case-
while, echo, etc.), classes, sensitive.
functions, and user-defined <html>
functions are NOT case-sensitive.
<body>
In the example below, all three
<?php
echo statements below are legal
(and equal): $color = "red";
<html><body> echo "My car is " . $color . "<br>";
<?php echo "My house is " . $COLOR .
"<br>";
ECHO "Hello World!<br>";
echo "My boat is " . $coLOR . "<br>";
echo "Hello World!<br>";
?>
EcHo "Hello World!<br>";
</body>
?>
</html>
</body></html>
PHP Variables
Variables are "containers" for storing
information. Rules for PHP variables:
Creating (Declaring) PHP Variables
A variable starts with the $ sign,
In PHP, a variable starts with the $ sign, followed by the name of the
followed by the name of the variable: variable.
Example
A variable name must start with a
<?php letter or the underscore character
$txt = "Hello world!";
A variable name cannot start with
$x = 5;
a number
$y = 10.5;
?>
A variable name can only contain
alpha-numeric characters and
Note: When you assign a text value to a
variable, put quotes around the value. underscores (A-z, 0-9, and_)
Note: Unlike other programming languages,
Variable names are case-sensitive
PHP has no command for declaring a ($age and $AGE are two different
variable. It is created the moment you first variables)
assign a value to it.
Output Variables
The PHP echo statement is often PHP is a Loosely Typed
used to output data to the screen.
Language
Example-1
<?php
In the example above, notice
that we did not have to tell PHP
$txt = "my India";
which data type the variable is.
echo "I love $txt!";
?>
PHP automatically converts the
variable to the correct data
Example-2
type, depending on its value.
<?php
$x = 5;
In other languages such as C,
C++, and Java, the programmer
$y = 4;
must declare the name and type
echo $x + $y;
of the variable before using it.
?>
PHP Variables Scope
In PHP, variables can be declared anywhere in
the script. Example-2
The scope of a variable is the part of the script <?php
where the variable can be referenced/used. function myTest() {
PHP has three different variable scopes: $x = 5; // local scope
Local echo "<p>Variable x inside function is:
Global $x</p>";
Static }
Example-1 myTest();
<?php // using x outside the function will generate an
$x = 5; // global scope error
function myTest() { echo "<p>Variable x outside function is:
// using x inside this function will generate an $x</p>";
error ?>
echo "<p>Variable x inside function is: Note: You can have local variables with the
$x</p>";} same name in different functions, because
myTest(); local variables are only recognized by the
echo "<p>Variable x outside function is: $x</p>"; function in which they are declared.
?>
PHP The global Keyword
PHP The static Keyword
The global keyword is used to access a
Normally, when a function is
global variable from within a function. completed/executed, all of its variables are
To do this, use the global keyword deleted. However, sometimes we want a local
before the variables (inside the function): variable NOT to be deleted. We need it for a
Example further job.
<?php
To do this, use the static keyword when you
first declare the variable:
$x = 5;
Example
$y = 10; <?php
function myTest() { function myTest() {
global $x, $y; static $x = 0;
$y = $x + $y; echo $x;
} $x++;
myTest(); }
echo $y; // outputs 15 myTest();
?> myTest();
myTest();
?>
PHP echo and print Statements
echo and print are more or less Example-1(echo)
the same. They are both used to <?php
output data to the screen. echo "<h2>PHP is Fun!</h2>";
The differences are small: echo "Hello world!<br>";
echo has no return value while echo "I'm about to learn PHP!<br>";
print has a return value of 1, so ?>
it can be used in expressions. Example-2(print)
echo can take multiple <?php
parameters (although such print "<h2>PHP is Fun!</h2>";
usage is rare) while print can print "Hello world!<br>";
take one argument. print "I'm about to learn PHP!";
echo is marginally faster than ?>
print.
PHP Data Types
Variables can store data of different PHP String
types, and different data types can
A string is a sequence of characters,
do different things. like "Hello world!".
PHP supports the following data
A string can be any text inside
types: quotes. You can use single or double
String quotes:
Integer Example
Float (floating point numbers - also <?php
called double) $x = "Hello world!";
Boolean $y = 'Hello world!';
Array echo $x;
Object echo "<br>";
NULL echo $y;
Resource ?>
PHP Integer
An integer data type is a non-decimal PHP Float
number between -2,147,483,648 and
A float (floating point number) is
2,147,483,647.
a number with a decimal point or
Rules for integers:
a number in exponential form.
An integer must have at least one digit
An integer must not have a decimal point
In the following example $x is a
An integer can be either positive or float. The PHP var_dump()
negative function returns the data type and
Integers can be specified in three formats: value:
decimal (10-based), hexadecimal (16-
based - prefixed with 0x) or octal (8-based
Example
- prefixed with 0) <?php
Example $x = 10.365;
<?php
var_dump($x);
$x = 5985;
var_dump($x); ?>
?>
PHP Boolean PHP Array
A Boolean represents two possible
An array stores multiple values in one
states: TRUE or FALSE. single variable.
In the following example $cars is an
$x = true;
array. The PHP var_dump() function
$y = false; returns the data type and value:
Booleans are often used in conditional Example
testing. <?php
PHP Resource $cars = array("Volvo","BMW","Toyota");
The special resource type is not an var_dump($cars);
actual data type. It is the storing of a ?>
reference to functions and resources Output:
external to PHP. array(3) { [0]=> string(5) "Volvo" [1]=>
A common example of using the string(3) "BMW" [2]=> string(6)
resource data type is a database call. "Toyota" }
PHP Object
PHP NULL Value
An object is a data type which stores data and
information on how to process that data.
Null is a special data type which can have
only one value: NULL.
In PHP, an object must be explicitly declared.
First we must declare a class of object. For this,
A variable of data type NULL is a variable
we use the class keyword. A class is a structure that has no value assigned to it.
that can contain properties and methods:
Tip: If a variable is created without a
Example value, it is automatically assigned a value
<?php of NULL.
class Car {
Variables can also be emptied by setting
function Car() { the value to NULL:
$this->model = "VW"; Example
}}
<?php
// create an object
$x = "Hello world!";
$herbie = new Car();
$x = null;
// show object properties
echo $herbie->model; var_dump($x);
?> ?>
PHP String Functions
Get The Length of a String Reverse a String
The PHP strlen() function returns the
The PHP strrev() function reverses a string:
length of a string. Example
The example below returns the length of <?php
the string "Hello world!": echo strrev("Hello world!"); // outputs !dlrow olleH
Example ?>
Search For a Specific Text Within a String
<?php
The PHP strpos() function searches for a specific
echo strlen("Hello world!"); // outputs 12 text within a string.
?>
If a match is found, the function returns the
Count The Number of Words in a String character position of the first match. If no match
is found, it will return FALSE.
The PHP str_word_count() function
The example below searches for the text
counts the number of words in a string:
"world" in the string "Hello world!":
Example Example
<?php <?php
echo str_word_count("Hello world!"); // echo strpos("Hello world!", "world"); // outputs 6
outputs 2 ?>
?>
PHP Constants
A constant is an identifier (name) for a
simple value. The value cannot be Example-1
changed during the script. <?php
A valid constant name starts with a letter define("GREETING", "Welcome to
or underscore (no $ sign before the iter.ac.in!");
constant name). echo GREETING;
Create a PHP Constant ?>
To create a constant, use the define() Example-2
function.
<?php
Syntax
define("GREETING", "Welcome to
define(name, value, case-insensitive) iter.ac.in!", true);
Parameters:
echo greeting;
name: Specifies the name of the constant
?>
value: Specifies the value of the constant
Note: Unlike variables,constants are
case-insensitive: Specifies whether the automatically global and can be used
constant name should be case-insensitive. across the entire script.
Default is false
PHP Operators
Operators are used to perform PHP String Operators
operations on variables and
PHP has two operators that are
values. specially designed for strings.
PHP divides the operators in . Concatenation $txt1 . $txt2
the following groups: Concatenation of $txt1 and
Arithmetic operators $txt2
Assignment operators .= Concatenation assignment
Comparison operators $txt1 .= $txt2 Appends $txt2
Increment/Decrement to $txt1
operators
Logical operators
String operators
Array operators
PHP Arithmetic Operators
Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y'th
power (Introduced in PHP 5.6)
PHP Array Operators
The PHP array operators are used to compare arrays.
Operator Name Example Result
+ Union $x + $y Union of $x and $y
== Equality $x == $y Returns true if $x and $y have the
same key/value pairs
=== Identity $x === $y Returns true if $x and $y have
the same key/value pairs in the
same order and of the same types
!= Inequality $x != $y Returns true if $x is not equal to $y
<> Inequality $x <> $y Returns true if $x is not equal to $y
!== Non-identity $x !== $y Returns true if $x is not identical to $y
PHP Conditional Statements
Conditional statements are used to perform different actions based on
different conditions.
In PHP we have the following conditional statements:
if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is true
and another code if that condition is false
if...elseif....else statement - executes different codes for more
than two conditions
switch statement - selects one of many blocks of code to be
executed
PHP Loops
Often when you write code, you want the same block of code to run
over and over again in a row. Instead of adding several almost equal
code-lines in a script, we can use loops to perform a task like this.
In PHP, we have the following looping statements:
while - loops through a block of code as long as the specified
condition is true
do...while - loops through a block of code once, and then
repeats the loop as long as the specified condition is true
for - loops through a block of code a specified number of
times
foreach - loops through a block of code for each element in
an array
The PHP while Loop
The while loop executes a Example:
block of code as long as the <html>
specified condition is true. <body>
Syntax <?php
while (condition is true) { $x = 1;
code to be executed; while($x <= 5) {
} echo "The number is: $x <br>";
$x++;
}
?>
</body>
</html>
The PHP do...while Loop
The do...while loop will Example:
always execute the block of <html>
code once, it will then check <body>
the condition, and repeat the
<?php
loop while the specified
condition is true. $x = 1;
do {
Syntax
echo "The number is: $x <br>";
do {
$x++;
code to be executed;
} while ($x <= 5);
} while (condition is true); ?>
</body>
</html>
The PHP for Loop
The for loop is used when you know in
Example: