Practical (1) (3)
Practical (1) (3)
File Operations
Command Description
cat Display file contents
less View file contents page by page
head Display the first 10 lines of a file
tail Display the last 10 lines of a file
grep Search for text within files
wc Count lines, words, and characters in a
file
cut Extract specific columns from a file
sort Sort the lines of a file
diff Compare two files line by line
echo Print text to the terminal or a file
<?php
$num = -5;
if ($num > 0) {
echo "$num is Positive";
} elseif ($num < 0) {
echo "$num is Negative";
} else {
echo "$num is Zero";
}
?>
<?php
for ($i = 2; $i <= 20; $i += 2) {
echo "$i ";
}
?>
6. (b) While Loop: Sum of First 10 Natural Numbers
<?php
$sum = 0;
$i = 1;
while ($i <= 10) {
$sum += $i;
$i++;
}
echo "Sum of first 10 natural numbers is: $sum";
?>
<?php
$num = 5;
$i = 1;
do {
echo "$num x $i = " . ($num * $i) . "<br>";
$i++;
} while ($i <= 10);
?>
<?php
$students = array("John", "Alice", "Mike", "Sara", "Tom");
foreach ($students as $student) {
echo "$student <br>";
}
?>
<?php
$marks = array("John" => 85, "Alice" => 90, "Mike" => 78);
foreach ($marks as $name => $score) {
echo "$name scored $score marks.<br>";
}
?>
<?php
$students = array(
array("John", 85, "A"),
array("Alice", 90, "A+"),
array("Mike", 78, "B")
);
foreach ($students as $student) {
echo "Name: " . $student[0] . ", Marks: " . $student[1] . ", Grade: " .
$student[2] . "<br>";
}
?>
8. Sorting an Array
<?php
$numbers = array(5, 2, 8, 1, 3);
sort($numbers);
echo "Sorted numbers: ";
foreach ($numbers as $num) {
echo "$num ";
}
?>
<?php
$numbers = array(10, 20, 5, 40, 30);
$max = max($numbers);
$min = min($numbers);
echo "Maximum value: $max <br>";
echo "Minimum value: $min";
?>