PHP
1 .Prime number pgm
<?php
function isPrime($number)
{
if($number<=1)
{
return false;
}
for($i=2;$i<=sqrt($number);$i++)
{
if($number%$i==0)
{
return false;
}
}
return true;
}
function displayprimesInRange($start,$end)
{
$primes=[];
$primeCount=0;
for($i=$start;$i<=$end;$i++)
{
if(isPrime($i))
{
$primes[]=$i;
$primeCount++;
}
}
echo "prime numbers between $start and $end are:".implode(",",$primes)."</br>";
echo "total number of prime numbers:$primeCount<br>";
}
$start=3;
$end=10;
displayprimesInRange($start,$end);
?>
2. Message passing pgm
<html>
<head>
<title>Message Passing</title>
</head>
<body>
<center>
<form action="page2.php" method="post">
<label> Enter Message:</label>
<input type="text" name="message"/>
<input type="submit" name="submit" value="send message"/>
</form>
</center>
</body>
</html>
<?php
$message=$_POST['message'];
echo "your sent message is:".$message;
?>
3. Calculator pgm
<?php
$ans=0;
if(isset($_POST["submit"]))
{
$num1=$_POST['n1'];
$num2=$_POST['n2'];
$oprnd=$_POST['submit'];
if($oprnd=="+")
$ans=$num1+$num2;
else if($oprnd=="-")
$ans=$num1-$num2;
else if($oprnd=="*")
$ans=$num1*$num2;
else if($oprnd=="/")
$ans=$num1/$num2;
}
?>
<html>
<head>
<title>Sample calculator</title>
</head>
<body><center>
<form method="post">
<h1>Sample Calculator</h1><br>
first number:<input name="n1"><br><br>
second number:<input name="n2"><br><br>
<input type="submit" name="submit" value="+"/>
<input type="submit" name="submit" value="-"/>
<input type="submit" name="submit" value="*"/>
<input type="submit" name="submit" value="/"/><br>
<br>Result:<input type="text" value="<?php echo $ans;?>"readonly<br>
</form></center>
</body>
</html>
4. String functions pgm
<?php
$str1="Change your thoughts and you will change the world!!!";
$str2="The first programmer was women";
echo "the Given strings are:<br>";
echo "string1 is:".$str1."<br>";
echo "string2 is:".$str2."<br>";
$len=strlen($str1);
echo "<br>The length of string1 is:".$len;
$word_count=str_word_count($str1);
echo "<br>The total words of str1 is:".$word_count;
$capitalize=ucwords($str1);
echo "<br>The uppercase of each word is:".$capitalize;
$upper=strtoupper($str1);
echo "<br>The uppercase of string is:".$upper."<br></br>";
$reverse=strrev($str1);
echo "The reverse of string1 is:<br>".$reverse;
$compare=strcmp($str1,$str2);
echo "<br>The comparision of str1 and str2 is:".$compare;
$repeate=str_repeat($str1,2);
echo "<br>".$repeate;
?>
5. Array function pgm
<?php
$arr=array(7.36,8,90,91,28,66,42,3);
echo "array elements are:";
foreach($arr as $value)
{
echo $value.",";
}
echo "<br> Minimum number is:".min($arr);
echo "<br> Maximum number is:".max($arr);
sort($arr);
echo "<br> the sorted array is:";
foreach($arr as $value)
{
echo $value.",";
}
rsort($arr);
echo "<br>The reverse sorted array is:";
foreach($arr as $value)
{
echo $value.",";
}
array_pop($arr);
echo "<br>after pop the array is:";
foreach($arr as $value)
{
echo $value.",";
}
array_push($arr,'50');
echo "<br>After push the array is:";
foreach($arr as $value)
{
echo $value.",";
}
?>
Comments
Post a Comment