PHP Lab Programms
PHP Lab Programms
PHP Lab Programms
com)
1.Create a simple HTML form and accept the user name and display the name
through PHP echo statement.
Sample Solution: -
PHP Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method='POST'>
<h2>Please input your name:</h2>
<input type="text" name="name">
<input type="submit" value="Submit Name">
</form>
<?php
//Retrieve name from query string and store to a local variable
$name = $_POST['name'];
echo "<h3> Hello $name </h3>";
?>
</body>
</html>
Copy
Sample Output:
Copy
Sample Output:
There are 5 lines in a6924e70-5a4c-11e7-b47b-99347412a245.php
3.Write a PHP function to test whether a number is greater than 30, 20 or 10
using ternary operator.
Sample Solution: -
PHP Code:
<?php
function trinary_Test($n){
$r = $n > 30
? "greater than 30"
: ($n > 20
? "greater than 20"
: ($n >10
? "greater than 10"
: "Input a number atleast greater than 10!"));
echo $n." : ".$r."\n";
}
trinary_Test(32);
trinary_Test(21);
trinary_Test(12);
trinary_Test(4);
?>
Copy
Sample Output:
32 : greater than 30
21 : greater than 20
12 : greater than 10
4 : Input a number atleast greater than 10!
echo word_digit("zero;three;five;six;eight;one")."\n";
echo word_digit("seven;zero;one")."\n";
?>
Copy
Sample Output:
035681
701
?>
9. Write a PHP function to change the following array's all values to upper or
lower case.
<?php
function array_change_value_case($input, $ucase)
{
$case = $ucase;
$narray = array();
if (!is_array($input))
{
return $narray;
}
foreach ($input as $key => $value)
{
if (is_array($value))
{
$narray[$key] = array_change_value_case($value, $case);
continue;
}
$narray[$key] = ($case == CASE_UPPER ? strtoupper($value) : strtolower($value));
}
return $narray;
}
$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red');
echo 'Actual array ';
print_r($Color);
echo 'Values are in lower case.';
$myColor = array_change_value_case($Color,CASE_LOWER);
print_r($myColor);
echo 'Values are in upper case.';
$myColor = array_change_value_case($Color,CASE_UPPER);
print_r($myColor);
?>
Copy
Sample Output:
Actual array Array
(
[A] => Blue
[B] => Green
[c] => Red
)
Values are in lower case.Array
(
[A] => blue
[B] => green
[c] => red
)
Values are in upper case.Array
(
[A] => BLUE
[B] => GREEN
[c] => RED
)
$pass_order = Array();
$passWord = '';
Copy
Sample Output:
Generated Password : h1'1#h7Gqfy
11. Create a script to construct the following pattern, using a nested for loop.
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
Pictorial Presentation:
Sample Solution:
PHP Code:
<?php
$n=5;
for($i=1; $i<=$n; $i++)
{
for($j=1; $j<=$i; $j++)
{
echo ' * ';
}
echo '\n';
}
for($i=$n; $i>=1; $i--)
{
for($j=1; $j<=$i; $j++)
{
echo ' * ';
}
echo '\n ';
}
?>
Copy
Sample Output:
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
12. Write a PHP script using nested for loop that creates a chess board as shown
below.
Use table width="270px" and take 30px as cell height and width.
Sample Solution:
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Chess Board using Nested For Loop</h3>
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
<!-- cell 270px wide (8 columns x 60px) -->
<?php
for($row=1;$row<=8;$row++)
{
echo "<tr>";
for($col=1;$col<=8;$col++)
{
$total=$row+$col;
if($total%2==0)
{
echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";
}
else
{
echo "<td height=30px width=30px bgcolor=#000000></td>";
}
}
echo "</tr>";
}
?>
</table>
</body>
</html>
13. Write a PHP program to check if an integer is the power of another integer.
Input : 16, 2
Example: For x = 16 and y = 2 the answer is "true", and for x = 12 and y = 2
"false"
Explanation :
Sample Solution :
PHP Code :
<?php
function is_Power($x, $y)
{
$a = $x;
$b = $y;
while ($x % $y == 0) {
$x = $x / $y;
}
if($x == 1)
{
return "$a is power of $b";
}
else
{
return "$a is not power of $b";
}
}
print_r(is_Power(16,2)."\n");
print_r(is_Power(12,2)."\n");
print_r(is_Power(81,3)."\n");
?>
Copy
Sample Output:
16 is power of 2
12 is not power of 2
81 is power of 3