PHP
1. Largest of three numbers using if else ladder.
$a = 15;
$b = 68;
$c = 600;
echo "<br>The given numbers are $a, $b and $c<br>";
if($a>$b && $a>$c)
{
echo "$a is Maximum number";
}
elseif($b>$a && $b>$c)
{
echo "$b is Maximum number";
}
else
{
echo "$c is Maximum number";
}
?>
2. check whether the given number is prime ornot usingforloop.
function primeCheck($number)
{
if($number ==1)
return 0;
for ($i = 2; $i <= $number/2; $i++)
{
if ($number % $i ==0)
return 0;
}
return 1;
}
$number = 1;
$flag = primeCheck($number);
if ($flag == 1)
echo "$number is prime number ";
else
echo"$number is Not a prime number ";
?>
3. To reverse a given number using while loop.
$num=1793;
$rev=0;
echo "Given number is $num";
echo "<br>Reverse of a given number is ";
while($num>=1)
{
$rem=$num%10;
$rev=$rev*10+$rem;
$num=$num/10;
}
echo $rev;
?>
4.To print the Fibonacci series using functions.
function fibonacci($n)
{
if($n==0)
return 0;
if($n==1)
return 1;
else
{
$fib=fibonacci($n-1)+fibonacci($n-2);
return $fib;
}
}
$num = 10;
echo "The Given number is $num";
echo "<br>The Fibonacci series of first $num is";
for($i=0;$i<$num;$i++)
{
$res=fibonacci($i);
echo "<br> $res ";
}
?>
5. To illustrate different string functions.
<?php
$str1="PHP SCRIPTING language";
$str2=" It is partially case sensitive ";
echo " The string1 is $str1";
echo "<br>The length of the string1 is ".strlen($str1);
echo "<br>The length of the string2 is ".strlen($str2);
echo "<br>After trimming, string2 is:".strlen(trim($str2));
echo "<br>Number of words in the string1 is ".str_word_count($str1);
echo "<br>The reverse of the string1 is ".strrev($str1);
echo"<br> String1 is in Lower case ".strtolower($str1);
echo"<br> String1 is in Upper case ".strtoupper($str1);
echo "<br>After replace php in to java is ".str_replace("PHP","Java",$str1);
echo "<br> Sub string of string1 is ".substr($str1, 18,34);
echo "<br> The lagnguge is present at ".strpos($str1,'guage') ." position";
?>
6. to implement calculator using switch case.
<?php
$a=7;
$b=5;
$opr= '+';
switch($opr)
{
case '+':echo "<br> The Operator is $opr";
echo "<br>Addition of $a and $b is ".($a+$b);
break;
case '-': echo "<br> The Operator is $opr";
echo " <br>Substraction of $a and $b is ".($a-$b);
break;
case '*' : echo "<br> The Operator is $opr";
echo " <br>Product of $a and $b is " . ($a*$b);
break;
case '/': echo "<br> The Operator is $opr";
echo " <br>Quotient of $a and $b is " .($a/$b);
break;
case '%': echo "<br> The Operator is $opr";
echo " <br>Reminder of $a and $b is".($a%$b);
break;
default : echo " <br>Inavalid operator or No Operator is selected";
}
?>
7. To find factorial of a number using foreach loop.
<?php
$n=4;
$fact=1;
$a=array(1,2,3,4);
foreach($a as $val)
{
$fact=$fact*$val;
}
echo " The Factorial of $n is $fact";
?>
8. To sort the given array.
<?php
$a=array(3,7,69,1,9,2);
echo "The Given array is <br>";
foreach ($a as $val)
echo "$val<br>";
for($i=0;$i<count($a);$i++)
{
for($j=0;$j<=count($a)-1;$j++)
{
if($a[$i]<$a[$j])
{
$temp=$a[$i];
$a[$i]=$a[$j];
$a[$j]=$temp;
}
}
}
echo "The sorted array is<br>";
for($i=0;$i<count($a);$i++)
echo $a[$i]. "<br>";
?>
9. To create and manage a database using SQLcommands.
<?php
$con=mysqli_connect("localhost","root","","mydb");
$sql = "CREATE TABLE students(id INT(6), name VARCHAR(30))";
if ($con->query($sql) === TRUE)
{
echo "Table STUDENTS created successfully";
}
$q1 = "INSERT INTO STUDENTS (id, name) VALUES ('101', 'Ramesh')";
if ($con->query($q1) === TRUE)
{
echo "<br>New record created successfully<br>";
}
$q2 = "SELECT id, name FROM STUDENTS";
$result = $con->query($q2);
if ($result->num_rows> 0)
{
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " -Name: " . $row["name"]."<br>";
}
}
$q3 = "INSERT INTO STUDENTS (id, name) VALUES ('102','Sangitha')";
$con->query($q3);
$q4 = "DELETE FROM STUDENTS WHERE id=102";
if ($con->query($q4) === TRUE)
{
}
else
{
echo "Record deleted successfully";
echo "Error deleting record: " . $con->error;
$con->close();
}
?>
Yar guru ninu kalavida
ReplyDeleteI love u rangnath ❤❤❤❤❤big fan of u ����like u so much
ReplyDeleteThis website helped us a lot in our exams:-)
ReplyDeleteLove from siddaganga women's clg❤️