You are on page 1of 10

1.) Write a PHP program Display "Goldengate International Collage" .

<!DOCTYPE html>

<html>

<head>

<title>My PHP Program</title>

</head>

<body>

<?php

echo "Goldengate International College";

?>

</body>

</html>

Output :

2.) To calculate the sum of two numbers with and without using PHP function
With PHP
// Define the two numbers
$num1 = 10;
$num2 = 20;

// Calculate the sum


$sum = $num1 + $num2;

// Output the result


echo "The sum of $num1 and $num2 is: $sum";
Output :

Without PHP :
// Define a function to calculate the sum
function sum($num1, $num2) {
return $num1 + $num2;
}

// Call the function with the two numbers


$result = sum(10, 20);

// Output the result


echo "The sum of 10 and 20 is: $result";

Output :

3.) To find the greatest number among three number using PHP .

// Define the three numbers

$num1 = 10;

$num2 = 20;

$num3 = 15;

// Check which number is the greatest

if ($num1 > $num2 && $num1 > $num3) {


echo "$num1 is the greatest number.";

} elseif ($num2 > $num1 && $num2 > $num3) {

echo "$num2 is the greatest number.";

} else {

echo "$num3 is the greatest number.";

Output :

4.) Display Multiplication table of 23 using PHP .


// Define the number to create the multiplication table
$num = 23;

// Display the multiplication table


for ($i = 1; $i <= 10; $i++) {
$result = $num * $i;
echo "$num x $i = $result<br>";
}
Output :
5.) Find the factorial of a positive number using PHP function .

// Define a function to calculate the factorial of a number


function factorial($num) {
// Base case: 0! and 1! are both 1
if ($num == 0 || $num == 1) {
return 1;
} else {
// Recursive case: n! = n * (n-1)!
return $num * factorial($num - 1);
}
}

// Call the function with the number whose factorial you want to find
$number = 5;
$result = factorial($number);

// Output the result


echo "The factorial of $number is: $result";

Output:

6.) Demonstrate Associative Array.

// Define an associative array of fruits and their prices


$fruits = array(
"apple" => 1.99,
"banana" => 0.99,
"orange" => 1.49,
"grape" => 2.99
);

// Access and output the values in the associative array


echo "The price of an apple is: $" . $fruits["apple"] . "<br>";
echo "The price of a banana is: $" . $fruits["banana"] . "<br>";
echo "The price of an orange is: $" . $fruits["orange"] . "<br>";
echo "The price of a grape is: $" . $fruits["grape"] . "<br>";
Output:

7.) Input user detail (id, name ,email , password) using form and display to other page using php .
<!DOCTYPE html>
<html>
<head>
<title>User Details Form</title>
</head>
<body>
<h2>User Details Form</h2>
<form action="display.php" method="post">
<label>ID:</label>
<input type="text" name="id"><br><br>

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

<label>Email:</label>
<input type="email" name="email"><br><br>

<label>Password:</label>
<input type="password" name="password"><br><br>

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


</form>
</body>
</html>
8.) Create a database named db user using PHP and MYSQL.
// Replace the values with your MySQL credentials
$host = 'localhost';
$username = 'root';
$password = 'password';

// Create a connection
$conn = new mysqli($host, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE db_user";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}

// Close connection
$conn->close();

Output :

9.) Create table user(id, name, email, password) in the above database using PHP.
// Replace the values with your MySQL credentials
$host = 'localhost';
$username = 'root';
$password = 'password';
$dbname = 'db_user';

// Create a connection
$conn = new mysqli($host, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Create table
$sql = "CREATE TABLE user (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
)";

if ($conn->query($sql) === TRUE) {


echo "Table user created successfully";
} else {
echo "Error creating table: " . $conn->error;
}

// Close connection
$conn->close();

Output :

10.)Insert five records in a table user with fields id, name, email, password using PHP.
// Replace the values with your MySQL credentials
$host = 'localhost';
$username = 'root';
$password = 'password';
$dbname = 'db_user';

// Create a connection
$conn = new mysqli($host, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Insert records
$sql = "INSERT INTO user (name, email, password) VALUES
('John Doe', 'johndoe@example.com', 'password123'),
('Jane Smith', 'janesmith@example.com', 'password456'),
('Bob Johnson', 'bobjohnson@example.com', 'password789'),
('Alice Lee', 'alicelee@example.com', 'password012'),
('Tom Davis', 'tomdavis@example.com', 'password345')
";
if ($conn->query($sql) === TRUE) {
echo "Records inserted successfully";
} else {
echo "Error inserting records: " . $conn->error;
}

// Close connection
$conn->close();

Output:

11.) Select or display user records by retrieving data from user table using PHP.

// Replace the values with your MySQL credentials

$host = 'localhost';

$username = 'root';

$password = 'password';

$dbname = 'db_user';

// Create a connection

$conn = new mysqli($host, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

// Retrieve data
$sql = "SELECT * FROM user";

$result = $conn->query($sql);

// Display data

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {

echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";

} else {

echo "0 results";

// Close connection

$conn->close();

Output:

You might also like