0% found this document useful (0 votes)
3 views

Unit 3 PHP

Uploaded by

foxyridersanju
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 3 PHP

Uploaded by

foxyridersanju
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 76

Unit -III

PHP
What is PHP?
• PHP is an acronym for "PHP: Hypertext
Preprocessor"
• PHP is a widely-used, scripting language and a
powerful tool for making interactive and
dynamic web pages.
• PHP scripts are executed on the server
• PHP is free to download and use
What is a PHP File?
• PHP files can contain text, HTML, CSS,
JavaScript, and PHP code
• PHP code is executed on the server, and the
result is returned to the browser as plain
HTML
• PHP files have extension ".php"
What Can PHP Do
• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, and
close files on the server
• PHP can collect form data
• PHP can add, delete, modify data in your
database
• PHP can be used to control user-access
• PHP can encrypt data
**PHP Syntax/Structure of PHP
• A PHP script can be placed anywhere in the
document.
• A PHP script starts with <?php and ends with ?>

• The default file extension for PHP files is ".php".


• A PHP file normally contains HTML tags, and
some PHP scripting code.
Example
*PHP echo function:
• The echo() function outputs one or more
strings.
• echo can take multiple parameters
• The echo() function is not actually a function.
• The echo statement can be used with or
without parentheses: echo or echo().
*PHP print function
• Print is an output function in php.
• The print statement can be used with or
without parentheses: print or print().
• print can take one argument.
***
PHP Comments
• A comment in PHP code is a line that is not
executed as a part of the program.
• PHP supports several ways of commenting:
• single-line comments
• multiple-line comments
single-line comments

• Comments with single line.


• Eg:
multiple-line comments

• Comment line in more than one lines .


• Eg:
*PHP Variables
• Variables in a program are used to store some
values or data that can be used later in a
program.
• The variables are also like containers that store
character values, numeric values, memory
addresses, and strings.
• In PHP, a variable starts with the $ sign, followed
by the name of the variable.
Syntax of declaring a variable in
PHP
$variablename=value;
Eg:
Rule for 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)
**Operators in PHP
• Operators are used to perform operations on variables
and values.
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
PHP Arithmetic Operators
• The PHP arithmetic operators are used to perform
common arithmetic operations such as addition,
subtraction, etc. with numeric values.
Eg:
PHP Assignment Operators
• The assignment operators are used to assign value to
different variables. The basic assignment operator is
"=".
Eg: =
+=
-=
*=
/=
%=
PHP Comparison Operators
• Comparison operators allow comparing two
values.
==
• Return TRUE if $a is equal to $b
=== (Identical)
• Returns true if $x is equal to $y, and they are
of the same type
!=
• Returns true if $x is not equal to $y
<>
• Returns true if $x is not equal to $
!== (Not identical)
• Returns true if $x is not equal to $y, or they
are not of the same type
>= Greater than or equal to
• Returns true if $x is greater than or equal to $y
<=Less than or equal to
• Returns true if $x is less than or equal to $y
<=>Spaceship
• Return -1 if $a is less than $b
Return 0 if $a is equal $b
Return 1 if $a is greater than $b
PHP Increment / Decrement Operators
PHP Logical Operators
and
or
xor
• True if either $x or $y is true, but not both
String operators
. Concatenation
.=Concatenation assignment
Array operators
Conditional assignment operators
Data types in PhP
• PHP supports 8 primitive data types that can be categorized
further in 3 types:
• Scalar Types (predefined)
• Compound Types (user-defined)
• Special types
Scalar Types (predefined)

• It holds only single value. There are 4 scalar


data types in PHP.
• boolean
• integer
• float
• string
PHP Boolean
• It holds only two values: TRUE (1) or FALSE (0).
It is often used with conditional statements. If
the condition is correct, it returns TRUE
otherwise FALSE.
PHP Integer
• Integer means numeric data with a negative or
positive sign. It holds only whole numbers, i.e.,
numbers without fractional part or decimal
points.
• Rules for integer:
• An integer can be either positive or negative.
• An integer must not contain decimal point.
• Integer can be decimal (base 10), octal (base 8),
or hexadecimal (base 16).
• Eg:
PHP Float
• A float (floating point number) is a number
with a decimal point or a number in
exponential form.
PHP String

• A string is a sequence of characters.


• A string can be any text inside quotes.
• We can use single or double quotes.
Compound Types

• It can hold multiple values. There are 2


compound data types in PHP.
• array
• object
PHP Array

• An array stores multiple values in one single


variable.
• In the following example $cars is an array. The
PHP var_dump() function returns the data
type and value:
PHP Object
• Classes and objects are the two main aspects
of object-oriented programming.
• A class is a template for objects, and an object
is an instance of a class.
Special types
PHP NULL Value

• Null is a special data type which can have only


one value: NULL.
• A variable of data type NULL is a variable that
has no value assigned to it.
*PHP Conditional Statements
• The PHP conditional statements are:
• 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 - The if Statement
• The if statement executes some code if one
condition is true
PHP if...else Statements
• The if...else statement executes some code if a
condition is true and another code if that
condition is false
Example
if...elseif...else
• The if...elseif...else a special statement that is
used to combine multiple if...else statements.
Example
The PHP switch Statement
• Use the switch statement to select one of
many blocks of code to be executed.
• The expression is evaluated once
• The value of the expression is compared with
the values of each case
• If there is a match, the associated block of
code is executed
• The break keyword breaks out of the switch
block
• The default code block is executed if there is
no match
Example
**PHP Loops
• Loops are used to execute the same block of code
again and again, as long as a certain condition is
true.
• loop types:
• 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
The while loop statement

• The while statement will execute a block of


code if and as long as a test expression is true.
• If the test expression is true then the code
block will be executed.
• After the code has executed the test
expression will again be evaluated and the
loop will continue until the test expression is
found to be false.
• Example
do…while Loop

• The do-while loop is a variant of while loop,


which evaluates the condition at the end of
each loop iteration.
• With a do-while loop the block of code
executed once, and then the condition is
evaluated, if the condition is true, the
statement is repeated as long as the specified
condition evaluated to is true.
Syntax

Example
PHP for Loop
• Syntax

• initialization — it is used to initialize the


counter variables
• condition — in the beginning of each iteration,
condition is evaluated.
• increment - increment or decrement variable
Example
**PHP foreach Loop

• The foreach statement is used to loop through


arrays.
• For every loop iteration, the value of the
current array element is assigned to $value
and the array pointer is moved by one, until it
reaches the last array element..
• The foreach loop is used to iterate over arrays.
• The foreach loop works only on arrays
• Example
Break
• The break statement can be used to jump out
of different kind of loops.
• When a break is encountered the control will
move to the next immediate statement after
the loop.
Continue Statement
• The continue statement can be used to jump
out of the current iteration of a loop, and
continue to the first.

You might also like