0% found this document useful (0 votes)
16 views8 pages

Practical No 05 PHP

The document contains multiple PHP code snippets demonstrating various functions, including string functions, math functions, and date/time functions. Each section provides examples of how to use these functions effectively, showcasing operations like formatting numbers, manipulating strings, performing mathematical calculations, and handling dates. Overall, it serves as a practical guide for understanding and applying PHP functions in programming.

Uploaded by

zubiyaansari417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Practical No 05 PHP

The document contains multiple PHP code snippets demonstrating various functions, including string functions, math functions, and date/time functions. Each section provides examples of how to use these functions effectively, showcasing operations like formatting numbers, manipulating strings, performing mathematical calculations, and handling dates. Overall, it serves as a practical guide for understanding and applying PHP functions in programming.

Uploaded by

zubiyaansari417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PRACTICAL NO: 05

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>";

// 1. date() - Format the local date and time


echo "1. Current date (Y-m-d format): " . date("Y-m-d") . "<br>";
echo "2. Current time (H:i:s format): " . date("H:i:s") . "<br>";
echo "3. Full date and time: " . date("l, d F Y H:i:s") . "<br>";
// 2. time() - Get the current timestamp
$currentTimestamp = time();
echo "4. Current Unix timestamp: " . $currentTimestamp . "<br>";

// 3. mktime() - Create a timestamp for a specific date and time


$customTimestamp = mktime(15, 30, 0, 7, 20, 2025);
echo "5. Timestamp for 2025-07-20 15:30:00: " . $customTimestamp . "<br>";
echo "6. Date for the above timestamp: " . date("Y-m-d H:i:s",
$customTimestamp) . "<br>";

// 4. strtotime() - Parse an English textual date into a timestamp


$futureDate = strtotime("+2 weeks");
echo "7. Date after 2 weeks: " . date("Y-m-d", $futureDate) . "<br>";

// 5. checkdate() - Validate a date


$isValidDate = checkdate(2, 29, 2024); // Leap year example
echo "8. Is 2024-02-29 a valid date? " . ($isValidDate ? "Yes" : "No") . "<br>";

// 6. getdate() - Get date/time information as an array


$dateInfo = getdate();
echo "9. Current day of the week: " . $dateInfo['weekday'] . "<br>";
echo "10. Current month: " . $dateInfo['month'] . "<br>";
echo "11. Current day of the month: " . $dateInfo['mday'] . "<br>";

// 7. date_default_timezone_set() - Set the default timezone


date_default_timezone_set("Asia/Kolkata");
echo "12. Current time in Asia/Kolkata timezone: " . date("H:i:s") . "<br>";
// 8. gmdate() - Get the Greenwich Mean Time (GMT/UTC)
echo "13. Current UTC date and time: " . gmdate("Y-m-d H:i:s") . "<br>";

// 9. diff between two dates using DateTime


$start = new DateTime("2025-01-01");
$end = new DateTime("2025-12-31");
$diff = $start->diff($end);
echo "14. Difference between 2025-01-01 and 2025-12-31: " . $diff->days . "
days<br>";

// 10. Formatting a timestamp with strftime()


echo "15. Current date using strftime: " . strftime("%A, %B %d, %Y") . "<br>";
?>

You might also like