In Linux, a PHP script is a program written in the PHP programming language that runs on a web server. These script files are typically stored with a .php extension, and they can include a mix of HTML, CSS, and PHP code. They can perform various tasks, including handling form data, generating dynamic web content, processing database queries, and interacting with APIs.
This guide will offer step-by-step instructions to test a PHP script in Linux.
- How to Test a PHP Script?
- Check PHP Installation
- Create a PHP Script
- Test the PHP Script
- Move the PHP Script
- Access the PHP Script
- Verify the Output in Web Browser
How to Test a PHP Script?
When a client requests a PHP script from the web server, the server executes the script and generates HTML content that is sent back to the client’s web browser. To test the PHP script, follow the below steps:
Step 1: Check PHP Installation
First, users need to make sure that PHP is installed on the Linux system. To check the version of PHP installed in the system, execute the below command:
$ php --version
The output shows that php 8.1.2 has been installed in the system.
Note: If the PHP is not installed, users can install it using the link of the Linux distribution.
Step 2: Create a PHP Script
Once users have installed PHP, create a PHP script using a text editor. For instance, create a file named “test.php” with the following code:
$ sudo nano test.php
<?php echo "Hello, world!"; ?>
Save and exit the nano text editor.
Step 3: Test the PHP Script
Once users are in the directory containing the PHP script, run the “test” script file using the “php” command:
$ php test.php
The output shows the “Hello, world!” in the terminal window.
That’s it! This is how you can test a PHP script in a Linux terminal. To access the PHP script in a web browser, follow the below procedure.
Step 4: Move the PHP Script
After creating the PHP script, move it to a directory that is accessible by the web server. For instance, move the “test.php” file to the “/var/www/html” directory on an Apache web server:
$ sudo mv test.php /var/www/html
In this way, the particular test.php file has been successfully moved to the “/var/www/html” directory.
Step 5: Access the PHP Script
To test the PHP script, access the .php file to the web browser. For this, open a web browser and enter the URL of the PHP script in the address bar. For instance, the “test.php” script is saved in the “/var/www/html” directory on an Apache web server; access it by entering the following URL in the web browser:
Type the above URL and hit the “Enter” button.
Step 5: Verify the Output in Web Browser
After accessing the PHP script, the script’s output can be seen in the web browser:
The output shows the text “Hello, world!” has been displayed on the screen.
To test the PHP script in Linux, create a .php script file and run the “php script_name.php” command in the terminal. Also, users can enter the URL “http://localhost/<script_name>.php” of the PHP script in the address bar to access the script file in the web browser. This testing implements various web application features, such as user authentication, session management, and file uploads.
This guide has explained step-by-step procedures to test a PHP script on the Linux system.