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

WT Practiclas

The document contains 19 programs written in PHP with varying objectives like printing output, using variables, arrays, operators, conditional statements, and functions. Each program has the objective, PHP code, and expected output.
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)
38 views16 pages

WT Practiclas

The document contains 19 programs written in PHP with varying objectives like printing output, using variables, arrays, operators, conditional statements, and functions. Each program has the objective, PHP code, and expected output.
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/ 16

Program 1

Object: Write a program in PHP to print “Hello World” using echo.


<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Output:
Hello World!
Program 2
Object : Write a program in PHP to declare and print various variables.
<?php
$str = “Hello”;
$X = 50;
$Y = 44.3;
echo “String is: $str <br>”;
echo “Integer is: $X <br>”;
echo “Float is: $Y”;
?>
Output:
String is: Hello
Integer is: 50
Float is: 44.3
Program 3
Object: Write a program in PHP to define constants using define() and const.
<?php
define("MSG", "Hello PHP constant", True);
const MESSAGE = "Const in PHP";
echo MSG , "<br>";
echo MESSAGE, "<br>";;
echo msg;
?>
Output:
Hello PHP constant
Const in PHP
Hello PHP constant
Program 4
Object: Write a program in PHP to define an array and print it using var_dump().
<?php
$Language = array(“php”,”java”,”html”,”css”);
var_dump($Language);
echo "<br>";
$Marks = array(98,72,56,87,93);
var_dump($Marks);
?>
Output:
array(4) { [0]=> string(3) "php" [1]=> string(4) "java" [2]=> string(4) "html" [3]=>
string(3) "css" }
array(5) { [0]=> int(98) [1]=> int(72) [2]=> int(56) [3]=> int(87) [4]=> int(93) }
Program 5
Object: Write a program in PHP to perform various arithmetic operations.
<?php
$x= 10;
$y= 5;
echo "Addition: ",$x+$y, "<br>";
echo "Subtraction: ",$x-$y, "<br>";
echo "Multiplication: ",$x*$y, "<br>";
echo "Division: ",$x/$y, "<br>";
echo "Modulous: ",$x%$y, "<br>";
echo "Exponential: ",$x**$y, "<br>";
?>
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulous: 0
Exponential: 100000

Program 6
Object: Write a program in PHP to perform various assignment operation.
<?php
$x= 10;
$y= $x;
echo "Value of y is: ",$y,"<br>";
$x += $y;
echo "Value of x is: ",$x,"<br>";
$x -= $y;
echo $x,"<br>";
$x /= $y;
echo $x,"<br>";
?>
Output:
Value of y is: 10
Value of x is: 20
10
1

Program 7
Object: Write a program in PHP to perform comparison operation.
<?php
$x = 100;
$y = "100";
var_dump($x == $y); // returns true because values are equal
echo “<br>”;
$a = 100;
$b = "100";
var_dump($a === $b); // returns false because types are not equal
echo “<br>”;
$c = 100;
$d = "100";
var_dump($c != $d); // returns false because values are equal
?>
Output:
bool(true)
bool(false)
bool(false)
Program 8
Object: Write a program in PHP to perform increment/decrement operation.
<?php
$x = 10;
echo ++$x;
echo "<br>";
echo $x++;
echo "<br>";
echo $x;
echo "<br>";
echo --$x;
echo "<br>";
echo $x--;
?>
Output:
11
11
12
11
11
Program 9
Object: Write a program in PHP to perform logical operation.
<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50) {
echo "Hello world!";
}
if ($x == 100 or $y == 80) {
echo "Hello world!";
}
if ($x == 100 xor $y == 50) {
echo "Hello world!"; //no output
}
if (!($x == 90)) {
echo "Hello world!";
}
?>
Output:
Hello world!
Hello world!
Hello world!
Program 10
Object: Write a program in PHP to perform string operation.
<?php
$txt1 = "Good";
$txt2 = " Morning!";
echo $txt1 . $txt2;
echo "<br>";
$str1 = "Hello";
$str2 = "Everyone";
$str1 .= $str2;
echo $str1;
?>
Output:
Good Morning!
HelloEveryone
Program 11
Object: Write a program in PHP to perform array operation.
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x + $y); // union of $x and $y
echo"<br>";
var_dump($x == $y);
echo"<br>";
var_dump($x != $y);
echo"<br>";
var_dump($x !== $y);
echo"<br>";
$arr1 = array("a" => "red", "b" => "green");
$arr2 = array("a" => "red", "b" => "green");
var_dump($arr1 === $arr2);
?>
Output:
array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4)
"blue" ["d"]=> string(6) "yellow" }
bool(false)
bool(true)
bool(true)
bool(true)
Program 12
Object: Write a program in PHP to check a number is even or odd using
conditional operator.
<?php
$x=12;
$y=15;
echo ($x%2==0) ? "even" : "odd";
echo("<br>");
echo ($y%2==0) ? "even" : "odd";
echo("<br>");
echo ($x%2!==0) ? "odd" : "even";
echo("<br>");
echo ($y%2!==0) ? "odd" : "even";
?>
Output:
even
odd
even
odd
Program 13
Object: Write a program in PHP to check whether a number is negative, positive or
zero using if else.
<?php
$num = 19;
if (!is_numeric($num))
echo "Enter a valid number";
elseif ($num > 0)
echo "The entered number $num is positive";
elseif ($num < 0)
echo "The entered number $num is negative";
else {
echo "The entered number $num is zero";
}
echo "<br>";
$n = A;
if (!is_numeric($n))
echo "Enter a valid number";
elseif ($n > 0)
echo "The entered number $n is positive";
elseif ($n < 0)
echo "The entered number $n is negative";
else {
echo "The entered number $n is zero";
}
?>
Output:
The entered number 19 is positive
Enter a valid number
Program 14
Object: Write a program in PHP to check student grade based on marks using if-
else statements.
<?php
$marks = 40;
if ($marks>=60)
{
$grade = "First Division";
}
else if($marks>=45)
{
$grade = "Second Division";
}
else if($marks>=33)
{
$grade = "Third Division";
}
else
{
$grade = "Fail";
}
echo "Student grade: $grade";
?>
Output:
Student grade: Third Division
Program 15
Object: Write a program in PHP to check whether a year is leap year or not.
<?php
function year_check($my_year){
if ($my_year % 400 == 0)
print("It is a leap year");
else if ($my_year % 100 == 0)
print("It is not a leap year");
else if ($my_year % 4 == 0)
print("It is a leap year");
else
print("It is not a leap year");
}
$my_year = 2016;
year_check($my_year);
?>
Output:
It is a leap year
Program 16
Object: Write a program in PHP and take angles of a triangle and check whether
Triangle is valid or not.
<?php
// Function to check if sum of the three angles is 180 or not
function Valid($a, $b, $c){
// Check condition
if ($a + $b + $c == 180 && $a != 0 && $b != 0 && $c != 0)
return true;
else
return false;
}
$a = 60;
$b = 40;
$c = 80;
if (Valid($a, $b, $c))
echo "Valid triangle";
else
echo "Invalid triangle";
?>
Output:
Valid triangle.
Program 17
Object: Write a program in PHP to show day of week using switch.
<?php
$day = 7;
switch($day){
case 1: echo ("Monday");
break;
case 2: echo ("Tuesday");
break;
case 3: echo ("Wednesday");
break;
case 4: echo ("Thursday");
break;
case 5: echo ("Friday");
break;
case 6: echo ("Saturday");
break;
case 7: echo ("Sunday");
break;
default: echo ("Enter a valid Number");
}
?>
Output:
Sunday
Program 18
Object: Write a program in PHP to check whether given number is palindrome or
not.
<?php
function palindrome($n){
$number = $n;
$sum = 0;
while(floor($number)) {
$rem = $number % 10;
$sum = $sum * 10 + $rem;
$number = $number/10;
}
return $sum;
}
$input = 1235321;
$num = palindrome($input);
if($input==$num){
echo "$input is a Palindrome number";
} else {
echo "$input is not a Palindrome";
}
?>
Output:
1235321 is a Palindrome number
Program 19
Object: Write a PHP program using functions create two function sum() and sub().
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
function sub($x, $y) {
$z = $x - $y;
return $z;
}
echo "10 + 5 = " . sum(10, 5) . "<br>";
echo "13 + 7 = " . sum(13, 7) . "<br>";
echo "10 - 5 = " . sub(10, 5) . "<br>";
echo "13 - 7 = " . sub(13, 7) . "<br>";
?>
Output:
10 + 5 = 15
13 + 7 = 20
10 - 5 = 5
13 - 7 = 6
Program 20
Object: Write a PHP program to swap two numbers with and without using third
Variable.
<?php
// Swapping using third variable
$a = 45;
$b = 78;
$third = $a;
$a = $b;
$b = $third;
echo "After swapping:<br>";
echo "a =".$a." b=".$b ,"</br>";
// Swapping without using third variable
$x=234;
$y=345;
$x=$x+$by;
$y=$x-$y;
$x=$x-$y;
echo "Value of x: $x</br>";
echo "Value of y: $y</br>";
?>
Output:
After swapping:
a =78 b=45
Value of x: 345
Value of y: 234

You might also like