Php script for linux

How to Test a PHP Script in Linux?

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:

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:

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:

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

Читайте также:  Linux and serial port

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.

Conclusion

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/.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.

Источник

How to Use and Execute PHP Codes in Linux Command Line – Part 1

PHP is an open source server side scripting Language which originally stood for ‘Personal Home Page‘ now stands for ‘PHP: Hypertext Preprocessor‘, which is a recursive acronym. It is a cross platform scripting language which is highly influenced by C, C++ and Java.

Run PHP Codes in Linux Command Line

A PHP Syntax is very similar to Syntax in C, Java and Perl Programming Language with a few PHP-specific feature. PHP is used by some 260 Million websites, as of now. The current stable release is PHP Version 5.6.10.

Читайте также:  Canon lbp 2900 драйвер astra linux ppd

PHP is HTML embedded script which facilitates developers to write dynamically generated pages quickly. PHP is primarily used on Server-side (and JavaScript on Client Side) to generate dynamic web pages over HTTP, however you will be surprised to know that you can execute a PHP in a Linux Terminal without the need of a web browser.

This article aims at throwing light on the command-line aspect of PHP scripting Language.

1. After PHP and Apache2 installation, we need to install PHP command Line Interpreter.

# apt-get install php5-cli [Debian and alike System) # yum install php-cli [CentOS and alike System)

Next thing, we do is to test a php (if installed correctly or not) commonly as by creating a file infophp.php at location ‘/var/www/html‘ (Apache2 working directory in most of the distros), with the content , simply by running the below command.

# echo '' > /var/www/html/infophp.php

and then point your browser to http://127.0.0.1/infophp.php which opens this file in web browser.

Check PHP Info

Same results can be obtained from the Linux terminal without the need of any browser. Run the PHP file located at ‘/var/www/html/infophp.php‘ in Linux Command Line as:

# php -f /var/www/html/infophp.php

Check PHP info from Commandline

Since the output is too big we can pipeline the above output with ‘less‘ command to get one screen output at a time, simply as:

# php -f /var/www/html/infophp.php | less

Check All PHP Info

Here Option ‘-f‘ parse and execute the file that follows the command.

2. We can use phpinfo() which is a very valuable debugging tool directly on the Linux command-line without the need of calling it from a file, simply as:

PHP Debugging Tool

Here the option ‘-r‘ run the PHP Code in the Linux Terminal directly without tags < and >.

3. Run PHP in Interactive mode and do some mathematics. Here option ‘-a‘ is for running PHP in Interactive Mode.

# php -a Interactive shell php > echo 2+3; 5 php > echo 9-6; 3 php > echo 5*4; 20 php > echo 12/3; 4 php > echo 12/5; 2.4 php > echo 2+3-1; 4 php > echo 2+3-1*3; 2 php > exit

Press ‘exit‘ or ‘ctrl+c‘ to close PHP interactive mode.

Читайте также:  Понимает ли linux ntfs

Enable PHP Interactive Mode

4. You can run a PHP script simply as, if it is a shell script. First Create a PHP sample script in your current working directory.

# echo -e '#!/usr/bin/php\n' > phpscript.php

Notice we used #!/usr/bin/php in the first line of this PHP script as we use to do in shell script (/bin/bash). The first line #!/usr/bin/php tells the Linux Command-Line to parse this script file to PHP Interpreter.

Second make it executable as:

5. You will be surprised to know you can create simple functions all by yourself using the interactive shell. Here is the step-by step instruction.

Start PHP interactive mode.

Create a function and name it addition. Also declare two variables $a and $b.

php > function addition ($a, $b)

Use curly braces to define rules in between them for this function.

Define Rule(s). Here the rule say to add the two variables.

All rules defined. Enclose rules by closing curly braces.

Test function and add digits 4 and 3 simply as :

php > var_dump (addition(4,3));
Sample Output

You may run the below code to execute the function, as many times as you want with different values. Replace a and b with values of yours.

php > var_dump (addition(a,b));
php > var_dump (addition(9,3.3));
Sample Output

Create PHP Functions

You may run this function till you quit interactive mode (Ctrl+z). Also you would have noticed that in the above output the data type returned is NULL. This can be fixed by asking php interactive shell to return in place of echo.

Simply replace the ‘echo‘ statement in the above function with ‘return

and rest of the things and principles remain same.

Here is an Example, which returns appropriate data-type in the output.

PHP Functions

Always Remember, user defined functions are not saved in history from shell session to shell session, hence once you exit the interactive shell, it is lost.

Hope you liked this session. Keep Connected for more such posts. Stay Tuned and Healthy. Provide us with your valuable feedback in the comments. Like ans share us and help us get spread.

Источник

Оцените статью
Adblock
detector