PHP – Operators and Control Structures
1. Operators in PHP
Operators are symbols that tell PHP to perform specific operations like addition,
comparison, assignment, logic checking etc.
1.1 Arithmetic Operators
Syntax:
$result = $a (+, -, *, /, %) $b;
Example:
<?php
$a = 10;
$b = 3;
echo "Addition: " . ($a + $b) . "<br>";
echo "Subtraction: " . ($a - $b) . "<br>";
echo "Multiplication: " . ($a * $b) . "<br>";
echo "Division: " . ($a / $b) . "<br>";
echo "Modulus: " . ($a % $b) . "<br>";
?>
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333
Modulus: 1
Explanation:
Arithmetic operators are used for basic mathematical calculations.
1.2 Assignment Operators
Syntax:
$x = value;
$x += value; // x = x + value
Example:
<?php
$x = 5;
$x += 3; // 8
$x -= 2; // 6
$x *= 4; // 24
$x /= 6; // 4
echo "Final Value: $x";
?>
Output:
Final Value: 4
1.3 Comparison Operators
Example:
<?php
$a = 10;
$b = "10";
var_dump($a == $b); // true
var_dump($a === $b); // false
var_dump($a != 5); // true
var_dump($a > 5); // true
?>
Output:
true
false
true
true
1.4 Logical Operators
Example:
<?php
$age = 20;
$citizen = true;
if($age >= 18 && $citizen){
echo "Eligible to vote";
} else {
echo "Not eligible";
}
?>
Output:
Eligible to vote
1.5 Increment / Decrement Operators
Example:
<?php
$num = 5;
echo ++$num . "<br>"; // 6
echo $num++ . "<br>"; // 6 (then becomes 7)
echo $num . "<br>"; // 7
?>
Output:
6
6
7
1.6 String Operators
Example:
<?php
$first = "Hello";
$second = "World";
echo $first . " " . $second; // Hello World
echo "<br>";
$msg = "PHP";
$msg .= " Programming";
echo $msg;
?>
Output:
Hello World
PHP Programming
2. Control Structures in PHP
Control structures decide the flow of execution – which code will run and when.
2.1 If Statement
Example:
<?php
$marks = 75;
if($marks >= 50){
echo "Pass";
}
?>
Output:
Pass
2.2 If-Else Statement
Example:
<?php
$marks = 35;
if($marks >= 50){
echo "Pass";
} else {
echo "Fail";
}
?>
Output:
Fail
2.3 If-ElseIf-Else
Example:
<?php
$marks = 85;
if($marks >= 90){
echo "Grade A+";
} elseif($marks >= 75){
echo "Grade A";
} else {
echo "Needs Improvement";
}
?>
Output:
Grade A
2.4 Switch Case
Example:
<?php
$day = 3;
switch($day){
case 1: echo "Monday"; break;
case 2: echo "Tuesday"; break;
case 3: echo "Wednesday"; break;
default: echo "Invalid day";
}
?>
Output:
Wednesday
3. Loops in PHP
Loops are used when you want to repeat code multiple times.
3.1 While Loop
Example:
<?php
$i = 1;
while($i <= 5){
echo $i . "<br>";
$i++;
}
?>
Output:
1
2
3
4
5
3.2 Do-While Loop
Example:
<?php
$i = 1;
do {
echo $i . "<br>";
$i++;
} while($i <= 5);
?>
Output:
1
2
3
4
5
3.3 For Loop
Example:
<?php
for($i = 1; $i <= 5; $i++){
echo "Number: $i<br>";
}
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
3.4 Foreach Loop
Example:
<?php
$colors = ["Red", "Green", "Blue"];
foreach($colors as $c){
echo $c . "<br>";
}
?>
Output:
Red
Green
Blue
PHP – Arrays, Functions and References
1. Arrays in PHP
An array is a collection of multiple values stored in a single variable.
Types of Arrays:
1. Indexed Array
2. Associative Array
3. Multidimensional Array
1.1 Indexed Array
Syntax:
$array = array("value1", "value2", "value3");
Example:
<?php
$colors = array("Red", "Green", "Blue");
echo $colors[0];
?>
Output:
Red
1.2 Associative Array
Syntax:
$array = array("key1"=>"value1", "key2"=>"value2");
Example:
<?php
$student = array("name"=>"Raj", "age"=>20);
echo $student["name"];
?>
Output:
Raj
1.3 Multidimensional Array
Example:
<?php
$marks = array("Amit" => array("Math"=>90, "English"=>85));
echo $marks["Amit"]["Math"];
?>
Output:
90
2. Functions in PHP
A function is a block of code that performs a specific task and can be reused.
2.1 User-defined Functions
Syntax:
function functionName(parameters){
// code
}
Example:
<?php
function greet($name){
return "Hello, " . $name;
}
echo greet("Mihir");
?>
Output:
Hello, Mihir
2.2 Functions with Default Parameter
Example:
<?php
function greet($name="Guest"){
echo "Welcome, " . $name;
}
greet();
?>
Output:
Welcome, Guest
2.3 Functions with Return Value
Example:
<?php
function add($a, $b){
return $a + $b;
}
echo add(10,20);
?>
Output:
30
3. References in PHP
A reference means two variables pointing to the same memory location.
3.1 Assign by Reference
Example:
<?php
$x = 10;
$y = &$x;
$y = 20;
echo $x;
?>
Output:
20
3.2 Pass by Reference in Functions
Example:
<?php
function addFive(&$num){
$num += 5;
}
$value = 10;
addFive($value);
echo $value;
?>
Output:
15