0% found this document useful (0 votes)
26 views

PHP_Practical

Uploaded by

zakeedadu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

PHP_Practical

Uploaded by

zakeedadu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PHP Practical

1. Write a Program to print Hello World.

<?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;

for ($i = 1; $i <= $no; $i++) {


$fact *= $i;
}

echo "Factorial of $no is: $fact";


?>

4. Write a Program to check number is Odd or Even.

<?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";
}
?>

6. Write a Program to Merge 2 Array.

<?php
$a = array(1, 2, 3);
$b = array(4, 5, 6);

$mergedArray = array_merge($a, $b);


print_r($mergedArray);
?>

7. Write a Program to Check number is Palindrome or not.

<?php
$num = "121";
$rev = strrev($num);
if ($num == $rev) {
echo "$num is Palindrome";
} else {
echo "$num is not Palindrome";
}
?>

8. Write a Program to compare 2 variables value using readline method.

<?php
$a = readline("Enter the value of A: ");
$b = readline("Enter the value of B: ");

if($a > $b) {


echo "$a is Greaterthan $b ";
}

else {
echo "$a is Lessthan $b ";
}
?>

9. Write a Program to implement ternary operator using readline method.

<?php
$a = readline("Enter the value of A: ");
$b = readline("Enter the value of B: ");
echo $a > $b ? $a : $b;
?>

10. Write a Program to generate a table using readline method.

<?php
$a = readline("Enter the num to generate the table: ");

for($i = 1; $i <= 10; $i++) {


$ans = $a * $i;
echo "$a x $i = $ans\n";

}
?>

11. Write a Program to Generate Star pattern.

<?php
$a= readLine("Enter num to generate pattern: ");
for($i = 1; $i <= $a; $i++)
{
for($j = 1; $j <= $i; $j++)
{
echo "*";
}
echo "\n";
}
?>

12. Write a Program to Generate number pattern.

<?php
$a= readLine("Enter num to generate pattern: ");

for($i = 1; $i <= $a; $i++)


{
for($j = 1; $j <= $i; $j++)
{
echo "$j";
}
echo "\n";
}
?>

13. Write a Program to Generate ABCD pattern.

<?php
$a= readLine("Enter num to generate pattern: ");
$asc = 65;

for($i = 1; $i <= $a; $i++) {


for($j = 1; $j <= $i; $j++) {
echo chr($asc);
$asc++;
}
echo "\n";
}
?>

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";
}
?>

17 Write PHP Code to Write in Hello.txt 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);
}
?>

18.Write PHP Code to use file Exist

<?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);
?>

If File Is Not Exist:


If File Is Exist

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";
}
?>

21.exception program in php(try, catch ,finally add 3 programs)

<?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");
}
?>

You might also like