Practical No 05 PHP
Practical No 05 PHP
1.
<?php
$num = "20100.3145";
echo number_format($num), "<br>";
$name = 'PHP';
echo "$name is string-oriented";
?>
2.
<?php
$name='PHP';
echo "$name is string-oriented";
?>
3.
<?php
$num = "20100.3145";
echo (int)$num, "<br>";
echo (float)$num, "<br>";
echo (double)$num;
?>
4.
<?php
$num="20100.3145";
echo number_format($num),"<br>";
echo number_format($num,3),"<br>";
?>
5.
<?php
$num = "1300.314";
echo intval($num), "<br>";
echo floatval($num);
?>
6.
<?php
printf("I have %s pens and %s pencils. <br><br>", 4, 18);
$str = sprintf("After using I have %s pens and %s pencils.<br>", 2, 9);
echo $str, "<br>";
$y = 2019;
$m = 11;
$date = 4;
echo "The date is: ";
printf("%04d-%02d-%02d<br>", $y, $m, $date);
?>
7.
All String Functions
<?php
echo "<h1>String Functions in PHP</h1>";
$text = "Hello World! PHP is amazing.";
// 1. str_word_count() - Count the number of words in a string
echo "1. Word count: " . str_word_count($text) . "<br>";
// 2. strlen() - Get the length of a string
echo "2. Length of the string: " . strlen($text) . "<br>";
// 3. strrev() - Reverse a string
echo "3. Reversed string: " . strrev($text) . "<br>";
// 4. strpos() - Find the position of the first occurrence of a substring
echo "4. Position of 'PHP': " . strpos($text, "PHP") . "<br>";
// 5. str_replace() - Replace all occurrences of a substring with another
echo "5. Replace 'amazing' with 'awesome': " . str_replace("amazing",
"awesome", $text) . "<br>";
// 6. ucwords() - Capitalize the first letter of each word
echo "6. Capitalize each word: " . ucwords($text) . "<br>";
// 7. strtoupper() - Convert a string to uppercase
echo "7. Uppercase: " . strtoupper($text) . "<br>";
// 8. strtolower() - Convert a string to lowercase
echo "8. Lowercase: " . strtolower($text) . "<br>";
// 9. str_repeat() - Repeat a string multiple times
echo "9. Repeat 'PHP ': " . str_repeat("PHP ", 3) . "<br>";
// 10. strcmp() - Compare two strings
echo "10. Compare 'PHP' and 'PHP': " . strcmp("PHP", "PHP") . "<br>";
echo "Compare 'PHP' and 'php': " . strcmp("PHP", "php") . "<br>";
// 11. substr() - Extract a substring
echo "11. Substring (6 to 11): " . substr($text, 6, 5) . "<br>";
// 12. str_split() - Split a string into an array
$splitted = str_split($text, 5);
echo "12. Split string into chunks of 5: ";
print_r($splitted);
echo "<br>";
// 13. str_shuffle() - Randomly shuffle the string
echo "13. Shuffled string: " . str_shuffle($text) . "<br>";
// 14. trim() - Remove whitespace from the beginning and end of a string
$extraSpaces = " Trim me! ";
echo "14. Trimmed string: '" . trim($extraSpaces) . "'<br>";
// 15. rtrim() - Remove whitespace from the right end of a string
echo "15. Rtrim result: '" . rtrim($extraSpaces) . "'<br>";
// 16. ltrim() - Remove whitespace from the left end of a string
echo "16. Ltrim result: '" . ltrim($extraSpaces) . "'<br>";
// 17. chop() - Alias of rtrim()
echo "17. Chop result: '" . chop($extraSpaces) . "'<br>";
// 18. chunk_split() - Split a string into chunks
echo "18. Chunked string (every 4 characters):<br>" . chunk_split($text, 4,
"-") . "<br>";?>
8.
<?php
echo "<h1>Math Functions in PHP</h1>";
$num = -15.67;
// 1. abs() - Absolute value of a number
echo "1. Absolute value of $num: " . abs($num) . "<br>";
// 2. ceil() - Round up to the nearest integer
echo "2. Ceiling of $num: " . ceil($num) . "<br>";
// 3. floor() - Round down to the nearest integer
echo "3. Floor of $num: " . floor($num) . "<br>";
// 4. round() - Rounds a number to the nearest integer
echo "4. Round $num: " . round($num) . "<br>";
// 5. sqrt() - Square root of a number
echo "5. Square root of 16: " . sqrt(16) . "<br>";
// 6. pow() - Raise a number to the power of another
echo "6. 2 raised to the power of 3: " . pow(2, 3) . "<br>";
// 7. max() - Find the maximum value in a set
echo "7. Maximum value (3, 7, 12, 5): " . max(3, 7, 12, 5) . "<br>";
// 8. min() - Find the minimum value in a set
echo "8. Minimum value (3, 7, 12, 5): " . min(3, 7, 12, 5) . "<br>";
// 9. rand() - Generate a random number
echo "9. Random number (1-100): " . rand(1, 100) . "<br>";
// 10. pi() - Get the value of π (pi)
echo "10. Value of pi: " . pi() . "<br>";
// 11. exp() - Exponential (e^x)
echo "11. e^3: " . exp(3) . "<br>";
// 12. log() - Natural logarithm (base e)
echo "12. Natural logarithm of 20: " . log(20) . "<br>";
// 13. log10() - Base-10 logarithm
echo "13. Log base 10 of 100: " . log10(100) . "<br>";
// 14. sin() - Sine of a number (angle in radians)
echo "14. Sine of π/2: " . sin(pi() / 2) . "<br>";
// 15. cos() - Cosine of a number (angle in radians)
echo "15. Cosine of 0: " . cos(0) . "<br>";
// 16. tan() - Tangent of a number (angle in radians)
echo "16. Tangent of π/4: " . tan(pi() / 4) . "<br>";
// 17. deg2rad() - Convert degrees to radians
echo "17. Convert 90 degrees to radians: " . deg2rad(90) . "<br>";
// 18. rad2deg() - Convert radians to degrees
echo "18. Convert π radians to degrees: " . rad2deg(pi()) . "<br>";
// 19. hypot() - Calculate the length of the hypotenuse
echo "19. Hypotenuse of a right triangle with sides 3 and 4: " . hypot(3, 4) .
"<br>";
// 20. fmod() - Returns the remainder of division
echo "20. Remainder of 10 divided by 3: " . fmod(10, 3) . "<br>";
?>
9.
<?php
echo "<h1>Date and Time Functions in PHP</h1>";