PHP Lab - Iv Sem - Bca
PHP Lab - Iv Sem - Bca
Aim : To create a basic student registration form and add validations using javascript
Description :
Forms in javascript are used to collect user input and data. A form is created using html tags
and can be manipulated using javascript. Forms contain input elements such as text boxes,
radio buttons, checkboxes, drop-down lists, etc. Which allow the user to input data.
Source code :
<!Doctype html>
<html>
<head>
<script>
Function validateform() {
// validate name
Return false;
// validate email
If (!Emailregex.test(email)) {
Return false;
P.V.V.SANDEEP MCA 1
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
If (!Phoneregex.test(phone)) {
Return false;
</script>
</head>
<body>
<label for="Name">name:</label>
<label for="Email">email:</label>
<label for="Phone">phone:</label>
</form>
</body>
</html>
Output :
P.V.V.SANDEEP MCA 2
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
2. Create a php program to find odd or even number from given number
Aim : To create a php program to find odd or even number from given number
Description :
The program assigns the desired number to the $number variable. It then uses the modulo
operator (%) to check if the number is divisible evenly by 2. If the result is 0, it indicates an
even number, and the program displays an appropriate message. Otherwise, it indicates an
odd number, and a different message is displayed
Source code :
<?Php
If ($number % 2 == 0) {
} else {
?>
Output :
15 is an odd number.
P.V.V.SANDEEP MCA 3
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
Description :
The program defines a function findmaximum() that takes three numbers as inputs. The max()
function is used to compare the three numbers and return the maximum among them.
The program then demonstrates the usage by assigning values to the $number1, $number2,
and $number3 variables. The findmaximum() function is called with these variables, and the
result is stored in the $maximum variable. Finally, the maximum number is displayed using
echo.
Source code :
<?Php
// example usage
$number1 = 15;
$number2 = 27;
$number3 = 9;
Echo "The maximum number among {$number1}, {$number2}, and {$number3} is:
{$maximum}";
?>
Output :
Description :
P.V.V.SANDEEP MCA 4
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
We have an array called $fruits that contains different fruit names. We use the count()
function to determine the length of the array and store it in the $length variable.
Next, we initialize a counter variable $i with a value of 0. The while loop runs as long as the
value of $i is less than the $length. Inside the loop, we access each array element using the $i
index and display it using echo. We then increment the counter variable $i by 1 in each
iteration.
The while loop continues until $i reaches the value of $length, ensuring that we access all the
elements of the $fruits array.
Source code :
<?Php
// example array
$length = count($fruits);
$i = 0;
$i++;
?>
Output :
Fruit: Apple
Fruit: Banana
Fruit: Orange
Fruit: Mango
P.V.V.SANDEEP MCA 5
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
Fruit: Grapes
Description :
We have an array called $fruits that contains different fruit names. The foreach loop allows us
to iterate over each element of the array.
Inside the foreach loop, we assign each array element to the variable $fruit in each iteration.
We then use echo to display the current fruit using $fruit variable.
The foreach loop automatically handles the iteration over each element of the array, and the
loop continues until all elements have been processed.
Output :
<?Php
// example array
?>
Output :
Fruit: Apple
Fruit: Banana
Fruit: Orange
Fruit: Mango
Fruit: Grapes
P.V.V.SANDEEP MCA 6
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
Description :
Strings can arrive from many sources, including user input, databases, files, and web pages.
Before you begin to work with data from an external source, you often will need to find out
more about it. Php provides many functions that enable you to acquire information about
strings.
Source code:
<?Php
$test = "Scallywag";
Print $test[0];
If (strlen($membership) == 4) {
} else {
$membership = "Pab7";
If (strstr($membership, "Ab")) {
Print "Thank you. Don't forget that your membership expires soon!";
} else {
$membership = "Mz00xyz";
$test = "Scallywag";
P.V.V.SANDEEP MCA 7
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
Output :
Hello
Mz
Wag
Wa
Description :
Php includes a lot of date and time functions. These functions are coming under php date and
time-related extensions.these are used to perform various operations with date and time, like
getting date-time information based on the input or parameters passed, performing date time
manipulation, converting date input formats from one another and more.
Source code :
<?Php
$mydate=getdate(date("U"));
?>
P.V.V.SANDEEP MCA 8
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
Output:
Aim : To write a php program to perform read and write operations on a file
Description :
We specify the file path as $file_path (replace it with the actual path of your file).
We write content to the file using file_put_contents(). In this case, we write the string "This is
some content to write to the file." To the file specified by $file_path.
We read the content of the file using file_get_contents(). The content of the file is stored in
the variable $content_read.
Source code :
<?Php
// file path
$file_path = 'example.txt';
// write to file
File_put_contents($file_path, $content_to_write);
$content_read = file_get_contents($file_path);
Echo $content_read;
?>
Output :
P.V.V.SANDEEP MCA 9
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
File content:
Description :
In php you can easily do this using the mysqli_connect() function. All communication between
php and the mysql database server takes place through this connection. Here're the basic
syntaxes for connecting to mysql using mysqli and pdo extensions.
Source code :
<?Php
If (!$conn) {
Global $conn;
$query = "Select * from users where username = '$username' and password = '$password'";
If (mysqli_num_rows($result) == 1) {
} else {
If (isset($_post['submit'])) {
$username = $_post['username'];
$password = $_post['password'];
If (authenticateuser($username, $password)) {
} else {
?>
<!Doctype html>
<html>
<head>
<title>user login</title>
</head>
P.V.V.SANDEEP MCA 11
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
<body>
<h1>user login</h1>
<label for="Username">username:</label>
<label for="Password">password:</label>
</form>
</body>
</html>
Output :
Successful login
Failed login
Description :
When the form is submitted (method="Post"), the php code is executed to handle the file
upload.
The code checks if the upload directory exists and creates it if necessary.
If the file was uploaded without errors (upload_err_ok), the file name is extracted and the file
is moved from the temporary location to the target directory using move_uploaded_file().
P.V.V.SANDEEP MCA 12
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
If the file upload is successful, a success message is displayed. Otherwise, an appropriate error
message is shown.
The html form includes an input field of type "File" (name="Filetoupload") and the enctype
attribute is set to "Multipart/form-data" To enable file uploads.
Source code :
<?Php
If ($_server["Request_method"] == "Post") {
If (!Is_dir($uploaddir)) {
Mkdir($uploaddir);
$uploadedfile = $_files["Filetoupload"];
$filename = basename($uploadedfile["Name"]);
If (move_uploaded_file($uploadedfile["Tmp_name"], $targetpath)) {
} else {
} else {
}
P.V.V.SANDEEP MCA 13
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
?>
<!Doctype html>
<html>
<head>
<title>file upload</title>
</head>
<body>
<h1>file upload</h1>
<br><br>
</form>
</body>
</html>
Output :
Description : A cookie is a small text file that lets you store a small amount of data (nearly 4kb)
on the user's computer. They are typically used to keeping track of information such as
P.V.V.SANDEEP MCA 14
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
username that the site can retrieve to personalize the page when user visit the website next
time.
The setcookie() function is used to set a cookie in php. Make sure you call
the setcookie() function before any output generated by your script otherwise cookie will not
set.
Source code :
<?Php
// set a cookie
$cookiename = "User";
If (isset($_cookie[$cookiename])) {
$cookievalue = $_cookie[$cookiename];
} else {
?>
Output :
Description :
P.V.V.SANDEEP MCA 15
PRAGATI WOMENS DEGREE COLLEGE PHP & MYSQL II BCA – IV SEM
A php session stores data on the server rather than user's computer. In a session based
environment, every user is identified through a unique number called session identifier or sid.
This unique session id is used to link each user with their own information on the server like
emails, posts, etc.
Source code :
<?Php
Session_start();
$_session['username'] = 'johndoe';
$_session['role'] = 'admin';
$username = $_session['username'];
$role = $_session['role'];
?>
Output :
Username: Johndoe
Role: Admin
P.V.V.SANDEEP MCA 16