"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Wednesday, June 26, 2024

Practical No. 01

 

Practical No. 01:

1.     Install and configure PHP, web server, MYSQL

2.     Write a program to print “Hello, World! ”

 

I Practical Significance:

ü PHP is popular scripting language, which is used to develop various web applications.

ü PHP is also object oriented programming language students will be able to setup PHP environment for executing PHP program or using different server like WAMP or XAMPP server.

 

II. Practical Outcome (PrOs) :

 

ü Install and configure PHP, web server, MYSQL

ü Write a program to print “Hello, World!”

 

III Minimum Theoretical Background

 

INSTALLATION OF PHP

 

Let’s Start :

Installing PHP on your computer allows you to safely develop and test a web application without affecting the live system.

To work with PHP locally, you need to have the following software AMP (Apache, MySQL, PHP):

ü PHP

ü A web server that supports PHP. We’ll use the Apache webserver.

ü A database server. We’ll use the MySQL database server.

Typically, you won’t install all this software separately because connecting them is tricky and not intended for beginners.

There are many AMP options available in the market that are given below:

·                     WAMP for Windows

·                     LAMP for Linux

·                     MAMP for Mac

·                     SAMP for Solaris

·                     FAMP for FreeBSD

·                     XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.

Therefore, it’s easier to find an all-in-one software package that includes PHP, a web server, and a database server. One of the most popular PHP development environments is XAMPP.

XAMPP is an easy install Apache distribution that contains PHP, MariaDB, and Apache webserver. XAMPP supports Windows, Linux, and macOS.

If you are on Windows and don't want Perl and other features of XAMPP, you should go for WAMP.

Download XAMPP

To install XAMPP on windows, you can go to the XAMPP official website and download the suitable version for your platform. Download this according to your Operating system .

Install XAMPP on Windows

To install XAMPP on Windows, you can follow these steps:

Step 1. Start the installation

Double-click the downloaded file to start setting up XAMPP:




Step 2. Select components to install

Select the components that you want to install. In this step, you can select Apache, MySQL, PHP, and phpMyAdmin, deselect other components like the following, and click the Next button to go to the next step.

Step 3. Specifying the installation folder

Select a folder to install XAMPP. It’s recommended to install XAMPP in the c:\xampp folder. Click the Next button to go to the next step.



Step 4. Selecting a language

Select a language for XAMPP Control Panel. By default, it’s English. And you can select your preferred language and click the Next button to go to the next step.

Step 5. Bitnami for XAMPP

Feel free to skip this step because you don’t need Bitnami for learning PHP. Just click the Next button to go to the next step.


Step 6. Begin installing XAMPP

And you’re now ready to install XAMPP. Click the Next button to start the installation. It’ll take a few minutes to complete.

Step 7. Completing the XAMPP setup

Once completed, the XAMPP setup wizard shows the following screen. You can click the Finish button to launch the XAMPP Control Panel:


Step 8. Completing the XAMPP setup

The XAMPP Control Panel lists installed services. To start a service, you click the corresponding Start button:


The following shows the Apache web server and MySQL are up and running. The Apache web server listens on the ports 80 and 443 while the MySQL listens on port 3306:

Step 9. Launch the XAMPP

Open the web browser and navigate to the following URL: http://localhost/. If the installation is completed successfully, you’ll see the welcome screen of the XAMPP.

 

Troubleshooting

By default, Apache uses port 80. However, if port 80 is used by another service, you’ll get an error like this:


In this case, you need to change the port from 80 to a free one, e.g., 8080. To do that, you follow these steps:

First, click the Config button that aligns with the Apache module:


Second, find the line that has the text 
Listen 80 and change the port from 80 to 8080 like this:



 

 

 

Third, click the Start button to start the Apache service. If the port is free, Apache should start properly, as shown in the following picture:

 

IV. How to write the PHP program and How to run the PHP Programs:

How to write a PHP Program:

Create a file named hello.php with the following content:

<?php

echo "Hello, World!\n";

?>

PHP Script Execution Methods:

PHP scripts can be executed in two primary ways:

ü Command-line Scripting

ü Server-side Scripting

1. Command-line Scripting

Definition:   Command-line scripting allows you to run PHP scripts directly from the command line interface (CLI), similar to other scripting languages like Python and Perl.

This method does not require a web server or browser.

Usage:

ü Performing administrative tasks such as sending emails, generating PDF files, and performing file operations.

ü Automating tasks through cron jobs (scheduled tasks on Unix-based systems) or Task Scheduler (on Windows).

ü Running scripts for maintenance tasks such as database backups, data migrations, and more.

How to Run:

1.     Open Terminal/Command Prompt:

ü For Windows: Open Command Prompt or PowerShell.

ü For Mac/Linux: Open Terminal.

2.     Navigate to Script Directory:

ü Use the cd command to navigate to the directory where your PHP script is located.

ü cd path/to/your/script

3.     Execute the Script:

ü  Use the php command followed by the script’s filename.

ü  php script.php

Example:

Create a file named hello.php with the following content:

<?php

echo "Hello, World!\n";

?>

Run this script from the command line:

php hello.php

Output:

Hello, World!

Advantages:

1.     Direct and straightforward execution.

2.     Useful for tasks that do not require a web server.

3.     Can be automated using cron jobs or Task Scheduler.

Security Considerations:

1.     Ensure that scripts executed from the command line have the necessary permissions.

2.     Avoid exposing sensitive information in command-line scripts.

2. Server-side Scripting

Definition:

Server-side scripting involves running PHP scripts on a web server to generate dynamic web pages. The PHP code is processed on the server, and the resulting HTML is sent to the client’s web browser.

Usage:

1.     Developing dynamic websites and web applications.

2.     Handling form submissions and processing user input.

3.     Interacting with databases to fetch, insert, update, or delete data.

4.     Managing sessions and user authentication.

5.     Generating dynamic content such as user profiles, product listings, and more.

How to Run:

Set Up a Web Server:

1.     Install a web server such as Apache, Nginx, or IIS.

2.     Ensure PHP is installed and configured to work with your web server.

3.     Place Script in Document Root:

4.     Save your PHP file in the web server’s document root directory.

Common directories:

XAMPP: htdocs

WAMP: www

MAMP: htdocs

LAMP: /var/www/html

5.     Access Script via Browser:

Open a web browser and navigate to the script’s URL

Example: http://localhost/your-script.php

Example:

Create a file named index.php with the following content:

index.php

<!DOCTYPE html>

<html>

<head>

    <title>My Web Page</title>

</head>

<body>

    <?php

    echo "<h1>Welcome to my website!</h1>";

    ?>

</body>

</html>

Place this file in the document root directory and access it via http://localhost/index.php.

Advantages:

1.     Seamless integration with HTML to create dynamic web pages.

2.     Access to web server features and functionalities.

3.     Ability to handle user inputs and interact with databases.

4.     Supports session management and user authentication.

Security Considerations:

1.     Sanitize and validate all user inputs to prevent security vulnerabilities such as SQL injection and cross-site scripting (XSS).

2.     Use secure session management practices to protect user data.

3.     Configure appropriate file permissions and server settings to protect sensitive files and directories.

 

 

 

Conclusion

By understanding these two methods of running PHP scripts, you can leverage PHP’s versatility for both web-based applications and command-line tasks. Command-line scripting is useful for automation and administrative tasks, while server-side scripting is essential for developing dynamic and interactive web applications.

V. Lab Assignment:


RUN THE PHP FIRST PHP PROGRAM

 

1.  Write a program to print “Hello , World! ” Program.

Code in PHP:

<?php

echo “Hello , World! ”;

?>

Output:

Hello , World!

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template