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

Practical PHP

The document contains 7 practical assignments on PHP programming concepts: 1. Write programs to determine if a number is even or odd using switch, if, and if/else statements. 2. Write programs to print the first 30 even numbers using for, while, and do/while loops. 3. Use an indexed array to store and display string values. 4. Write programs to calculate string length, count words in a string, and find percentage using a function. 5. Demonstrate a parameterized function. 6. Filter an array to return even numbers using an anonymous function. 7. Generate a PDF file using the FPDF library displaying sample text.

Uploaded by

Shubhangi Dighe
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)
13 views

Practical PHP

The document contains 7 practical assignments on PHP programming concepts: 1. Write programs to determine if a number is even or odd using switch, if, and if/else statements. 2. Write programs to print the first 30 even numbers using for, while, and do/while loops. 3. Use an indexed array to store and display string values. 4. Write programs to calculate string length, count words in a string, and find percentage using a function. 5. Demonstrate a parameterized function. 6. Filter an array to return even numbers using an anonymous function. 7. Generate a PDF file using the FPDF library displaying sample text.

Uploaded by

Shubhangi Dighe
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/ 5

Practical No: 2

Write a program to find given number is even or odd using switch statement
<?php
$num = 40;
switch ($num % 2)
{
case 0:
echo $num." is an even number.";
break;
case 1:
echo $num." is an odd number.";
break;
default:
echo "Invalid input";
}

?>

Write a program to find given number is even or odd using if statement


$number = 20;
if ($number % 2 == 0) {
print "It's even";
}

Write a program to find given number is even or odd using if else statement

$number = 40;
if ($number % 2 == 0) {
print "It's even";
}
else{
print "It's Odd";
}

Practical No: 3

Write a program to print first 30 even numbers. (Using for, while, do..while)
Using for:

<?php

$end=50;
$even= "Even Numbers Are : ";
$odd="<br /> Odd Numbers Are : ";

for($i=1;$i<=$end;$i++)
{
if($i%2==0)
{
$even.=$i.",";
}else $odd.=$i.",";
}
echo $even.$odd;

?>
Using while:

<?php
$i=0;
while($i <= 10){
if($i % 2 == 0){
echo $i." - Even, ";
}else{
echo $i." - Odd, ";
}
$i++;
}
?>

Using do while:
<?php
$even=””;
$odd=””;
$i=1;

do
{
if($i%2==0)
{
$even = $even . ” ” .$i ;
}
else
{
$odd = $odd . ” ” .$i;
}$i++;
}
while( $i<=20 );
echo “The Even no = ” . $even.”<br/>”;
echo “The Odd no =”. $odd;

?>

Practical No: 4

Develop a program to using Indexed array.


<?php
$size[0]="Big";
$size[1]="Medium";
$size[2]="Short";
echo "Size: $size[0], $size[1] and $size[2]";
?>
Practical No: 5

Write a PHP program to- Calculate length of string.


<?php
$str = 'Javatpoint';
echo "Your String is:".$str;
echo "<br>";
echo "By using 'strlen()' function:".strlen($str); // 10
?>

Count the number of words in string without using string functions.


$wordcount = 0;
for($i=0; $i < strlen($str); $i++){
if($str[$i]!=' ')
{
$wordcount++;
while($str[$i] !=' '){
$i++;
}
}
echo "Total word count is".' '. $wordcount;
<?php
$maxmarks=300;
$percent=function ($marks) use ($maxmarks) {return
$marks*100/$maxmarks;};
echo "marks=285 percentage=". $percent(285);
?>

Practical No: 06

to filter an array of numbers and return only the even numbers:

$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];


$evenNumbers = array_filter($numbers, function($number) {
return $number % 2 == 0;
});
print_r($evenNumbers);

// Output: Array ( [1] => 2 [3] => 4 [5] => 6 [7] => 8 )

Write a program to demonstrate parameterized function

<?php
function Number($Number)
{
echo "Phone Number is $Number"."<br/>";
}
Number("123223");
Number("234324");
Number("345435");
?>
Practical No: 07

<?php

ob_end_clean();
require('fpdf/fpdf.php');

// Instantiate and use the FPDF class


$pdf = new FPDF();

//Add a new page


$pdf->AddPage();

// Set the font for the text


$pdf->SetFont('Arial', 'B', 18);

// Prints a cell with given text


$pdf->Cell(60,20,'Hello GeeksforGeeks!');

// return the generated output


$pdf->Output();

?>

You might also like