PHP_Practical
PHP_Practical
PHP_Practical
<?php
echo "Hello World";
?>
2. Write a Program for Static, Local and Global Variable using Function.
<?php
$a = 20;
function Local_variable() {
$b = 40;
global $a;
$Add = $a + $b;
echo "Ans: $Add<br>";
}
function Static_variable() {
static $x = 0;
$x++;
echo "Static Variable: $x<br>";
}
Static_variable();
Static_variable();
Local_variable();
?>
3. Write a Program to Find Factorial of 4.
<?php
$fact = 1;
$no = 4;
<?php
$a = 20;
if ($a == 0) {
echo "$a is Zero";
} else if ($a % 2 == 0) {
echo "$a is Even";
} else {
echo "$a is Odd";
}
?>
5. Write a Program to check number is Even and Greaterthan 20.
<?php
$a = 44;
if ($a % 2 == 0 && $a > 20) {
echo "$a is Even and Greater than 20";
} elseif ($a % 2 == 0) {
echo "$a is only Even";
} else {
echo "$a is Odd";
}
?>
<?php
$a = array(1, 2, 3);
$b = array(4, 5, 6);
<?php
$num = "121";
$rev = strrev($num);
if ($num == $rev) {
echo "$num is Palindrome";
} else {
echo "$num is not Palindrome";
}
?>
<?php
$a = readline("Enter the value of A: ");
$b = readline("Enter the value of B: ");
else {
echo "$a is Lessthan $b ";
}
?>
<?php
$a = readline("Enter the value of A: ");
$b = readline("Enter the value of B: ");
echo $a > $b ? $a : $b;
?>
<?php
$a = readline("Enter the num to generate the table: ");
}
?>
<?php
$a= readLine("Enter num to generate pattern: ");
for($i = 1; $i <= $a; $i++)
{
for($j = 1; $j <= $i; $j++)
{
echo "*";
}
echo "\n";
}
?>
<?php
$a= readLine("Enter num to generate pattern: ");
<?php
$a= readLine("Enter num to generate pattern: ");
$asc = 65;
14. Write a Program to implement fopen() and fread() function to open and read file.
<?php
$f = fopen("1.txt","r");
$r = fread($f,filesize("1.txt"));
echo "$r";
?>
15. Write a Program to implement fopen() and fread() function to open and read file with specified
file size.
<?php
$f = fopen("1.txt","r");
$r = fread($f,3);
echo "$r";
?>
16. Write a Program to check file exist or not, and if file exist than read the file and close with
fclose() function.
<?php
$f = fopen("1.txt","r");
if(file_exists("1.txt"))
{
$r = fread($f,filesize("1.txt"));
echo $r;
fclose($f);
}
else
{
echo "An error occured to open the file";
}
?>
<?php
if(file_exists("1.txt"))
{
$f = fopen("1.txt","w");
if(fwrite($f,"Hello Pime"))
{
echo "Data Written Success..";
}
else
{
echo "Data is not Written";
}
fclose($f);
}
?>
<?php
if(file_exists("1.txt"))
{
$f = fopen("1.txt","r");
echo "Yes File Is Exist";
}
else
{
$f = fopen("1.txt","w");
echo "File Is Created";
}
fclose($f);
?>
19.Set one cookie With name user and value shreyarth Time span is 3 day
<?php
$name = "User";
$value = "Shreyarth";
setcookie($name,$value,time()+72);
?>
<?php
if(isset($_COOKIE["$name"]))
{
echo "cookie is set For 3 Days: ".$_COOKIE["$name"];
}
else{
echo "cookie is not set";
}
?>
20. Modify Value of Cookie from Shreyarth to University.
<?php
$name = "User";
$value = "Shreyarth";
setcookie($name,$value);
?>
<?php
if(isset($_COOKIE["$name"]))
{
echo "cookie is set Before Change: ".$_COOKIE["$name"];
$_COOKIE["$name"]= "University";
echo "<br>cookie value is changed After Change: ".$_COOKIE["$name"];;
}
else{
echo "cookie is not set";
}
?>
<?php
echo Div(5,5);
function Div($a,$b) {
try{
if($b == 0)
{
throw new Exception("Divide By Zero", );
}
else{
echo "Division:";
echo $a/$b;
}
}
catch(Exception $e)
{
echo "Divide By Zero Error";
}
finally {
echo "<br>Addition :";
echo $a + $b;
}
}
?>
22.increase page viewer by session
<?php
session_start();
if(!isset($_SESSION["PageView"]))
{
$_SESSION["PageView"] = 1;
}
else
{
$_SESSION["PageView"]++;
}
echo "PageView : ".$_SESSION["PageView"];
?>
23.database connectivity(create, database,insert,update ,dlt)
Create DataBase:
<?php
$user = "root";
$pass = "";
$con = mysqli_connect("localhost",$user,$pass);
if($con)
{
echo "Connection Success";
$q = "CREATE DATABASE Prime"; // create DataBase Named Prime
if(mysqli_query($con,$q))
{
echo "DataBase is Created";
}
else{
echo "Something Went Wrong";
}
}
else{
die("Connection Failed");
}
?>
Create Table:
<?php
$user = "root";
$pass = "";
$dbname = "prime";
$con = mysqli_connect("localhost",$user,$pass,$dbname);
if($con)
{
echo "Connection Success";
$q = "CREATE Table EmpLoyee(EmpId VARCHAR(20) PRIMARY KEY,
EmpName VARCHAR(20));"; // create Table Named Employee
if(mysqli_query($con,$q))
{
echo "Table is Created";
}
else{
die("Something is wrong");
}
}
else{
die("Connection Failed");
}
?>
Insert Data:
<?php
$user = "root";
$pass = "";
$dbname = "prime";
$con = mysqli_connect("localhost",$user,$pass,$dbname);
if($con)
{
echo "Connection Success<br>";
$q = "INSERT INTO Employee(`EmpId`,`EmpName`)VALUES('EMP01','YOOGLE');"; //
Insert Data
if(mysqli_query($con,$q))
{
echo "Data Insert Success..<br>";
}
else{
die("Something is wrong");
}
}
else{
die("Connection Failed");
}
?>
Update Data:
<?php
$user = "root";
$pass = "";
$dbname = "prime";
$con = mysqli_connect("localhost",$user,$pass,$dbname);
if($con)
{
echo "Connection Success<br>";
$q = "UPDATE Employee SET `EmpName`='YUG' WHERE `EmpId`='EMP01';"; // Update
Data
if(mysqli_query($con,$q))
{
echo "Data Update Success..<br>";
}
else{
die("Something is wrong");
}
}
else{
die("Connection Failed");
}
?>
Delete Data:
<?php
$user = "root";
$pass = "";
$dbname = "prime";
$con = mysqli_connect("localhost",$user,$pass,$dbname);
if($con)
{
echo "Connection Success<br>";
$q = "DELETE FROM Employee WHERE `EmpId`='EMP01';"; // Delete Data
if(mysqli_query($con,$q))
{
echo "Data Delete Success..<br>";
}
else{
die("Something is wrong");
}
}
else{
die("Connection Failed");
}
?>