PHP Assignment
PHP Assignment
1. Answer
<?php
$a = 42;
$b = 20;
$c = $a + $b;
$c = $a - $b;
$c = $a * $b;
$c = $a / $b;
$c = $a % $b;
$c = $a++;
$c = $a--;
?>
2. Answer
<?php
$a=20;
$b=10;
$c=1;
if($a>$b && $a>$c)
{
echo "Greater value is a=".$a;
}
else if($b>$a && $b>$c)
{
echo "Greater value is b=".$b;
}
else if($c>$a && $c>$b)
{
echo "Greater value is c=".$c;
}
else
{
echo"Dont Enter Equal Values";
}
?>
3. Answer
<?php
function result($N)
if ($num % 3 == 0 or $num % 5 == 0)
$N = 20;
result($N);
?>
4. Answer
<?php
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach ($numbers as $index=>$value) {
if ($value % 2 == 0)
echo "$value <br>";
}
?>
-----------------------------------------------------------------------------------
<?php
echo 'The even number upto 35 are : ';
for($i=7; $i<=35; $i++)
{
if($i%2 == 0)
{
echo '<br>'.$i;
}
?>
5. Answer
<?php
function result($N)
{
for ($num = 2; $num < $N; $num++)
{
// Short-circuit operator is used
if ($num % 2 == 0 && $num % 5 == 0)
echo $num, " ";
}
}
$N = 50;
result($N);
?>
6. Answer
<?php
function reverse_number($number)
{
$snum = (string) $number;
$revstr = strrev($snum);
return $reverse;
}
echo reverse_number(12345);
?>
7. Answer
<?php
$x = 42;
$y = 70;
$lcm = ($x*$y)/$gcd;
echo "LCM of $x and $y is: $lcm";
?>
9. Answer
echo "<tr/>";
}
?>
</table>
10. Answer
<?php
function checkPrime($num)
{
if ($num == 1)
return 0;
for ($i = 2; $i <= $num/2; $i++)
{
if ($num % $i == 0)
return 0;
}
return 1;
}
<?php
$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>