0% found this document useful (0 votes)
13 views

PHP 6-10 Lab Program

Uploaded by

mageshkumar6936
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

PHP 6-10 Lab Program

Uploaded by

mageshkumar6936
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

6.

Create a script using a for loop to add all the integers between 0
and 30 and display the total.

<?php

// Initialize sum variable

$sum = 0;

// Loop through numbers from 1 to 30

for($x=1; $x<=30; $x++)

// Add current number to the sum

$sum += $x;

// Print the sum of numbers from 1 to 30

echo "The sum of the numbers 0 to 30 is $sum"."\n";

?>
7. Write a PHP script using nested for loop that creates a chess board.

<!DOCTYPE html>

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<h3>Chess Board using Nested For Loop</h3>

<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">

<!-- cell 270px wide (8 columns x 60px) -->

<?php
8. Write a PHP function that checks if a string is all lower case.

// Outer loop for rows

for($row=1; $row<=8; $row++)

echo "<tr>"; // Start a new table row

// Inner loop for columns

for($col=1; $col<=8; $col++)

// Calculate total sum of row and column indices

$total = $row + $col;

// Check if total is even or odd to determine cell color

if($total % 2 == 0)

// If total is even, set cell background color to white

echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";

else

// If total is odd, set cell background color to black

echo "<td height=30px width=30px bgcolor=#000000></td>";

echo "</tr>"; // End the table row

?>

</table>
</body>

</html>

<?php

// Define a function to check if a string is all lowercase

function is_str_lowercase($str1)

// Iterate through each character in the string

for ($sc = 0; $sc < strlen($str1); $sc++) {

// Check if the character's ASCII value is within the uppercase range

if (ord($str1[$sc]) >= ord('A') &&

ord($str1[$sc]) <= ord('Z')) {

return false; // If any uppercase character is found, return false

return true; // If no uppercase characters are found, return true

// Test the function with sample strings and display the results

var_dump(is_str_lowercase('abc def ghi')); // Output: bool(true) (all lowercase)

var_dump(is_str_lowercase('abc dEf ghi')); // Output: bool(false) (uppercase 'E' found)

?>
9. Write a PHP script to calculate the difference between two dates.

<?php

// Start date

$sdate = "1981-11-04";

// End date

$edate = "2013-09-04";

// Calculate the difference between the end date and the start date in seconds

$date_diff = abs(strtotime($edate) - strtotime($sdate));

// Calculate the number of years in the difference

$years = floor($date_diff / (365*60*60*24));

// Calculate the number of months in the remaining difference

$months = floor(($date_diff - $years * 365*60*60*24) / (30*60*60*24));

// Calculate the number of days in the remaining difference

$days = floor(($date_diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

// Output the calculated duration

printf("%d years, %d months, %d days", $years, $months, $days);

// Print a newline character for formatting

printf("\n");

?>
10. Write a PHP script to display time in a specified time zone

<?php

// Set the default timezone

date_default_timezone_set('Asia/Kolkata');

echo "Current date and time is :";

// Create timezone object

$timezone_object = date_default_timezone_get();

$myDate = date("d-m-y h:i:s");

// If timezone object is true

if ($timezone_object) {

echo 'date_default_timezone_set: ' . date_default_timezone_get();

echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');

echo $myDate;

?>

You might also like