Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

PHP File

Uploaded by

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

PHP File

Uploaded by

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

Experiment No 1.

Hello World Program

<?php

echo "Hello, World!";

?>

Experiment No. 2.

Variable Declaration and Output

<?php

$name = "John";

$age = 25;

echo "Name: $name <br>";

echo "Age: $age <br>";

?>

Experiment No. 3.

Simple Arithmetic Operations

<?php

$num1 = 10;

$num2 = 5;

$sum = $num1 + $num2;

$diff = $num1 - $num2;

$prod = $num1 * $num2;

$quot = $num1 / $num2;


echo "Sum: $sum <br>";

echo "Difference: $diff <br>";

echo "Product: $prod <br>";

echo "Quotient: $quot <br>";

?>

Experiment No 4.

If-Else Condition

<?php

$num = 4;

if ($num > 0) {

echo "$num is positive.";

} else {

echo "$num is negative.";

?>

Experiment No. 5.

While Loop

`while` loop that prints numbers from 1 to 5.

<?php

$i = 1;

while ($i <= 5) {

echo "Number: $i <br>";

$i++;
}

?>

Experiment No. 6.

For Loop

print numbers from 1 to 5.

<?php

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

echo "Number: $i <br>";

?>

Experiment No. 7.

Function Example

A simple function to add two numbers.

<?php

function add($a, $b) {

return $a + $b;

$result = add(10, 20);

echo "The sum is: $result";

?>

Function 8.

Array Example
<?php

$fruits = array("Apple", "Banana", "Orange");

foreach ($fruits as $fruit) {

echo "$fruit <br>";

?>

Experiment 9.

Form Handling

A simple form that takes user input and processes it using PHP.

HTML Form (index.html)

<form method="post" action="process.php">

Name: <input type="text" name="name"><br>

Age: <input type="text" name="age"><br>

<input type="submit" value="Submit">

</form>

PHP Form Processing (process.php)

<?php

$name = $_POST['name'];

$age = $_POST['age'];

echo "Hello, $name. You are $age years old.";

?>

Experiment No. 10.


Simple Calculator

A simple calculator program that adds, subtracts, multiplies, and divides two numbers using a form.

HTML Form (calculator.html)

<form method="post" action="calculator.php">

Number 1: <input type="number" name="num1"><br>

Number 2: <input type="number" name="num2"><br>

Operation:

<select name="operation">

<option value="add">Add</option>

<option value="subtract">Subtract</option>

<option value="multiply">Multiply</option>

<option value="divide">Divide</option>

</select><br>

<input type="submit" value="Calculate">

</form>

PHP Calculator Processing (calculator.php)

<?php

$num1 = $_POST['num1'];

$num2 = $_POST['num2'];

$operation = $_POST['operation'];

switch ($operation) {

case 'add':

$result = $num1 + $num2;

break;

case 'subtract':
$result = $num1 - $num2;

break;

case 'multiply':

$result = $num1 * $num2;

break;

case 'divide':

if ($num2 != 0) {

$result = $num1 / $num2;

} else {

$result = "Cannot divide by zero.";

break;

default:

$result = "Invalid operation.";

break;

echo "Result: $result";

?>

Experiment 11.

Working with Dates

display the current date and time.

<?php

echo "Current Date and Time: " . date("Y-m-d H:i:s");

?>
Experiment No. 12.

PHP Super globals

An example of using the `$_GET` superglobal to retrieve data from a URL.

<!-- index.html -->

<form method="get" action="get.php">

Name: <input type="text" name="name"><br>

<input type="submit" value="Submit">

</form>

<!-- get.php -->

<?php

$name = $_GET['name'];

echo "Hello, $name!";

?>

You might also like