KRUPANIDHI DEGREE COLLEGE
PHP and MySQL Lab MANUAL
1. Write a PHPscript to print “hello world”.
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
2. Write a PHPscript to find odd or even number from given number.
<html>
<body>
<form method="post">
Enter a number:
<input type="number" name="number">
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if($_POST){
$number = $_POST['number'];
//divide entered number by 2
//if the reminder is 0 then the number is even otherwise the number is odd
if(($number % 2) == 0){
echo "$number is an Even number";
}else{
echo "$number is Odd number";
}
}
?>
3. Write a PHPscript to find maximum of three numbers.
<?php
$n1=120;
$n2=55;
$n3=80;
if($n1>$n2 && $n1>$n2){
echo "$n1 is maximum";
}else
if($n2>$n1 && $n2>$n3){
echo "$n2 is maximum";
}else{
echo "$n3 is maximum";
}
?>
4. Write a PHPscript to swap two numbers.
<?php
$a = 45;
$b = 78;
// Swapping Logic
$third = $a;
$a = $b;
$b = $third;
echo "After swapping:<br><br>";
echo "Value of a: $a</br>";
echo "Value of b: $b</br>";
?>
5. Write a PHPscript to find the factorial of a number.
<html>
<head>
<title>Factorial Program using loop in PHP</title>
</head>
<body>
<form method="post">
<b> Enter the Number:<br>
<input type="number" name="number" id="number">
<input type="submit" name="submit" value="Submit" /></b>
</form>
<?php
if($_POST){
$fact = 1;
//getting value from input text box 'number'
$number = $_POST['number'];
echo "Factorial of $number:<b>";
//start loop
for ($i = 1; $i <= $number; $i++){
$fact = $fact * $i;
}
echo $fact . "<br>";
}
?>
</body>
</html>
6. Write a PHPscript 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";
}
?>
7. Write a PHP script to reverse a given number and calculate its sum.
<html>
<head>
<title>Reverse</title>
<body>
<form method="post">
Enter the number:
<input type="number"name="number"/>
<input type="submit" value="submit"/>
</form>
<?php
if($_POST){
$num=$_POST['number'];
$sum=0;
$rev=0;
while(floor($num)) {
$r=$num%10;
$rev=(($rev*10)+$r);
$sum=$sum+$r;
$num=$num/10;
}
echo"Reverse=$rev<br>";
echo"sum=$sum";
}
?>
</body>
</html>
8. Write a PHP script to generate a Fibonacci series using Recursive function.
<?php
function fibonacci($n){
if($n==0){
return 0;
}else if($n==1){
return 1;
}else{
return(fibonacci($n-1)+fibonacci($n-2));
}
}
if(isset($_GET['n'])){
$n=$_GET['n'];
if(is_numeric($n) && $n>=0){
echo "Fibonacci series for n=$n:";
for($i=0;$i<$n;$i++) {
echo fibonacci($i) ." ";
}
}else{
echo "please provide a valid non-negative numeric value for 'n'";
}
}
else{
echo "please provide a value for 'n' in the URL parameter";
}
?>
9. Write a PHP script to implement atleast seven string functions.
<?php
$string="I Like PHP Programming";
echo "Given String:$string<br/></br>";
//[Link] Length
$length=strlen($string);
echo "[Link] length:$length<br/></br>";
//[Link]
$uppercase=strtoupper($string);
echo "[Link]:$uppercase<br/></br>";
//[Link]
$lowercase=strtolower($string);
echo "[Link]:$lowercase<br/></br>";
//[Link]
$substring=substr($string,7);
echo "[Link] from position 7:$substring<br/></br>";
//[Link]
$repalcestring=str_replace("Like","Love",$string);
echo "[Link] 'Like' with 'Love':$repalcestring</br></br>";
//[Link]
$reversedstring=strrev($string);
echo "[Link] string:$reversedstring<br/></br>";
//[Link] count
$wordcount=str_word_count($string);
echo "[Link] count:$wordcount<br/></br></br>";
?>
10. Write a PHP program to insert new item in array on any position in PHP.
<?php
$originalArray=array("apple","banana","mango","grapes");
echo"Orginal Array <br>";
print_r($originalArray);
$newItem="orange";
$insertPosition=2;
echo "<br> Inserting $newItem at position $insertPosition <br>";
array_splice($originalArray, $insertPosition, 0, $newItem);
echo "Updated Array:<br>";
print_r($originalArray);
?>
PART B
[Link] a PHP script to implement constructor and destructor.
<?php
class MyClass
{
public function __construct() {
echo “Constructor called. object created.<br>”;
}
public function __destruct() {
echo “Destructor called. object destroyed.<br>”;
}
public function displayMessage()
{
echo “Hello from MyClass!”;
}
}
$obj=new MyClass();
$obj->displayMessage();
?>
12. Write a PHP script to implement form handling using get method
<!DOCTYPE html>
<html>
<head>
<title> Login Form </title>
</head>
<body><h2>Login Form</h2>
<form method=”get” action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>”>
<label for=”username”> Username:</label><br>
<input type=”text” id=”username” name=”username”></br></br>
<label for=”password”> Password:</label><br>
<input type=”password” id=”password” name=”password”></br></br>
<input type=”submit” value=”Login”>
</form>
<?php
if(server[“REQUEST_METHOD”]==”GET” && isset($_GET[‘username’])
&& isset($_GET[‘password’]))
{
$username=$_GET[‘username’];
$password=$_GET['password’];
if($username == “admin” && $password == “password”)
{
echo “<p>Login successful. Welcome, $username!</p>”;
}else{
echo “<p>Invalid username or password. Please try again.</p>”;
}
}
?>
</body>
</html>
13. Write a PHP script to implement form handling using post method
<!DOCTYPE html>
<html>
<head>
<title> Login Form </title>
</head>
<body><h2>Login Form</h2>
<form method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>”>
<label for=”username”> Username:</label><br>
<input type=”text” id=”username” name=”username”></br></br>
<label for=”password”> Password:</label><br>
<input type=”password” id=”password” name=”password”></br></br>
<input type=”submit” value=”Login”>
</form>
<?php
if($_server[“REQUEST_METHOD”]==”POST” &&
isset($_POST[‘username’]) && isset($_POST[‘password’]))
{
$username=$_POST[‘username’];
$password=$_POST['password’];
if($username == “admin” && $password == “password”)
{
echo “<p>Login successful. Welcome, $username!</p>”;
}else{
echo “<p>Invalid username or password. Please try again.</p>”;
}
}
?>
</body>
</html>
[Link] a PHP script that receive string as a form input.
<!DOCTYPE html>
<html>
<head>
<title> String Input Form</title>
</head>
<body>
<h2> String Input Form</h2>
<form method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’]; ?> “>
<label for=”inputString”>Enter a String:</label></br>
<input type=”text” id=”inputString” name=”inputString”></br></br>
<input type=”submit” value=”submit”>
</form>
<?php
if($_SERVER[“REQUEST_METHOD”]==”POST” && isset ($_POST[‘inputString’])) {
$inputString=$_POST[‘inputString’];
echo ”<p>You entered:$inputString</p>”;
}
?>
</body>
</html>
15. Write a PHP script that receive form input by the method post to check the
number is prime or not
<html>
<head>
<title> prime number </title>
</head>
<body>
<h2> prime number</h2>
<form method="post">
Enter a Number: <input type="text" name="input"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($_POST)
{
$input=$_POST['input'];
for ($i = 2; $i <= $input-1; $i++) {
if ($input % $i == 0) {
$value= True;
}
}
if (isset($value) && $value) {
echo 'The Number '. $input . ' is not prime';
} else {
echo 'The Number '. $input . ' is prime';
}
}
?>
</body>
</html>