Php Lab Manual
Php Lab Manual
<?php
// Function to calculate the area of a triangle
function calculateTriangleArea($base, $height)
{
return 0.5 * $base * $height;
}
<?php
// Using single quotes
$string1 = 'Hello';
$string2 = 'World';
$concatenatedString1 = $string1 . ' ' . $string2;
//(ii)Strings as variables
<?php
// Defining string variables
$firstName = 'John';
$lastName = 'Doe';
<?php
$greeting = 'Hello';
$name = 'Alice';
$punctuation = '!';
(vi) Strings and string variables containing single quotes as part string
contents
<?php
$quote = "It's a beautiful day!";
$author = 'John';
<?php
function checkNumber($number)
{
// Check if the number is odd or even
if ($number % 2 == 0)
{
$oddEven = "even";
}
else
{
$oddEven = "odd";
}
Output:
<?php
// Function to calculate the roots of a quadratic equation
case 1:
If you input
a=1,
b=−3,
c=2
case 2:
If you input
a=1,
b=2,
c=1:
case 3:
If you input
a=1,
b=1,
c=1:
<?php
function sqrtt($num, $precision = 0.0001)
{
$x = $num;
$y = 1;
while ($x - $y > $precision)
{
$x = ($x + $y) / 2;
$y = $num / $x;
}
return round ($x, 4);
}
// Example usage
$number = 101;
$result = sqrtt($number);
echo "The square root of $number is: $result";
?>
<?php
function generateFloydsTriangle($rows)
{
$number = 1; // Starting number
for ($i = 1; $i <= $rows; $i++)
{
// Print each row
for ($j = 1; $j <= $i; $j++)
{
echo $number . " ";
$number++; // Increment the number for the next position
}
echo "\n"; // New line after each row
}
}
?>
<?php
/**
* Function to calculate the mean of an array
*
* @param array $numbers
* @return float
*/
function calculateMean(array $numbers): float {
$count = count($numbers);
if ($count === 0) {
return 0; // Avoid division by zero
}
$sum = array_sum($numbers);
return $sum / $count;
}
/**
* Function to calculate the standard deviation of an array
*
* @param array $numbers
* @return float
*/
function calculateStandardDeviation(array $numbers): float
{
$mean = calculateMean($numbers);
$sumOfSquares = 0;
$count = count($numbers);
if ($count <= 1)
{
return 0; // Standard deviation is not defined for 0 or 1 element
}
?>
return $histogram;
}
// Example usage
$scores = [85, 92, 77, 68, 92, 88, 75, 82, 91, 70, 65, 88, 95, 72, 78, 90, 85, 80, 75,
88];
$histogram = createHistogram($scores);
echo "Histogram:\n";
for ($i = 0; $i < 10; $i++) {
echo sprintf("%2d-%2d: %d\n", $i * 10, $i * 10 + 9, $histogram[$i]);
}
echo "90-100: " . $histogram[10] . "\n";
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
<?php
// Function to calculate Fibonacci number using recursion
function fibonacci($n) {
// Base cases
if ($n <= 0) {
return 0; // Fibonacci of 0 is 0
} elseif ($n == 1) {
return 1; // Fibonacci of 1 is 1
} else {
// Recursive case
return fibonacci($n - 1) + fibonacci($n - 2);
}
}
// Example usage
$numberOfTerms = 10; // Change this value to generate more or fewer terms
printFibonacciSeries($numberOfTerms);
?>
<?php
// Function to print the first N lines of a file
// Example usage
$filename = "ple.txt";
$N = 5; // Number of lines to print
// Example usage
$filename =
C:\Users\AIML\Desktop\MY_PRAC_PHP\php_lab_programs\ple.txt"; // Specify
the path to your file
$word = "am"; // Replace with the word you want to search for
countWordFrequency ($filename, $word);
?>
/*Develop a PHP program to filter the elements of an array with key names.
Sample Input Data:
1st array: ('c1' => 'Red', 'c2' => 'Green', 'c3' => 'White', c4 => 'Black')
2nd array: ('c2', 'c4') */
<?php
?>
2.Function Definition:
4.Filtering the Array: We call the filterArrayByKeys function with the $colors
array and the $keysToFilter, storing the result in $filteredColors.
5.Output: Finally, we print the filtered array using print_r, which displays the
filtered colors.
<?php
Class Employee
{
// Properties
public $Emp_Name;
public $Emp_ID;
public $Emp_Dept;
public $Emp_Salary;
public $Emp_DOJ;
11. Develop
a. PHP program to count the occurrences of Aadhaar numbers present in a
text.
b. Develop a PHP program to find the occurrences of a given pattern and
replace them with a text.
11a.
<?php
function countAadhaarOccurrences($text) {
// Regular expression to match valid Aadhaar numbers
$pattern = "/\b[2-9][0-9]{3}\s[0-9]{4}\s[0-9]{4}\b/";
// Count occurrences
return count($matches[0]);
}
// Example usage
$text = "Here are some Aadhaar numbers: 2347 5678 9412, 2345 6789 4333,
2256 7890 124";
$count = countAadhaarOccurrences($text);
echo "Number of valid Aadhaar numbers found: " . $count;
?>
<?php
// Define the input string
$string = "\n The quick brown fox jumps over the lazy dog. The fox is clever.\n
\n ";
// Replace all occurrences of the find value with the replace text
$new_string = str_replace($find, $replace, $string);
<html>
<body>
<input type="submit">
</form>
</body>
</html>
<html>
<body>
</body>
</html>