php program
php program
<? php
function is Divisible($number,$divisor){
if($divisor==0){
}elseif($number%$divisor==0){ return"$number is
divisible by $divisor";
else
$testNumber=20;
$divisor=5;
$result=isDivisible($testNumber,$divisor); echo
$result;
?>
4a
<?php
$x=$n;
$root;
$count=0 while(true){
$count++;
$root=0.5*($x+($x/$x));
if (abs($root-$x)<$1){
break;
$x=$root;
return $root;
$n=327;
$l=0.00001;
?>
4b
<?php
function generatefloydsTriangle($rows){
$number=1;
for($i=1;$i<=$rows;$i++){
for($j=1;$j<=$i;$j++){ echo
$number."";
$number++;
echo"<br>";
$rows=5;
generatefloydsTriangle($rows);
?>
5a
<?php
function squareRoot($n,$l){
$x=$n;
$root;
$count=0; while(true)
$count++;
$root=0.5*($x+($n/$x)); if
(abs($root-$x)<$l) { break;
$x=$root;
return $root;
$n=327;
$l=0.00001;
?>
5b
<?php
function quadraticRoots($a,$b,$c){
$delta=$b*$b-4*$a*$c;
if($delta>0){
$root1=(-$b+sqrt($delta))/(2*$a);
$root2=(-$b-sqrt($delta))/(2*$a);
}elseif($delta==0){
$root=-$b/(2*$a);
}else{
$realPart=-$b/ (2 * $a);
$imaginaryPart=sqrt(abs($delta))/(2*$a);
$a=2;
$b=4;
$c=3;
quadraticRoots($a,$b,$c);
?>
<?php
function printFirstNLines($filename,$n){
$file=fopen($filename,"r"); if($file){
$i=0; while(($line=fgets($file))!==false&&$i<=$n){
echo $line;
$i++;
fclose($file);
}else{
echo"Unable to open file:$filename";
function UpdateFileContent($filename,$content){
$file=fopen($filename,"a");
if($file){ fwrite($file,$content);
fclose($file);
}else{
$filename="example.txt";
$linesToprint=5;
printFirstNLines($filename,$linesToprint);
echo "\n\n";
UpdateFileContent($filename,$newcontent);
?>
<?php
function countWordFrequency($filename,$word){
$fileContent=file_get_contents($filename);
$fileContent=Strtolower($fileContent);
$word=strtolower($word);
$wordsArray=str_word_count($fileContent,1);
$wordCount=array_count_values($wordsArray);
return isset($wordCount[$word])?$wordCount[$word]:0;
} if($_SERVER["REQUEST_METHOD"]=="POST"){
$wordToSearch=$_POST['word'];
$filename="sample.txt"; if(file_exists($filename)){
}else{
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"content="width=device-width,initial-scale=1.0">
</head>
<body>
<input type="submit"value="Submit">
</form>
</body>
</html>
6a
<?php
date_default_timezone_set('UTC');
echo"current date and time:".date('Y-m-d H:i:s')."<br>";
echo"today's date:".date('Y-m-d')."<br>";
echo"day of week:".date('l')."<br>;
echo"Month:".date('F')."<br>";
echo"current time:".date('H:i:s')."<br>";
echo"12-hour format:".date('h:i:s A')."<br>";
echo"year:".date('Y')."<br>";
echo"month:".date('m')."<br>";
echo"day:".date('d')."<br>";
echo"hour:".date('H')."<br>";
echo"minute:".date('i')."<br>";
echo"second:".date('s')."<br>";
echo"current timeStamp:".time()."<br>";
$specific date=strtotime("2023-10-15");
echo"specific date:".date('Y-m-d',$specific date)."<br>";
?>
10a
<?php
class Employee{
public $name;
public $id;
public $dept;
public $salary;
public $doj;
public function __construct($name,$id,$dept,$salary,$doj){
$this->name=$name;
$this->id=$id;
$this->dept=$dept;
$this->salary=$salary;
$this->doj=$doj;
}
public function display(){
echo"employee name:$this->name<br>";
echo"employee ID:$this->id<br>";
echo"employee department:$this->dept\n";
echo"employee salaryu:$this->salary\n";
echo"employee data of joining:$this->doj\n";
}
}
$employee1=new Employee("John Doe","E1001","IT",50000,"2023-09-15");
echo"employee data:\n";
$employee1->display();
?>
12
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Form</title>
</head>
<body>
<form action="display.php" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
If($_SERVER[“REQUEST_METHOD”]==”POST“){
$name=$_POST[“name”]
$email=$_POST[“email”]
echo”<h2>Form Data</h2>”;
echo”<p>Name:$name</p>”;
echo”<p>Email:$email</p>”;
}else{
Echo”Error:form not submitted”;
}
?>
11a
<?php
function countAadhaarOccurences($text){
$pattern="/\b\d{4}\s?\d{4}\s?\d{4}\b/";
preg_match_all($pattern,$text,$matches);
return count($matches[0]);
}
$text="My Aadhaar number is 1234 5678 9012.Please note it down .Another Aadhaar number is 3456 2278
9275";
$occurences=countAadhaarOccurences($text);
echo"number of Aadhaar occurences:$occurences";
?>
11b
<?php
function replacePattern($text,$pattern,$replacement){
$result=preg_replace($pattern,$replacement,$text);
return $result;
}
$text="The quick brown for jumps over the lazy dog.";
$pattern="/brown/";
$replacement="red";
$newText=replacePattern($text,$pattern,$replacement);
echo"Modified text:$newText";
?>
9a
<?php
function filterArrayByKeyNames($inputArray,$KeyNames){
$outputArray=array();
foreach($inputArray as $Key=>$value){
if(!in_array($Key,$KeyNames)){
$outputArray[$Key]=$value;
}
}
return $outputArray;
}
$array1=array('c1'=>'red','c2'=>'green','c3'=>'white','c4'=>'black');
$array2=array('c1','c3');
$output=filterArrayByKeyNames($array1,$array2);
echo"array<br>(<br>";
foreach($output as $Key=>$value){
echo"[$Key]=>$value<br>";
}
echo")<br>";
?>