WT Practiclas
WT Practiclas
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