100% found this document useful (1 vote)
322 views

PHP Lab Programms

The document provides examples of PHP programs and functions to perform various tasks: 1. Accept user input in a form and display it using echo. 2. Count the number of lines in a text file. 3. Use a ternary operator to test if a number is greater than 30, 20, or 10.

Uploaded by

akshaya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
322 views

PHP Lab Programms

The document provides examples of PHP programs and functions to perform various tasks: 1. Accept user input in a form and display it using echo. 2. Count the number of lines in a text file. 3. Use a ternary operator to test if a number is greater than 30, 20, or 10.

Uploaded by

akshaya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PHP PROGRAMMS(w3resources.

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:

View the output in the browser

2. PHP : Exercise-16 with Solution


Write a PHP script to count number of lines in a file.
Note : Store a text file name into a variable and count the number of lines of text
it has.
Sample Solution: -
PHP Code:
<?php
$file = basename($_SERVER['PHP_SELF']);
$no_of_lines = count(file($file));
echo "There are $no_of_lines lines in $file"."\n";
?>

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!

4.Write a PHP program to convert word to digit.


Input: zero;three;five;six;eight;one
Output: 035681
Sample Solution: -
PHP Code:
<?php
function word_digit($word) {
$warr = explode(';',$word);
$result = '';
foreach($warr as $value){
switch(trim($value)){
case 'zero':
$result .= '0';
break;
case 'one':
$result .= '1';
break;
case 'two':
$result .= '2';
break;
case 'three':
$result .= '3';
break;
case 'four':
$result .= '4';
break;
case 'five':
$result .= '5';
break;
case 'six':
$result .= '6';
break;
case 'seven':
$result .= '7';
break;
case 'eight':
$result .= '8';
break;
case 'nine':
$result .= '9';
break;
}
}
return $result;
}

echo word_digit("zero;three;five;six;eight;one")."\n";
echo word_digit("seven;zero;one")."\n";
?>

Copy
Sample Output:
035681

701

5. Write a PHP program to remove duplicates from a sorted list.


Input: (1,1,2,2,3,4,5,5)
Output: (1,2,3,4,5)
Sample Solution: -
PHP Code:
<?php
function remove_duplicates_list($list1) {
$nums_unique = array_values(array_unique($list1));
return $nums_unique ;
}
$nums = array(1,1,2,2,3,4,5,5);
print_r(remove_duplicates_list($nums));
?>
6. Write a PHP program to compute the sum of the digits of a number.
Sample Solution: -
PHP Code:
<?php
function sum_of_digits($nums) {
$digits_sum = 0;
for ($i = 0; $i < strlen($nums); $i++) {
$digits_sum += $nums[$i];
}
return $digits_sum;
}
echo sum_of_digits("12345")."\n";
echo sum_of_digits("9999")."\n";
?>
7. Write a function to calculate the factorial of a number (a non-negative integer).
The function accepts the number as an argument.
<?php
function factorial_of_a_number($n)
{
if($n ==0)
{
return 1;
}
else
{
return $n * factorial_of_a_number($n-1);
}
}
print_r(factorial_of_a_number(4)."\n");
?>

8. Write a PHP function that checks whether a passed string is a palindrome or


not?
<?php
function check_palindrome($string)
{
if ($string == strrev($string))
return 1;
else
return 0;
}
echo check_palindrome('madam')."\n";

?>
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
)

10. Write a PHP function to generate a random password (contains uppercase,


lowercase, numeric and other) using shuffle() function.
<?php
function rand_Pass($upper = 1, $lower = 5, $numeric = 3, $other = 2) {

$pass_order = Array();
$passWord = '';

//Create contents of the password


for ($i = 0; $i < $upper; $i++) {
$pass_order[] = chr(rand(65, 90));
}
for ($i = 0; $i < $lower; $i++) {
$pass_order[] = chr(rand(97, 122));
}
for ($i = 0; $i < $numeric; $i++) {
$pass_order[] = chr(rand(48, 57));
}
for ($i = 0; $i < $other; $i++) {
$pass_order[] = chr(rand(33, 47));
}

//using shuffle() to shuffle the order


shuffle($pass_order);

//Final password string


foreach ($pass_order as $char) {
$passWord .= $char;
}
return $passWord;
}
echo "\n"."Generated Password : ".rand_Pass()."\n";
?>

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

View the output in the browser

You might also like