Programming Exercises on PHP
Exercise 1:Write a console application in PHP to check whether the given number is prime or not.
Solution:
Create a file with the extension Prime.php and place the following code in it:
<?php
$n = 50;
$prime = false;
if($n%2 == 0)
{
print("The number $n is not a Prime Number");
$prime = true;
}
if($n%3 == 0)
{
print("The number $n is not a Prime Number");
$prime = false;
}
if(!$prime)
print("The number $n is a Prime Number");
?>
Execute the above program from the PHP Client as follows:
php Prime.php
Exercise 2:
Develop a console application in PHP that uses a for-loop to generate the output as shown in the following table. Find the following for first 10 integers:
- Inverse (1/I) of 10 integers
- Square (I*I) value of first 10 integers
- Factorial (I!) of first 10 integers
I
|
I*I
|
I!
|
I/1
|
1
|
1
|
1
|
1
|
2
|
4
|
2
|
0.5
|
3
|
9
|
6
|
0.33333333333333
|
4
|
16
|
24
|
0.25
|
5
|
25
|
120
|
0.2
|
6
|
36
|
720
|
0.16666666666667
|
7
|
49
|
5040
|
0.14285714285714
|
8
|
64
|
40320
|
0.125
|
9
|
81
|
362880
|
0.11111111111111
|
10
|
100
|
3628800
|
0.1
|
Solution:
<table border=1>
<tr>
<th>I</th>
<th>I*I</th>
<th>I!</th>
<th>1/I</th>
</tr>
<?php
for($i = 1;$i<=10; $i++)
{
?>
<tr><td>
<?php
Print("$i");
?>
</td><td>
<?php
$square = $i * $i;
print("$square");
?>
</td><td><?php
$fact = 1;
for($j=1; $j<=$i; $j++)
$fact = $fact * $j;
print("$fact");
?>
</td><td><?php
$invers = 1/$i;
print("$invers \n");
}
?></td></tr>
?>
</table>
Exercise 3:
Create a web page containing Tables from 1 to 9, each having entries from 1 to 9. The output should look like this:
Multiplication table
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 x 1 = 1 | 2 x 1 = 2 | 3 x 1 = 3 | 4 x 1 = 4 | 5 x 1 = 5 | 6 x 1 = 6 | 7 x 1 = 7 | 8 x 1 = 8 | 9 x 1 = 9 |
1 x 2 = 2 | 2 x 2 = 4 | 3 x 2 = 6 | 4 x 2 = 8 | 5 x 2 = 10 | 6 x 2 = 12 | 7 x 2 = 14 | 8 x 2 = 16 | 9 x 2 = 18 |
1 x 3 = 3 | 2 x 3 = 6 | 3 x 3 = 9 | 4 x 3 = 12 | 5 x 3 = 15 | 6 x 3 = 18 | 7 x 3 = 21 | 8 x 3 = 24 | 9 x 3 = 27 |
1 x 4 = 4 | 2 x 4 = 8 | 3 x 4 = 12 | 4 x 4 = 16 | 5 x 4 = 20 | 6 x 4 = 24 | 7 x 4 = 28 | 8 x 4 = 32 | 9 x 4 = 36 |
1 x 5 = 5 | 2 x 5 = 10 | 3 x 5 = 15 | 4 x 5 = 20 | 5 x 5 = 25 | 6 x 5 = 30 | 7 x 5 = 35 | 8 x 5 = 40 | 9 x 5 = 45 |
1 x 6 = 6 | 2 x 6 = 12 | 3 x 6 = 18 | 4 x 6 = 24 | 5 x 6 = 30 | 6 x 6 = 36 | 7 x 6 = 42 | 8 x 6 = 48 | 9 x 6 = 54 |
1 x 7 = 7 | 2 x 7 = 14 | 3 x 7 = 21 | 4 x 7 = 28 | 5 x 7 = 35 | 6 x 7 = 42 | 7 x 7 = 49 | 8 x 7 = 56 | 9 x 7 = 63 |
1 x 8 = 8 | 2 x 8 = 16 | 3 x 8 = 24 | 4 x 8 = 32 | 5 x 8 = 40 | 6 x 8 = 48 | 7 x 8 = 56 | 8 x 8 = 64 | 9 x 8 = 72 |
1 x 9 = 9 | 2 x 9 = 18 | 3 x 9 = 27 | 4 x 9 = 36 | 5 x 9 = 45 | 6 x 9 = 54 | 7 x 9 = 63 | 8 x 9 = 72 | 9 x 9 = 81 |
Do the following before running PHP from browser (from Terminal
):
- Install Apache Server using the following command
sudo apt-get install apache2 - Run the following script for setting up Password for root user:
sudo passwd root - Use the following command from the Terminal to have a UI for root login in the login screen:
sudo sh -c 'echo "greeter-show-manual-login=true" >> /etc/lightdm/lightdm.conf'
- If Step 3 doesn't work on your computer, try the following command and include the command
greeter-show-manual-login=true
in lightdm.conf file:
gksudo gedit
/etc/lightdm/lightdm.conf
<?php
echo "<h1>Multiplication table</h1>";
echo "<table border=2 width=75%";
echo "<tr>";
for ($i = 1; $i<= 9 ; $i++)
echo "<td>".$i."</td>";
echo "</tr>";
for ($i = 1; $i <= 9; $i++ ) {
//this is the outer loop
echo "<tr>";
for ( $j = 1; $j <= 9; $j++ ) { // inner loop
echo "<td> $j x $i = ".$i * $j."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Exercise 4:
Create a registration form which contains fields name,Roll No,Gender and a submit button. All the details should be displayed in the server page when the user clicks the submit button.
Solution:
<html>
<body>
<form action="" method="post">
<table align="center">
<tr>
<td>Name</td>
<td> </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Roll Number</td>
<td> </td>
<td><input type="text" name="roll"></td>
</tr>
<tr>
<td>Gender</td>
<td> </td>
<td><select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
<br><br>
<?php
if(isset($_REQUEST['submit']))
{
echo "<br>Name :".$_REQUEST['name'];
echo "<br>Roll Number :".$_REQUEST['roll'];
echo "<br>Gender :".$_REQUEST['gender'];
}
?>
Slightly modify the above program so that it can store and display all the students who have registered themselves through this form.
Solution:
Replace the PHP code from the above program with the following:
<?php
$con = mysql_connect("localhost","root","admin");
mysql_select_db("testdb",$con);
echo "Students who are registered already : <br/>";
echo "<table border=2 width=75%>";
echo "<tr>";
echo "<td>Student Name</td><td>Roll Number</td><td>Gender</td>";
echo "</tr>";
$result = mysql_query("SELECT * FROM Student");
if(mysql_num_rows($result)<>0)
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Roll']."</td>";
echo "<td>".$row['Gender']."</td>";
echo "</tr>";
}
}
$name = $_REQUEST['name'];
$rollno = $_REQUEST['roll'];
$gender = $_REQUEST['gender'];
if ($name <> "")
{
$result = mysql_query("SELECT * FROM Student WHERE Name = '$name' and Roll='$rollno'");
if(mysql_num_rows($result)==0)
{
$sql = "INSERT INTO Student VALUES('$name','$rollno','$gender')";
mysql_query($sql,$con);
echo "<tr>";
echo "<td>".$name."</td>";
echo "<td>".$rollno."</td>";
echo "<td>".$gender."</td>";
echo"</tr>";
}
}
echo "</table>";
mysql_close($con);
?>
This program requires a database named testdb and a table named Student, which is created using the following command in the local mysql server (localhost):
Student | CREATE TABLE `Student` (
`Name` varchar(15) DEFAULT NULL,
`Roll` varchar(10) DEFAULT NULL,
`Gender` char(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1