0% found this document useful (0 votes)
4 views50 pages

php file

The document provides a comprehensive introduction to PHP, including its syntax, installation steps for XAMPP, and various PHP programming examples such as printing text, performing arithmetic operations, and handling user input. It also covers database operations, cookie management, session handling, and file manipulation in PHP. Each section includes coding examples with corresponding outputs to demonstrate functionality.

Uploaded by

kunwar.chawla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views50 pages

php file

The document provides a comprehensive introduction to PHP, including its syntax, installation steps for XAMPP, and various PHP programming examples such as printing text, performing arithmetic operations, and handling user input. It also covers database operations, cookie management, session handling, and file manipulation in PHP. Each section includes coding examples with corresponding outputs to demonstrate functionality.

Uploaded by

kunwar.chawla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 50

1.

Introduction to PHP and Syntax


Introduction:- The term PHP is an acronym for “PHP: Hypertext Preprocessor”. PHP
is a server-side scripting language designed specifically for web development. It is open-
source which means it is free to download and use. The files have the extension “.php”.
PHP was originally acronym for “Personal Home Page”
Rasmus Lerdorf in 1994 inspired the first version of PHP and participated in the later
versions. It is an interpreted language and it does not require a compiler.
 PHP code is executed in the server.
 It can be integrated with many databases such as Oracle, Microsoft SQL Server,
MySQL, PostgreSQL, Sybase, and Informix.
Syntax:- The structure which defines PHP computer language is called PHP syntax.
The PHP script is executed on the server and the HTML result is sent to the browser.
PHP scripts can be written anywhere in the document within PHP tags along with normal
HTML.
The script starts with <?php and ends with ?>. Writing the PHP code inside <?php
….?> is called Escaping to PHP.

<?php
\\ PHP code goes here
?>

1
2. Installing a XAMPP Server steps
Step 1: Download: XAMPP is a release made available by the non-profit project
Apache Friends. Versions with PHP 5.5, 5.6, or 7 are available for download on
the Apache Friends website.

Step 2: Run .exe file: Once the software bundle has been downloaded, you can start
the installation by double clicking on the file with the ending .exe.

Step 3: Start the setup wizard: After you’ve opened the .exe file (after
deactivating your antivirus programs) then, click on ‘Next’ to configure the installation
settings.

2
Step 4: Choose software components: For a full local test server, we recommend
you install using the standard setup and all available components. After making your
choice, click ‘Next’.

Step 5: Choose the installation directory: In this next step, you have the chance
to choose where you’d like the XAMPP software packet to be installed. If you opt for the
standard setup, then a folder with the name XAMPP will be created under C:\ for you.
After you’ve chosen a location, click ‘Next’.

3
Step 6: Start the installation process: Click to start the installation. The setup
wizard will unpack and install the selected components and save them to the designated
directory. This process can take several minutes in total.

Step 7: Complete installation: Once all the components are unpacked and installed,
you can close the setup wizard by clicking on ‘Finish’. Click to tick the corresponding
check box and open the XAMPP Control Panel once the installation process is finished .

4
Output:

5
3. Write a program to print Hello in PHP
Coding:
<html>
<head>
<title> First page </title>
</head>
<body>
<h1>
<?php
echo"Hello World";
?>
</h1>
</body>
</html>

6
Output:

7
4. Write a program to addition two number PHP
Coding:
<html>
<head>
<title> Addition </title>
</head>
<body>
<h1>
<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>
</h1>
</body>
</html>

8
Output:

9
5. Write a program to print factorial of a number in PHP
Coding:
<html>
<head>
<title> Factorial </title>
</head>
<body>
<h1>
<?php
$num=5;
$fac=1;
for($x=$num;$x>=1;$x--)
{
$fac=$fac*$x;
}
echo "Factorial of $num is $fac";
?>
</h1>
</body>
</html>

10
Output:

11
6. Write a program call by value in PHP
Coding:
<html>
<head>
<title> Call by Value </title>
</head>
<body>
<h1>
<?php
function abc($x)
{
$x=$x+10;
return($x);
}
$a=20;
echo abc($a)."<br>";
echo $a;
?>
</h1>
</body>
</html>

12
Output:

13
7. Write a program call by reference in PHP
Coding:
<html>
<head>
<title> Call by Reference </title>
</head>
<body>
<h1>
<?php
function abc(&$x)
{
$x=$x+10;
return($x);
}
$a=20;
echo abc($a)."<br>";
echo $a;
?>
</h1>
</body>
</html>

14
Output:

15
8. Write a program of recursion in PHP
Coding:
<html>
<head>
<title> Recursion </title>
</head>
<body>
<h1>
<?php
function dis($n)
{
if($n<=5)
{
echo "$n <br>";
dis($n+1);
}
}
dis(1);
?>
</h1>
</body>
</html>

16
Output:

17
9. Write a program in PHP to show string function
Coding:
<html>
<head>
<title> String function </title>
</head>
<body>
<h1>
<?php
echo strlen("Hello world")."<br>";
echo strcmp("Hello Werld","Hello World")."<br>";
echo ("Hello World")."<br>";
echo chr(65)."<br>";
echo strtoupper("HELLO world")."<br>";
echo strtolower("HELLO world")."<br>";
echo ucfirst("hello WORLD")."<br>";
echo lcfirst("HELLO world")."<br>";
?>
</h1>
</body>
</html>

18
Output:

19
10. Write a program in PHP to show array function
Coding:
<html>
<head>
<title> Array Function </title>
</head>
<body>
<h1>
<?php
$cars=array("BMW","MERCEDES","AUDI","BENTLEY");
$arrlenght= count($cars);
echo current($cars)."<br>";
echo next($cars)."<br>";
echo next($cars)."<br>";
echo prev($cars)."<br>";
for($x=0;$x<$arrlenght;$x++) {
echo $cars[$x];
echo"<br>"; }
echo reset($cars)."<br>";
echo end($cars)."<br>";
?>
</h1>
</body>
</html>

20
Output:

21
11.Write a program in PHP to show importing and Accessing
User Input
Coding for Importing User Data:
<html>
<head>
<title> Importing User Input </title>
</head>
<body>
<h1>
<form action="accessing.php" method="post">
Name: <input type="text" name="name"><br><br>
E-Mail: <input type="text" name="email"><br><br>
<input type="submit" ale="submit">
</form>
</h1>
</body>
</html>

22
Output:

23
Coding for Accessing User Data:
<html>
<head>
<title> Accessing User Data </title>
</head>
<body>
<h1>
Welcome
<?php
echo $_POST["name"];
?><br><br>
Your E-Mail Address is
<?php
echo $_POST["email"];
?>
</h1>
</body>
</html>

24
Output:

25
12.Write a program in PHP to create database and database table
Coding:
<html>
<head>
<title> Creating Database </title>
</head>
<body>
<h1>
<?php
$con=mysqli_connect("localhost","root","");
$sql="CREATE DATABASE COLLEGE";
if(mysqli_query($con,$sql)) {
echo "Sucessfully Created Database"; }
else {
echo "Failed to create Database";}
mysqli_close($con);
$con=mysqli_connect("localhost","root","","college");
$sql="CREATE TABLE BCA(fname CHAR(30),lname
CHAR(20),sem INT)";
if(mysqli_query($con,$sql)) {
echo "<br><br>Sucessfully Created Table"; }
else {
echo "<br><br>Failed to create Table";}
?>
</h1>
</body>
</html>

26
Output:

27
13.Write a program in PHP to insert data in database
Coding:
<html>
<head>
<title> Creating Database </title>
</head>
<body>
<h1>
<?php
$con=mysqli_connect("localhost","root","","college");
mysqli_query($con,"INSERT INTO BCA
VALUES('RAJ','KUMAR',3)");
mysqli_query($con,"INSERT INTO BCA
VALUES('SIMRAN','JEET',5)");
mysqli_query($con,"INSERT INTO BCA
VALUES('PREM','DHILON',1)");
echo "Records are Successfully Inserted";
?>
</h1>
</body>
</html>

28
Output:

29
14. Write a program in PHP to select data from a database table
Coding:
<html>
<head>
<title> Creating Database </title>
</head>
<body>
<h1>
<?php
$con=mysqli_connect("localhost","root","","college");
$result=mysqli_query($con,"SELECT * FROM BCA");
while($row=mysqli_fetch_array($result))
{
echo $row['fname']." ".$row['lname']." ".$row['sem'];
echo "<br>";
}
?>
</h1>
</body>
</html>

30
Output:

31
15.Write a program to create a cookie in PHP
Coding:
<html>
<head>
<title>cookie</title>
<?php
setcookie("message","PHP Programming");
?>
</head>
<body>
<h1> create cookie
<?php
echo "<br><br>The Cookie is set";
?>
</h1>
</body>
</html>

32
Output:

33
16. Write a program to accessing the cookie in PHP

Coding:
<html>
<head>
<title>cookie</title>
<?php
setcookie("message","PHP Programming");
?>
</head>
<body>
<h1>
<?php
echo $_COOKIE["message"];
?>
</h1>
</body>
</html>

34
Output:

35
17.Write a program to delete a cookie
Coding:
<html>
<head>
<title>Deleting cookie</title>
<?php
setcookie("message","PHP Programming",time()-3600);
?>
</head>
<body>
<h1>
<?php
echo "Deleted Cookie";
?>
</h1>
</body>
</html>

36
Output:

37
18.Write a program to create a session in PHP
Coding:
<?php
session_start();
if(isset($_SESSION['counter'])) {
$_SESSION['counter']+=1;
}
else{
$_SESSION['counter']=1;
}
$msg = "You have visited ".$_SESSION['counter'];
$msg .= " time this page in current session.";
?>
<html>
<head>
<title> Creating a Session in PHP </title>
</head>
<body>
<h1>
<?php echo($msg); ?>
</h1>
</body>
</html>

38
Output:

39
19.Write a program to show working of php session in PHP
Coding:
<html>
<body>
<h1>
<?php
Session_start();
$_SESSION["name"] = "Sushil";
$_SESSION["city"] = "Patiala";
$enc_session = session_encode();
print "<b>Encoded Session Data: <br/></b>";
print $enc_session. "<br/><br/>";
$_SESSION["name"] = "Puneet";
$_SESSION["city"] = "Khanna";
print "<b>SESSION Array: <br/></b>";
print "<pre>"; print_r($_SESSION);
print "</pre>";
session_decode ($enc_session);
print "<b>Reloaded SESSION Array: <br/></b>";
print "<pre>"; print_r($_SESSION);
print "</pre>";
?>
</h1>
</body>
</html>

40
File named one.txt:

Output:

41
20.Write a program to show opening a file
Coding:
<html>
<body>
<h1>
<?php
$myfile="one.txt";
//name of the file is one.txt
$fp=fopen($myfile,'r');
if($fp == false)
{
echo ("error");
exit();
}
else{
echo("File has Opened Successfully");
}
?>
</h1>
</body>
</html>

42
Output:

43
21.Write a program to show reading a file
Coding:
<?php
$myFile = "one.txt";
$fp = fopen($myFile, 'r');
if ($fp == false){
echo ("Error in opening new file");
exit();
}
$fsize= filesize ($myFile);
$filetext = fread($fp, $fsize);
echo ("<pre> $filetext</pre>");
fclose($fp);
?>

44
Output:

45
22.Write a program to show copying a file
Coding:
<?php
$fn= "one.txt";
$newfn= "two.txt";
if (copy($fn,$newfn)){
echo "The file was copied successfully";
}
else
echo "An error occurred during copying the file";
fclose($fn);
fclose($newfn);
?>

46
Output:

47
23.Write a program to show renaming a file
Coding:
<?php
$fn= "one.txt";
$newfn= "three.txt";
if (rename($fn, $newfn)){
echo sprintf("%s was renamed to %s", $fn, $newfn);
}
else{
echo "An error occurred during renaming the file";
}
?>

48
Output:

49
24.Write a program to show deleting a file
Coding:
<?php
$fn= "two.txt";
if (unlink($fn)){
echo sprintf("The file %s deleted successfully", $fn);
}
else{
echo sprintf("An error occurred deleting the file %s", $fn);
}
?>

50

You might also like