Assignments Of Class 1: Introduction to PHP

Rashmi Mishra
0

PHP Class 1: Introduction to PHP - Assignments

Assignment 1: What is PHP

Answer:

What is PHP? 

PHP (Hypertext Preprocessor) is a widely-used open-source scripting language designed for web development. 

It is particularly suited for server-side scripting, allowing developers to create dynamic and interactive web pages that can interact with databases and perform complex tasks on the server before sending the output to the user’s browser.

How does PHP integrate with HTML? 

PHP can be embedded directly into HTML code, making it easy to create dynamic web pages. 

When a web server processes a PHP file, it executes the PHP code and outputs the result as HTML, which is then displayed in the user's web browser. This seamless integration allows developers to combine HTML and PHP to create interactive websites.

The importance of PHP in building dynamic websites: 

1.   PHP plays a crucial role in creating dynamic websites, where content can change based on user interactions, database queries, or other conditions. 

2.   It allows websites to deliver personalized experiences, such as user logins, shopping carts, and content management systems. 

3.   PHP’s ability to connect with various databases, like MySQL, makes it a powerful tool for building data-driven web applications.

Examples of popular websites built using PHP:

1.   Facebook: Initially built using PHP, Facebook remains one of the largest and most complex PHP-based web applications.

2.   Wikipedia: The world’s largest online encyclopedia uses PHP to manage its vast content and user base.

3.   WordPress: The most popular content management system (CMS) is built on PHP, powering over 40% of all websites globally.

Assignment 2: Setting Up XAMPP and Running Your First PHP Script

Objective:

To install XAMPP and verify the PHP installation by running a phpinfo() script.


Steps:

1.   Download XAMPP:

o    Go to the XAMPP website.

o    Choose the installer compatible with your operating system (Windows/macOS/Linux).

2.   Install XAMPP:

o    Open the downloaded installer file.

o    Follow the installation steps on-screen, choosing to install Apache, MySQL, and PHP (by default).

o    Select the installation directory (default for Windows is C:\xampp).

3.   Start Apache Server:

o    Open the XAMPP Control Panel.

o    Click “Start” next to Apache.

o    Once Apache is running, the status indicator will turn green.

4.   Create a PHP Script:

o    Navigate to the htdocs directory (located at C:\xampp\htdocs by default).

o    In htdocs, create a new file called info.php.

o    Open info.php in a text editor, and add this code:

<?php

phpinfo();

?>

o    The phpinfo() function outputs information about the PHP installation.

5.   Run the PHP Script in a Browser:

o    Open your web browser.

o    In the address bar, type http://localhost/info.php and press Enter.

o    You should see a page displaying PHP configuration information if everything is installed correctly.

Explanation:

The phpinfo() function outputs detailed information about PHP, including version details, extensions, and configuration settings. This ensures your PHP installation is working and shows you the current configuration.

Expected Result:

A page with a detailed PHP configuration list should appear, confirming that PHP is installed and running correctly.


Assignment 3: Creating and Running a Simple "Hello World" Script

Objective:

Create a basic PHP file that displays “Hello, PHP World!” when accessed in a browser.


Steps:

1.   Ensure XAMPP is Running:

o    Make sure Apache is started in the XAMPP Control Panel. If Apache isn’t running, click “Start.”

2.   Create a New PHP File:

o    In the htdocs directory, create a file named hello.php.

3.   Write the PHP Code:

o    Open hello.php in a text editor and type the following code:

<?php

echo "Hello, PHP World!";

?>

o    Here, echo is a PHP command that outputs text to the browser.

4.   Run the PHP File in a Browser:

o    Open a browser and type http://localhost/hello.php in the address bar, then press Enter.

5.   Observe the Output:

o    You should see the message “Hello, PHP World!” displayed in your browser.

Explanation:

This simple script introduces PHP syntax and outputs a message. The echo statement in PHP is similar to print statements in other languages and is used to display text or variables.

Expected Result:

The text "Hello, PHP World!" should be visible on the webpage, confirming PHP is working and outputting content as expected.


Assignment 4: Displaying Server Information Using PHP Variables

Objective:

Use PHP to display server details like the server name and PHP version by storing information in variables.


Steps:

1.   Create a New PHP File:

o    Inside the htdocs directory, create a file named server_info.php.

2.   Write PHP Code with Variables:

o    Open server_info.php in a text editor and add the following code:

<?php

$server_name = $_SERVER['SERVER_NAME'];

$php_version = PHP_VERSION;

echo "Server Name: " . $server_name . "<br>";

echo "PHP Version: " . $php_version;

?>

o    $server_name uses the $_SERVER['SERVER_NAME'] variable to fetch the server's name.

o    $php_version uses the predefined PHP_VERSION constant to display the current PHP version.

3.   Run the PHP File in a Browser:

o    Open a browser and navigate to http://localhost/server_info.php.

4.   Observe the Output:

o    You should see output like this:

Server Name: localhost

PHP Version: X.Y.Z

Explanation:

In this assignment, PHP variables are used to store server information, which is then outputted to the browser. $_SERVER is a global array in PHP that holds server-related information, and PHP_VERSION is a constant that returns the version of PHP in use.

Expected Result:

The browser displays the server name and PHP version, demonstrating how PHP retrieves server information.


Assignment 5: Basic HTML and PHP Integration

Objective:

Combine PHP and HTML in a single file to display a simple greeting message within an HTML structure.


Steps:

1.   Create a New PHP File:

o    In the htdocs directory, create a file named greeting.php.

2.   Write HTML and PHP Code:

o    Open greeting.php in a text editor and type the following code:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Greeting Page</title>

</head>

<body>

    <h1>Welcome to My PHP Page</h1>

    <p>

        <?php

        $greeting = "Hello, visitor! Welcome to learning PHP!";

        echo $greeting;

        ?>

    </p>

</body>

</html>

o    This file contains a basic HTML structure with a <p> tag that displays a message.

o    Inside the <p> tag, PHP is used to define a variable $greeting and display its content using echo.

3.   Run the File in a Browser:

o    Open a browser and go to http://localhost/greeting.php.

4.   Observe the Output:

o    The page should display:

Welcome to My PHP Page

Hello, visitor! Welcome to learning PHP!

Explanation:

This exercise demonstrates how PHP can be embedded within HTML. The PHP code is processed on the server, while the HTML is rendered in the browser. This combination allows developers to build dynamic content while keeping a structured HTML layout.

Expected Result:

The webpage displays a greeting message within an HTML structure, showcasing the integration of PHP and HTML.


Assignment  6: Create a PHP Calculator for Addition

Objective:

Build a simple PHP script that takes two numbers, adds them, and displays the result.


Steps:

1.   Create a New PHP File:

o    In the htdocs directory, create a file named calculator.php.

2.   Write the PHP and HTML Form Code:

o    Open calculator.php in a text editor and add the following code:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Simple Calculator</title>

</head>

<body>

    <h1>PHP Calculator</h1>

    <form method="post" action="">

        <label for="num1">Number 1:</label>

        <input type="number" name="num1" required>

        <br>

        <label for="num2">Number 2:</label>

        <input type="number" name="num2" required>

        <br>

        <button type="submit">Add</button>

    </form>

 

    <?php

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $num1 = $_POST['num1'];

        $num2 = $_POST['num2'];

        $sum = $num1 + $num2;

        echo "<h3>Result: $num1 + $num2 = $sum</h3>";

    }

    ?>

</body>

</html>

3.   Run the File in a Browser:

o    Go to http://localhost/calculator.php and enter two numbers in the form fields.

4.   Observe the Output:

o    When you click "Add," the result of the addition should appear below the form.

Explanation:

This assignment shows how PHP can handle form data. When the form is submitted, PHP retrieves the numbers entered, adds them, and displays the result.

Expected Result:

The form collects two numbers and displays their sum, illustrating how PHP can interact with HTML forms.


Assignment 7: Creating a Simple Web Page with PHP

Answer:

Created a new file named welcome.php.

Added the following PHP script:

<?php

echo "Welcome! Today's date is " . date('Y-m-d') . " and the current time is " . date('H:i:s');

?>

 Saved the file in the htdocs directory and started the Apache server using XAMPP.

Opened a web browser and navigated to http://localhost/welcome.php.

The web page displayed the current date and time, confirming that the PHP script executed correctly.

Output of the welcome.php script showing the current date and time.

 

Post a Comment

0Comments

Post a Comment (0)