lab internal 1
lab internal 1
<?php
echo 10 <=> 20; // Output: -1 (10 is less than 20)
echo 20 <=> 20; // Output: 0 (Both are equal)
echo 30 <=> 20; // Output: 1 (30 is greater than 20)
?>
Bitwise Operators
<?php
$a = 5; // Binary: 0101
$b = 3; // Binary: 0011
switch ($day) {
case "Monday":
echo "Start of the week!";
break;
case "Friday":
echo "Weekend is near!";
break;
case "Sunday":
echo "It's a holiday!";
break;
default:
echo "It's a normal day.";
}
?>
Loops
for Loop
<?php
for ($i = 1; $i <= 3; $i++) {
echo "Number: $i <br>";
}
?>
while Loop
<?php
$num = 1;
while ($num <= 3) {
echo "Count: $num <br>";
$num++;
}
?>
do-while Loop
<?php
$x = 5;
do {
echo "Value: $x <br>";
$x++;
} while ($x < 5);
?>
foreach Loop (For Arrays)
<?php
$fruits = array("Apple", "Banana", "Mango");
sayHello("daksh");
?>
2) Return value
<?php
function add($a, $b) {
return $a + $b; // Returns the sum
}
welcome("daksh");
welcome(); // Uses default value "Guest"
?>
3) Variable-Length Argument
<?php
function sumNumbers(...$numbers) {
return array_sum($numbers);
}
$value = 10;
doubleValue($value);
echo "Doubled Value: $value";
?>
5) Anonymous Functions (Lambda)
<?php
$greet = function($name) {
return "Hello, $name!";
};
echo $greet("daksh");
?>
Arrays
Numeric (Indexed) Arrays
<?php
// Method 1: Using array()
$fruits = array("Apple", "Banana", "Mango");
// Method 2: Using []
$fruits = ["Apple", "Banana", "Mango"];
Associative Arrays
<?php
$marks = [
"Math" => 95,
"English" => 88,
"Science" => 92
];
Multidimensional
<?php
$students = [
["daksh", 22, "BCA"],
["John", 21, "BBA"],
["Sara", 23, "MCA"]
];
// Accessing elements
echo $students[0][0]; // Output: Daksh
echo $students[1][2]; // Output: BBA
?>
<?php
$numbers = [5, 3, 8, 1, 9];
sort($numbers);
print_r($numbers);
?>
<?php
$numbers = [5, 3, 8, 1, 9];
rsort($numbers);
print_r($numbers);
?>
<?php
$ages = ["Daksh" => 22, "John" => 19, "Sara" => 25];
asort($ages);
print_r($ages);
?>
Sort by Values (Descending) – arsort()
<?php
$ages = ["Daksh" => 22, "John" => 19, "Sara" => 25];
arsort($ages);
print_r($ages);
?>
<?php
$students = ["Daksh" => 95, "John" => 88, "Sara" => 92];
ksort($students);
print_r($students);
?>
<?php
$students = ["Daksh" => 95, "John" => 88, "Sara" => 92];
krsort($students);
print_r($students);
?>
Class
<?php
class Car {
// Properties (variables)
public $brand;
public $color;
// Method (function)
public function displayInfo() {
echo "This car is a $this->color $this->brand.";
}
}
?>
Object
<?php
$myCar = new Car(); // Create an object
$myCar->brand = "Tesla";
$myCar->color = "Red";
// Constructor method
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
<?php
// Simple array of students with their marks
$students = [
['name' => 'Alice', 'marks' => ['Math' => 85, 'Science' => 78, 'English' => 92]],
['name' => 'Bob', 'marks' => ['Math' => 67, 'Science' => 74, 'English' => 70]],
['name' => 'Charlie', 'marks' => ['Math' => 90, 'Science' => 88, 'English' => 95]],
];
// Loop through each student, calculate average and grade, then display the results
foreach ($students as $student) {
$average = calculateAverage($student['marks']);
$grade = getGrade($average);
echo $student['name'] . " - Average: " . round($average, 2) . ", Grade: " . $grade . "<br>";
}
?>
Output
Alice - Average: 85, Grade: B
Bob - Average: 70.33, Grade: C
Charlie - Average: 91, Grade: A