Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Chapter 3

PHP
 In this session we will see
 The definition of PHP
 Basic Syntax in PHP
 Programming Operation in PHP
 Variables in PHP
 The Goal of this session
 Understand the basic Syntax of PHP
 Understand the use of operator in PHP
 Understand declare, scope of Variables in PHP

1
Introduction
 PHP is a popular high-level scripting language used by a range of organizations and
developers.
 PHP is an acronym for "PHP: Hypertext Preprocessor“
 PHP is a server-side, open source scripting language
 PHP is cross platform
 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
 PHP provides a solid and well-defined programming language that includes support
for
 object-orientated programming,
 file handling,
 arithmetic, and more

2
Common use of PHP
 PHP performs system functions, i.e. it can create, open, read, write,
and close from files on a system
 PHP allows to add, delete, modify elements within your database
 Access cookies variables and set cookies
 restrict users to access some pages of your website
 encrypt data

3
Why I need PHP?
 PHP runs on different platforms (Windows, Linux, Unix, etc.)
 PHP is compatible with almost all servers used today like apache.
 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

4
PHP Syntax
 A PHP script always
starts with <?php and
ends with ?>
 A PHP script can be placed anywhere in the document.
Syntax
PHP code should enclosed within:
<?php and ?> So that it is distinguished from HTML.
Hence, the PHP parser only parses code which is in between <?php and ?>
 PHP code can be embedded in HTML

5
PHP Operators
 Operators are used to perform operations on variables and values.
 basically operators are divided in to the following group.
 Arithmetic operators
 Assignment operators
 Comparison operators
 Increment/Decrement operators
 Logical operators
 String operators

6
PHP Arithmetic Operators

 The PHP arithmetic operators are used with numeric values to


perform common arithmetical operations, such as addition,
subtraction, multiplication etc.

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

7
PHP Assignment Operators
 The PHP assignment operators are used with numeric values to write a value to a
variable.
 The basic assignment operation 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
8
PHP Comparison Operators
 The PHP comparison operators are used to compare two values (number or string)
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 $x >= $y Returns true if $x is greater than or equal to


equal to $y
9
<= Less than or equal $x <= $y Returns true if $x is less than or equal to $y
PHP Increment / Decrement Operators
 The PHP increment operators are used to increment a variable's value.
 The PHP decrement operators are used to decrement a variable's value.

Operator Name Description

++$x Pre-increment Increments $x by one, then returns $x

$x++ Post- Returns $x, then increments $x by one


increment
--$x Pre- Decrements $x by one, then returns $x
decrement
$x-- Post- Returns $x, then decrements $x by one
decrement
10
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


11
PHP String Operators

 PHP has two operators that are specially designed for strings

Operator Name Example Result

. Concatenation $txt1 . $txt2 Concatenation of $txt1


and $txt2

.= Concatenation $txt1 .= $txt2 Appends $txt2 to $txt1


assignment

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


o echo has no return value while print has a return value of 1 so it can be used
in expressions.
o echo can take multiple parameters (although such usage is rare) while print
can take one argument.
o echo is marginally faster than print.

13
What is Variable?
 Variable is a symbol or name that stands for a value.
 Variables are used for storing values such as numeric values,
characters, character strings.
 Variable in PHP is case insensitive.

14
PHP is a Loosely Typed Language
In the example above, notice that we did 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.

In other languages such as C, C++, and Java, the programmer must declare the
name and type of the variable before using it.

15
Cont………
PHP Variables Scope

In PHP, variables can be declared anywhere in the script.

The scope of a variable is the part of the script where the variable can be
referenced/used.

PHP has three different variable scopes:


o local

o global

o static

16
Global Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function:
Example
<?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>";


?>
17
Local Scope
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
Example
<?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>";
?>
You can have local variables with the same name in different functions, because
local variables are only recognized by the function in which they are declared. 18

You might also like