Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical 2 teni

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PHP. Practical set-2.

1CE2502

Practical 1: Write PHP Script to demonstrate assosiative array.


<!DOCTYPE html>
<html>
<head>
<title>p21</title>
</head>
<style>
table{
color:black;border:1px solid black;
width: 300px;
}
td{
border:1px solid black;
padding: 5px;
}
</style>
<body>
<?php
echo "Associative Array: <br>";
$asc=array("Dhruv"=>1,"Het"=>2,"Meet"=>3);
foreach($asc as $key=>$val){
echo "$key=>$val <br>";
}
echo "<br>Index Array: <br>";
$idx=array("Blue","Red","Green");
foreach($idx as $colr){
echo $colr . "<br>"; }
echo "<br>Multidimensional Array: <br><hr>";
$mld=array( array("Name ","Enrol no.","City"),
array("Valay",1142,"Ghandhinagar"),

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

array("Het",1142,"Unjha"),
array("Dhruv",1159,"Gandhinagar"), );
echo " <table> ";
for($i=0;$i<4;$i++)
{
echo "<tr>";
for($j=0;$j<3;$j++)
{
echo "<td>" . $mld [$i][$j] ."</td> ";
}
echo "</tr>";
} echo "</table>"; ?>
</body>
</html>

Output:

Valay
Va

Valay 1142 Gandhinagar

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

Practical 2: Write PHP script for checking that given number is prime
or not using user- defined function.
<!DOCTYPE html>
<html>
<head>
<title>pr22</title>
</head>
<body>
<?php
$a=3;
function prime($a){
$i=2;
while($i<$a)
{
if($a % $i==0)
{
return 0;
}
$i++;
}
return 1;
}
$p=prime($a);
if($p==0){
echo "$a is not prime";
}
else { echo "$a is prime."; }
?>
</body>
</html>

Output:

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

Practical 3: Write PHP script to swap two number using user-defined


function . (call by value and call by reference).
<?php
function call($x,$y)
{
$c=$x;
$x=$y;
$y=$c;
echo "After call value of A is $x in function.";
echo "<br>After call value of B is $y in function.";
}
function ref(&$x,&$y)
{
$c=$x;
$x=$y;
$y=$c;
}
$a=10;
$b=11;
echo "Before call value of A is $a<br>";
echo "Before call value of B is $b<br>";
call($a, $b);
echo "<br>After call value of A is $a";
echo "<br>After call value of B is $b";
ref($a,$b);
echo "<br>After reference value of A is $a";
echo "<br>After reference value of B is $b";
?>

Output:

Practical 4: Write PHP script to demonstrate Variable function.


<?php //Variable Function

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

$a=8;

$get=gettype($a);

echo $get. "<br><hr>"; //gettype()

$st=false;

$m="3.4mm";

settype($st,"integer");

settype($m,"float");

echo "$st <br>";

echo $m ."<br><hr>"; //settype()

if(isset($a)) //isset()

{ echo "Variable has some value "; }

echo "<br>Before distroy A is $a";

unset($a); //unset()

echo "After distroy A is $a<hr>";

$val="MP114";

$f="1.2gh";

echo "strval() : ".strval($val);

echo "<br> intval() : ".intval("238dh");

echo "<br>floatval() : ".floatval($f)."<br><hr>";

$no=array(10,"meet",false,4.5);

print_r($no); //print_r()

echo "<br>";

var_dump($no);

?>

Output:

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

Practical 5: Write PHP script to demonstrate String function.


<?php //string Function
echo chr(97)."<br/>";
echo chr(52)."<br/>";//chr()
echo ord("D"). "<br/>";
echo ord("%"). "<br/>";//ord()
$str= "UNIVERSITY";
$st = "dhruv";
echo "Lenght :- ".strlen($str) ."<br>";//strlen()
$str= strtolower($str);
echo $str ."<br>"; //strolower()
$st= strtoupper($st);
echo $st ."<br>"; //stroupper()
$text=" Hello World ";
echo ltrim($text) ."<br>";
echo rtrim($text) ."<br>";
echo trim($text) ."<br>";
$r=substr("abcdef",-1); //substr()
echo $r ."<br>";
$r=substr("abcdef",2,-1);
echo $r ."<br>";
echo str_replace("World","!!","HelloWorld").'<br>';
echo strrev($st) .'<br>';
//strcmp() and print()
print(strcmp($str,$st)?"Different":"Same") ."<br>";
?>

Output:

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

VALAY PATEL

Practical 6: Write a php script to demonstrate Date function .


<?php // Date Fnction
$timestamp = strtotime('1st January 2004');
echo date("j")."th"; // J represent Current Date
echo '</br>';
echo date("F"); // F represent Current Month
echo '</br>';
echo date("Y"); // Y represent Current Year
echo '</br>';
echo date("h:i:s"); /* H represent Hour, I represent Minute, S
represent Second*/
echo '</br>';
echo date("A"); // A represent AM or PM

echo idate('m', $timestamp);


?>
Output:-

Practical 7: Write a php script to demonstrate Math function.


<?php

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

echo "Abs() fun.: ".abs(-4.2)."<br>";


echo "floor() fun.: ".floor(4.593)."<br>";
echo "floor() fun.: ".floor(-3.19)."<br>";
echo "ceil() fun.: ".ceil(4.593)."<br>";
echo "ceil() fun.: ".ceil(-3.19)."<br>";
echo "round() fun.: ".round(3.17)."<br>";
echo "round() fun.: ".round(3.67)."<br>";
echo "round() fun.: ".round(-4.5)."<br>";
$r=fmod(4,2);
echo "fmod() fun: ".$r."<br>";
echo "min() fun: ".min(2,3,4,5,6)."<br>";
echo "max() fun: ".max(2,3,4,5,6)."<br>";
echo "pow() fun: ".pow(6,2)."<br>";
echo "sqrt() fun: ".sqrt(9)."<br>";
echo "Random number: ".rand(1,8);
?>

Output:

Practical 8: Write a php script to demonstrate Array function.


<?php
$a = array("Dog","Cat","Horse");
$total=count($a);

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

echo $total ."<br>";


print_r(array_reverse($a)); //reverse print

list($a, $b, $c) = $a;


echo "<br>I have several animals, a $a, a $b and a $c.";
$ar = array("Dog","Cat","Horse");

$mode = current($ar);
echo "<br>". $mode;
$mode = next($ar);
echo "<br>". $mode;
$mode = current($ar);
echo "<br>". $mode;
$mode = prev($ar);
echo "<br>". $mode;
$mode = end($ar);
echo "<br>". $mode;
$mode = current($ar);
echo "<br>". $mode;

$fruits = array("lemon", "orange", "banana", "apple");


sort($fruits);
foreach ($fruits as $val) {
echo "<br>".$val;
} ?>

Output:

Practical 9: Write a php script to demonstrate File function.


<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Do one thing every day that scares you.");
fclose($file);
?>

Output:

Patel valay. 22171341142


PHP. Practical set-2. 1CE2502

Patel valay. 22171341142

You might also like