Uploaded by Abdul ghafar stanikzai

1Introduction to PPHP

advertisement
Introduction to PHP
Introduction to PHP
• PHP is a recursive acronym for "PHP: Hypertext Preprocessor".
• PHP is a server side scripting language that is embedded in HTML.
It is used to manage dynamic content, databases, session
tracking, even build entire e-commerce sites.
• It is integrated with a number of popular databases, including
MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL
Server.
• PHP Syntax is C-Like.
Introduction to PHP
• PHP is a server-side scripting language
• PHP scripts are executed on the server
• PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid,
PostgreSQL, Generic ODBC, etc.)
• PHP is open source software
• PHP is free to download and use
•PHP runs on different platforms (Windows, Linux, Unix, etc.)
• PHP is compatible with almost all servers used today (Apache, IIS, etc.)
• PHP is FREE to download from the official PHP resource: www.php.net
• PHP is easy to learn and runs efficiently on the server side
Introduction to PHP
Common Uses of PHP
•
•
•
•
•
•
•
•
•
•
•
•
Open-source
Easy to use ( C-like and Perl-like syntax)
Stable and fast
Multi-platform
Many databases support
Many common built-in libraries
Pre-installed in Linux distributions
PHP can handle forms, i.e. gather data from files, save data to a file,
thru email you can send data, return data to the user.
You add, delete, modify elements within your database thru PHP.
Access cookies variables and set cookies.
Using PHP, you can restrict users to access some pages of your
website.
It can encrypt data.
Introduction to PHP
Alternatives to PHP
• Practical extraction and Report Language (Perl)
• Active Server Pages (ASP)
• Java server pages (JSP)
• Ruby
Characteristics of PHP
Five important characteristics make PHP's practical
nature possible:
• Simplicity
• Efficiency
• Security
• Flexibility
• Familiarity
Introduction to PHP
• Instead of lots of commands to output HTML (as seen in C or Perl), PHP
pages contain HTML with embedded code that does "something" (like in
the next slide, it outputs "Hi, I'm a PHP script!").
• The PHP code is enclosed in special start and end processing
instructions <?php and ?> that allow you to jump into and out of "PHP
mode."
How PHP generates HTML/JS Web pages
Client
Browser
1
4
Apache
3
PHP module
2
1: Client from browser send HTTP request (with POST/GET variables)
2: Apache recognizes that a PHP script is requested and sends the request to PHP module
3: PHP interpreter executes PHP script, collects script output and sends it back
4: Apache replies to client using the PHP script output as HTML output
All PHP code must be included inside one of the three special markup tags ate are recognized by the PHP Parser.
<?php PHP code goes here ?>
<? PHP code goes here ?>
<script language="php"> PHP code goes
here </script>
• PHP code is executed on the server, generating HTML which is then sent to the
client.
• The client would receive the results of running that script, but would not know what
the underlying code was.
•A visual,
if you please...
PHP Hello World
•Above is the PHP source code.
PHP Hello World
• It renders as HTML that looks like this:
PHP Hello World
•This program is extremely simple and you really did not
need to use PHP to create a page like this. All it does is
display: Hello World using the PHP echo() statement.
•Think of this as a normal HTML file which happens to
have a set of special tags available to you that do a lot of
interesting things.
PHP Comments
•In PHP, we use // to make a single-line comment or /* and */ to
make a large comment block.
Hello World! (web oriented)
<html>
<head>
<title>My personal Hello World! PHP script</title>
</head>
<body>
<?
echo “Hello World!”;
?>
</html>
PHP tag, allow to insert PHP code.
Interpretation by PHP module will substitute the code with code output
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<p>bcvbkbkcbvkvbkvbkvk>/p>
<?php
echo "Hello World!";
?>
</body>
</html>
My first PHP page
Hello World!
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts
of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
10
PHP Variables
Variables are used for storing values, like text strings, numbers or arrays.
• When a variable is declared, it can be used over and over again in your script.
• All variables in PHP start with a $ sign symbol.
• The correct way of declaring a variable in PHP:
To use or assign variable $ must be present before the name of the variable
The assign operator is '='
There is no need to declare the type of the variable the current stored value
produces an implicit type-casting of the variable.
A variable can be used before to be assigned
•




$A = 1;
$B = “2”;
$C = ($A + $B); // Integer sum
$D = $A . $B; // String concatenation
echo $C; // prints 3
echo $D;// prints 12
PHP Variables
 In PHP, a variable does not need to be declared before adding a value to it.
 In the example above, you see that you do not have to tell PHP which data
type the variable is.
 PHP automatically converts the variable to the correct data type, depending
on its value.
 A variable name must start with a letter or an underscore "_" -- not a
number
 A variable name can only contain alpha-numeric characters, underscores
(a-z, A-Z, 0-9, and _ )
 A variable name should not contain spaces. If a variable name is more
than one word, it should be separated with an underscore ($my_string) or
with capitalization ($myString)
<!DOCTYPE html>
<html>
<body>
Variable names are care sensitive
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
Hello World!
Hello World!
Hello World!
Creating (Declaring) PHP Variables
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
<!DOCTYPE html>
<html>
<body>
<?php
$color =
echo "My
echo "My
echo "My
?>
</body>
</html>
"red";
car is " . $color . "<br>";
house is " . $COLOR . "<br>";
boat is " . $coLOR . "<br>";
My car is red
My house is
My boat is
Rules for PHP variables:

A variable starts with the $ sign, followed by the name of the variable

A variable name must start with a letter or the underscore character

A variable name cannot start with a number


A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
Variable names are case-sensitive ($age and $AGE are two different variables)
Output Variables echo statement is often used to output data to the screen
<?php
$txt = “lpu.in";
echo “This is $txt!";
?>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
<?php
$txt = " lpu.in ";
echo " This is " . $txt . "!";
?>
PHP Variables Scope
PHP has three different variable scopes:

local

global

static
Global and Local Scope A variable declared outside a function has a GLOBAL SCOPE
and can only be accessed outside a function:
Global Scope
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
Variable x inside function is:
Variable x outside function is: 5
LOCAL SCOPE
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
Variable x inside function is: 5
Variable x outside function is:
PHP The global Keyword
The global keyword is used to access a global
variable from within a function.
To do this, use the global keyword before the
variables (inside the function):
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
15
PHP The static Keyword
When a function is completed/executed, all of its variables are deleted. However, sometimes
we want a local variable NOT to be deleted. We need it for a further job.
To do this, use the static keyword when you first declare the variable:
<?php
function myTest()
{
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
0
1
2
PHP echo and print Statements
echo and print are more or less the same. They are both used to output data to the screen.
The differences are small:
echo has no return value while print has a return value of 1 so it can be used in
expressions.
echo can take multiple parameters (although such usage is rare) while print can take
one argument.
echo is marginally faster than print.
PHP echo Statements
<?php
echo "<h2> PHP is Fun! </h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
PHP is Fun!
Hello world!
I'm about to learn PHP!
This string was made with
multiple parameters.
PHP echo and print Statements
Display Variables with echo
The following example shows how to output text and variables with the echo statement:
<?php
$txt1 = "Learn Open Source PHP";
$txt2 = “lpu.in";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Learn Open Source PHP
Study PHP at lpu.in
9
PHP echo and print Statements
PHP print Statement
The print statement can be used with or without parentheses:
print or print().
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
PHP is Fun!
Hello world!
I'm about to learn PHP!
Display Variables with print
The following example shows how to output text and variables with the print statement:
<?php
$txt1 = "Learn PHP";
$txt2 = " lpu.in ";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>
Learn PHP
Study PHP at lpu.in
9
PHP Operators

Arithmetic operators

Assignment operators

Comparison operators

Increment/Decrement
operators

Logical operators

String operators

Array Operators
PHP 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)
<!DOCTYPE
<html>
<body>
<?php
$x =
$y =
echo
echo
echo
echo
echo
echo
?>
</body>
</html>
html>
6;
2;
$x
$x
$x
$x
$x
$x
+ $y;
- $y;
* $y;
/ $y;
% $y;
** $y;
Output
8
4
12
3
0
36
PHP Operators
PHP Assignment Operators
The basic assignment operator in PHP is "=".
Assignment
Same as...
Description
x=y
x=y
The left operand gets set to the value of the expression on
the right
x += y
x=x+y
Addition
x -= y
x=x-y
Subtraction
x *= y
x=x*y
Multiplication
x /= y
x=x/y
Division
x %= y
x=x%y
Modulus
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo $x;
?>
</body>
</html>
<!DOCTYPE htm
l>
<html>
<body>
<?php
$x = 10;
$x /= 5;
echo $x;
?>
</body>
</html>
10
2
<!DOCTYPE html>
<html>
<body>
<?php
$x = 20;
$x += 100;
echo $x;
?>
</body>
</html>
120
PHP Operators
PHP Comparison Operators
Operator
Name
Example
Result
==
Equal
$x == $y
Returns true if $x is equal to $y
===
Identical
$x === $y
Returns true if $x is equal to $y, and they are of
the same type
!=
Not equal
$x != $y
Returns true if $x is not equal to $y
<>
Not equal
$x <> $y
Returns true if $x is not equal to $y
!==
Not identical
$x !== $y
Returns true if $x is not equal to $y, or they are not
of the same type
>
Greater than
$x > $y
Returns true if $x is greater than $y
<
Less than
$x < $y
Returns true if $x is less than $y
>=
Greater than or equal to
$x >= $y
Returns true if $x is greater than or equal to $y
<=
Less than or equal to
$x <= $y
Returns true if $x is less than or equal to $y
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
<?php
$x = 100;
$y = "100";
var_dump($x == $y); // returns
true because values are equal
?>
var_dump($x === $y); // returns
false because types are not equal
?>
</body>
</html>
</body>
</html>
bool(true)
bool(false)
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
<?php
$x = 100;
$y = "100";
var_dump($x != $y);
// returns false
var_dump($x !== $y);
because values are equal
because types are not equal
?>
?>
</body>
</html>
</body>
</html>
bool(false)
bool(true)
// returns true
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<?php
$x = 50;
$y = 50;
<?php
$x = 50;
$y = 50;
var_dump($x <= $y);
var_dump($x >= $y); // returns true
because $x is greater than or equal to $y
?>
// returns true
because $x is less than or equal to $y
?>
</body>
</html>
</body>
</html>
bool(true)
bool(true)
PHP Operators
PHP Increment / Decrement Operators
Operator
Name
Description
++$x
Pre-increment
Increments $x by one, then returns $x
$x++
Post-increment
Returns $x, then increments $x by one
--$x
Pre-decrement
Decrements $x by one, then returns $x
$x--
Post-decrement
Returns $x, then decrements $x by one
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo ++$x;
?>
<?php
$x = 10;
echo $x++;
?>
<?php
$x = 10;
echo --$x;
?>
<?php
$x = 10;
echo $x--;
?>
</body>
</html>
</body>
</html>
</body>
</html>
</body>
</html>
9
10
11
10
PHP Operators
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
Operator
Name
Example
Result
and
And
$x and $y
True if both $x and $y are true
or
Or
$x or $y
True if either $x or $y is true
xor
Xor
$x xor $y
True if either $x or $y is true, but not both
&&
And
$x && $y
True if both $x and $y are true
||
Or
$x || $y
True if either $x or $y is true
!
Not
!$x
True if $x is not true
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
<?php
$x = 100;
$y = 50;
<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50)
{
echo "Hello world!";
}
?>
if ($x == 100 && $y == 50)
{
echo "Hello world!";
}
?>
if ($x == 100 xor $y
== 80)
{
echo "Hello world!";
}
?>
</body>
</html>
</body>
</html>
Hello world!
Hello world!
</body>
</html>
Hello world!
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
<?php
$x = 100;
if ($x == 10 xor $y == 50)
{
echo "Hello world!";
}
?>
</body>
</html>
Hello world!
if ($x !== 90)
{
echo "Hello world!";
}
?>
</body>
</html>
Hello world!
PHP Operators
PHP String Operators
Operator Name
Example
Result
.
Concatenation
$txt1 . $txt2
Concatenation of $txt1 and $txt2
.=
Concatenation assignment
$txt1 .= $txt2
Appends $txt2 to $txt1
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<?php
$txt1 = "Hello";
$txt2 = " world!";
echo $txt1 . $txt2;
?>
<?php
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>
</body>
</html>
Hello world!
</body>
</html>
Hello world!
PHP Operators
PHP Array Operators
Operator
The PHP array operators are used to compare arrays.
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
+
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
print_r($x + $y); // union of $x and $y
?>
</body>
</html>
Array ( [a] => red [b] => green [c] => blue [d] => yellow )
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x == $y);
?>
</body>
</html>
bool(false)
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x === $y);
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x !== $y);
?>
</body>
</html>
bool(false)
bool(true)
Download