0% found this document useful (0 votes)
3 views28 pages

Unit-1 PHP

The document provides an overview of PHP programming concepts, including constants, data types, operators, conditional statements, and loops. It explains the syntax and functionality of various PHP elements such as echo, print, and different types of operators. Additionally, it covers control structures like if statements, switch statements, and looping mechanisms like while, do...while, for, and foreach.

Uploaded by

tecnohitarth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views28 pages

Unit-1 PHP

The document provides an overview of PHP programming concepts, including constants, data types, operators, conditional statements, and loops. It explains the syntax and functionality of various PHP elements such as echo, print, and different types of operators. Additionally, it covers control structures like if statements, switch statements, and looping mechanisms like while, do...while, for, and foreach.

Uploaded by

tecnohitarth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 28

Unit-1 PHP

The INSB IITMS BCA College, Idar


PHP Constants
 A constant is an identifier (name) for a simple value.
The value cannot be changed during the script.
 A valid constant name starts with a letter or underscore
(no $ sign before the constant name).
Note: Unlike variables, constants are automatically global
across the entire script.
 PHP Constant Syntax: use the define() function
 define(name, value, case sensitive)
define(“abc", "Welcome to BCA College idar!");
echo abc;
Echo & Print
 Echo has no return value While print is more like
PHP function so it return value, Which is always
set 1.
 Echo can take multiple parameters while print can
take one argument.
 echo and print are more or less the same. They are
both used to output data to the screen.
 echo is marginally faster than print.
Datatype
 Variables can store data of different types, and different
data types can do different things.
PHP supports the following data types:
1) String
2) Integer
3) Float (floating point numbers - also called double)
4) Boolean
5) Array
6) Object
7) NULL
PHP Operators
 Operators are used to perform operations on variables
and values.
PHP divides the operators in the following groups:
1) Arithmetic operators
2) Assignment operators
3) Comparison operators
4) Increment/Decrement operators
5) Logical operators
6) String operators
7) Conditional assignment operators
8) Bitwise operators
9) Precedence and accociativity
Arithmetic Operator
The PHP arithmetic operators are used with numeric
values to perform common arithmetical operations, such
as addition, subtraction, multiplication etc.
Assignment Operator
 The PHP assignment operators are used with numeric values to
write a value to a variable. The basic assignment operator in PHP is
"=". It means that the left operand gets set to the value of the
assignment expression on the right.
Comparison Operator
The PHP comparison operators are used to compare two values
(number or string).
Increment / Decrement Operator
 The PHP increment operators are used to increment a variable's
value. The PHP decrement operators are used to decrement a
variable's value.
Logical Operator
The PHP logical operators are used to combine conditional
statements.
String Operator
PHP has two operators that are specially designed for strings.
Conditional Assignment Operators
The PHP conditional assignment operators are used to
set a value depending on conditions.
Bitwise Operators
 Bitwise operators are used on (binary) numbers.
Operator Precedence and Associativity
The precedence of an operator specifies how "tightly" it binds two
expressions together. For example, in the expression 1 + 5 * 3, the
answer is 16 and not 18 because the multiplication ("*") operator has
a higher precedence than the addition ("+") operator. Parentheses
may be used to force precedence, if necessary. For instance: (1 + 5) *
3 evaluates to 18.
 When operators have equal precedence their associativity decides
how the operators are grouped. For example "-" is left-associative,
so 1 - 2 - 3 is grouped as (1 - 2) - 3 and evaluates to -4. "=" on the
other hand is right-associative, so $a = $b = $c is grouped as $a = ($b
= $c).
Operators of equal precedence that are non-associative cannot be
used next to each other, for example 1 < 2 > 1 is illegal in PHP. The
expression 1 <= 1 == 1 on the other hand is legal, because
the == operator has a lower precedence than the <= operator.
Variable Handling Functions
PHP Conditional Statements
 Very often when you write code, you want to perform
different actions for different conditions. You can use
conditional statements in your code to do this.
 In PHP we have the following conditional statements:
1) If statement - executes some code if one condition is
true
Ex. if (condition)
{
// code to be executed if condition is true;
}
 if (5 > 3) { echo "Have a good day!"; }
PHP Conditional Statements
2) If…else statement - executes some code if a condition
is true and another code if that condition is false
Syntax:-
if (condition)
{
// code to be executed if condition is true;
} else
{
// code to be executed if condition is false;
}
 If($a==$b) { echo”True”; } else { echo “ False”; }
PHP Conditional Statements
3) If…else If…else statement -
The if...elseif...else statement executes different codes for
more than two conditions.
Syntax:-
if (condition)
{
// code to be executed if condition is true;
} else if (condition)
{
// code to be executed if condition is false and this
condition is true;
} else {
// code to be executed if all conditions are false;
}
4) Switch statement - The switch statement is used to
perform different actions based on different conditions.
Syntax:-
switch(expression) {
case label1:
//code block
break;
case label2:
//code block;
break;
case label3:
//code block
break;
default:
//code block }
PHP Loops and Branching Statements
when you write code, you want the same block of code to run
over and over again a certain number of times. So, instead of adding
several almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again,
as long as a certain condition is true.
In PHP, we have the following loop types:
1) While - loops through a block of code as long as the specified
condition is true
2) Do...while - loops through a block of code once, and then repeats
the loop as long as the specified condition is true
3) for- loops- through a block of code a specified number of times
4) foreach- loops through a block of code for each element in an
array
5) Break-The break statement can be used to jump out of a for loop.
6) Continue- The continue statement stops the current iteration in
the for loop and continue with the next.
PHP Loops and Branching Statements
1) While - loops through a block of code as long as the specified
condition is true.
Syntax:-
while(condition)
{
//code to be executed
}
For ex.
$i = 1;
while ($i < 6)
{ echo $i;
$i++;
}
Alternative Syntax:
while(condition):
code to be executed
Endwhile;
2) Do...while - loops through a block of code once, and then repeats
the loop as long as the specified condition is true.
-> The do...while loop will always execute the block of code at least
once, it will then check the condition, and repeat the loop while the
specified condition is true.
For ex. $i = 1;
do { echo $i;
$i++;
} while ($i < 6);

Ex.
$i = 1;
do {
if ($i == 3) break;
echo $i;
$i++;
} while ($i < 6);
PHP Loops and Branching Statements
3) for- loops- The for loop is used when you know how many times
the script should run.
Syntax:- for (expression1, expression2, expression3)
{
// code block
}
Ex.
for ($n = 1; $n <= 10; $n++)
{
echo "$n ";
} // Output: 1 2 3 4 5 6 7 8 9 10

for ($x = 0; $x <= 10; $x++)


{
echo "The number is: $x <br>";
}
PHP Loops and Branching Statements
4) foreach- The foreach loop - Loops through a block of code for
each element in an array or each property in an object. The most
common use of the foreach loop, is to loop through the items of an
array.
Syntax:
foreach ($array as $var) {
//code to be executed
}
Ex.
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x)
{
echo "$x <br>";
}
PHP Loops and Branching Statements
5) Break-The break statement can be used to jump out of a for loop.
The break statement can be used to jump out of a for loop.
Ex.
for ($x = 0; $x < 10; $x++)
{
if ($x == 5)
{
break;
}
echo "The number is: $x <br>";
}
PHP Loops and Branching Statements
6) Continue- The continue statement stops the current iteration in
the for loop and continue with the next.
Ex.
$x = 0;
while($x < 10)
{
if ($x == 4)
{
continue;
}
echo "The number is: $x <br>";
$x++;
}

You might also like