0% found this document useful (0 votes)
38 views10 pages

WT Prac2

The document provides instructions and examples for performing basic PHP programs using operators, conditions, loops, and date/time functions. It includes 8 exercises: 1. Describing PHP functions like echo(), print(), phpinfo(), etc. 2. Demonstrating different ways to write PHP code with or without HTML. 3. Writing a program to display greeting messages based on the time of day. 4. Writing a function to return the number of days in a given month. 5. Writing a program to calculate the sum of the first 100 odd numbers. 6. Writing a program to list prime numbers within a given range. 7. Writing programs to generate Fibonacci

Uploaded by

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

WT Prac2

The document provides instructions and examples for performing basic PHP programs using operators, conditions, loops, and date/time functions. It includes 8 exercises: 1. Describing PHP functions like echo(), print(), phpinfo(), etc. 2. Demonstrating different ways to write PHP code with or without HTML. 3. Writing a program to display greeting messages based on the time of day. 4. Writing a function to return the number of days in a given month. 5. Writing a program to calculate the sum of the first 100 odd numbers. 6. Writing a program to list prime numbers within a given range. 7. Writing programs to generate Fibonacci

Uploaded by

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

GANPAT UNIVERSITY

U.V. PATEL COLLEGE OF ENGINEERING


B.Tech 6th Semester CE/IT
2CEIT6PE1: Web Technology
Practical-2

Aim: Perform basic programs of PHP using operator, condition, Loop, Date
& Time functions.

Exercise:
1. Study the following functions & write a description for each:
echo(), print(), phpinfo(), define(), var_dump(), date(), Time()
echo():
Description: echo() is a language construct in PHP used to output one or more strings. It
doesn't have a return value. It's often used to output HTML or other content dynamically
within PHP scripts.
Example:
$name = "Vriddhi Shah";
echo "Hello, $name!";
print():
Description: print() is a language construct in PHP used to output a string. It's similar to
echo(), but it returns 1, so it can be used in expressions. Like echo(), it's commonly used
for outputting content.
Example:
$name = "Vriddhi Shah";
print "Hello, $name!";
phpinfo():
Description: phpinfo() is a function that outputs information about PHP's configuration.
This can be useful for debugging purposes or when checking server settings. It displays
information such as PHP version, configuration options, and loaded modules.
Example:
phpinfo();
define():
Description: define() is used to define a constant in PHP. Constants are like variables, but
once they are defined, they cannot be changed or undefined later in the script. They
provide a way to store values that don't change throughout the script's execution.
Example:
define("PI", 3.14);
var_dump():
Description: var_dump() is a debugging function in PHP used to display structured
information (type and value) about one or more variables. It's commonly used to inspect
variables and their contents during development.
Example:
<?php
$array = [1, 2, 3];
var_dump($array);
?>

date():
Description: date() is a function in PHP used to format and display the current date and
time according to a specified format. It's commonly used for tasks involving date and
time manipulation.
Example:
<?php
date_default_timezone_set('Asia/Kolkata');
echo date("Y-m-d H:i:s");
?>

time():
Description: time() is a function in PHP that returns the current Unix timestamp, which
represents the current date and time as the number of seconds since the Unix Epoch
(January 1 1970 00:00:00 GMT).
Example:
echo time();

2. Demonstrate different ways to write a PHP code.


1. Without any HTML markups
2. Embedding HTML markups in PHP code
3. Embedding PHP code in HTML

PHP Code without HTML Markups:


Code:
<?php
$name = "Vriddhi Shah";
echo "Hello, $name!";
?>
Output:

Embedding HTML Markups in PHP Code:


Code:
<?php
echo "<html>";
echo "<h1> welcome </h1>" ;
echo "</html>" ;
?>

Output:

Embedding PHP Code in HTML:


Code:
<html>
<head>
<title>Greetings</title>
</head>
<body>
<h1>Hello, <?php echo "John"; ?>!</h1>
</body>
</html>
Output:
3. Write a program that displays a different message based on time of day.
[Note: For example, a page should display “Good Morning” if it is accessed in the
morning.]
Code:
<?php
date_default_timezone_set('Asia/Kolkata');
echo "Current time:".date("H:i:s")."<br>";
$current_hour = date("H");

if ($current_hour >= 24 && $current_hour < 12) {


$message = "Good Morning";
} elseif ($current_hour >= 12 && $current_hour < 16) {
$message = "Good Afternoon";
} elseif ($current_hour >= 16 && $current_hour < 18) {
$message = "Good Evening";
} else {
$message = "Good Night";
}
echo "Hello, ".$message. "!!";
?>
Output:

4. Write a PHP function daysInMonth() that takes a month (between 1 and 12) as a
parameter and returns the number of days in that month in a non-leap year.
[For example daysInMonth(6) should return 30, because June has 30 days.]
Code:
<?php
function daysInMonth($month) {
$days_in_month = [
1 => 31,
2 => 28,
3 => 31,
4 => 30,
5 => 31,
6 => 30,
7 => 31,
8 => 31,
9 => 30,
10 => 31,
11 => 30,
12 => 31
];
return $days_in_month[$month];
}
echo "Number of days in April: " . daysInMonth(4);
?>
Output:

5. Write a PHP program to make the sum of the first 100 odd numbers.
Code:
<?php
$sum = 0;
$count = 0;
$number = 1;
while ($count < 100) {
if ($number % 2 != 0) {
$sum += $number;
$count++;
}
$number++;
}
echo "The sum of the first {$count} odd numbers is: $sum";
?>
Output:

6. Write a PHP program to list out Prime numbers in given range.


Code:
<?php
echo "Prime number in the range 1 to 10 are :</br>";
for($i=1;$i<=10;$i++){
if($i==1) continue;
$flag=0;
for($j=2;$j<$i;$j++){
if($i%$j == 0){
$flag=1;
break;
}
}
if($flag==0) echo "The prime no is : {$i}</br>";
}
?>
Output:

7. Write a PHP program to print fibonacci series with and without using recursion and
check which method is efficient.
[Note: microtime() function is an inbuilt function in PHP which is used to return the
current Unix timestamp with microseconds.]
Code:
<?php
function Fibowithoutrecursion($lastval){
$series=array();
$num1=0;
$num2=1;
for ($i = 0; $i < $lastval; $i++) {
$series[] = $num1;
$temp = $num1 + $num2;
$num1 = $num2;
$num2 = $temp;
}
echo "The {$lastval} fibonnaci series is : <br>";
foreach( $series as $value ) {
echo "{$value} ";
}
}
$start=microtime(TRUE);
Fibowithoutrecursion(40);
$end=microtime(TRUE);
echo "<br>Time taken to execute the program without recursion: ".($end-$start)."
seconds";
function Fibowithrecursion($lastval){
if ($lastval == 0) {
return 0;
}
if ($lastval == 1) {
return 1;
}
return Fibowithrecursion($lastval - 1) + Fibowithrecursion($lastval - 2);
}
$lastval=40;
$start=microtime(TRUE);
echo "<br>The {$lastval} fibonnaci series is : <br>" ;
for ($i = 0; $i < $lastval; $i++) {
echo Fibowithrecursion($i) . " ";
}
$end=microtime(TRUE);
echo "<br>Time taken to execute the program without recursion: ".($end-$start)."
seconds";
?>
Output:

8. Write a PHP program to enter the numbers of rows and columns and in the next page
generate the table with given rows and cols.
Code:
<?php
$r=5;
$c=3;
echo "<table border=1 width=40% height=30%>";
for($i=0;$i<$r;$i++){
echo "<tr>";
for($j=0;$j<$c;$j++) {
echo "<td></td>";
}
echo "</tr>";
}
echo "</table>";
?>
Output:
9. Write a PHP program to print table of a
number. [For example: 9 * 1 = 9
9 * 2 = 18 ….]
Code:
<?php
function printMultiplicationTable($number) {
echo "Multiplication table for $number:<br>";
for ($i = 1; $i <= 10; $i++) {
echo "$number * $i = " . ($number * $i) . "<br>";
}
}
$number = 9;
printMultiplicationTable($number);
?>
Output:

You might also like