Professional Documents
Culture Documents
PHP1
PHP1
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
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
!== 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
PHP has two operators that are specially designed for strings
12
PHP echo and print Statements
echo and print are more or less the same.
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
The scope of a variable is the part of the script where the variable can be
referenced/used.
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();