Running php file in linux

Running PHP code/scripts on the command line

I just began learning PHP. I’ve installed php5 on Linux and wrote very simple code just to get going. How can I run scripts? I tried using the -f option, but it works as a cat command and just spits out the code to standard output. The interactive interpreter option works fine. Is a web browser the only way to execute a PHP script?

If you are here (from a search engine hit) looking for a way to run PHP source directly from the command line (like a Perl one-liner), then go to Stack Overflow question How can I execute PHP code from the command line?. (It is the -r option)

4 Answers 4

If it is acting like cat, then you probably forgot to switch out of template mode and into script mode with

Shorter way for command line:

php -r ‘echo «Hello «; echo «Jay»;’
OR
php -r ‘echo dirname(«parent/child/reply») . «\n»;’

As already mentioned, you can execute your PHP with the following.

If you wish to pass an argument(s), you can simply do so like this:

In your PHP file you can use this argument by accessing the $argv array like this:

The above would print our «I like Apples». Note the array index is 1 and not 0. 0 is used for script name. In this case $argv would be «myScript.php»

Actually, PHP’s main purpose is to generate web pages, but there are at least two other options:

The first one can be achieved in many ways (eg. by giving proper permissions to the file and calling script by providing its URI, eg. ./index.php ), the second one can be invoked by php -a command (as stated in the documentation mentioned above).

Источник

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.

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.

Читайте также:  Linux kernel pci e

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.

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.

Читайте также:  Linux macbook pro 2019

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.

Источник

How to Run PHP Code from Linux Command Line: Tips and Tricks

Learn how to execute PHP code from the Linux command line, including running scripts as shell scripts, executing commands via shell, and running scripts at regular intervals.

  • Running PHP code on the command line
  • Running PHP scripts as shell scripts
  • How to Execute PHP Codes in Linux through Command Line
  • Executing commands via shell
  • Running PHP scripts at regular intervals
  • Other code examples for running PHP files on Linux shell
  • Conclusion
  • How to run PHP file in shell script?
  • How to run a PHP file in Linux terminal?
  • Can PHP run shell command?
  • How to run a PHP file?

PHP is an open-source server-side scripting language that is widely used for web development. PHP scripts can be executed from a web server, but did you know that they can also be executed from the Linux command line? In this post, we will discuss how to execute PHP code from the Linux command line, including running scripts as shell scripts, executing commands via shell, and running scripts at regular intervals.

Running PHP code on the command line

PHP code can be executed directly from the command line by using the php command followed by the file name. For example, to execute a PHP script named script.php , you can use the following command:

To run PHP code directly in the command line, use the -r flag followed by the PHP code in single-quotes. For example, to execute the PHP code echo «Hello World»; , you can use the following command:

Читайте также:  Открыть вторую консоль linux

You can also use php -a to enter interactive mode and execute PHP code line by line. This is useful for testing and debugging small pieces of code.

Running PHP scripts as shell scripts

To run a PHP script as a shell script, add #!/usr/bin/php to the top of the script and make it executable using chmod +x filename.php . For example, to create a PHP script that outputs “Hello World”, you can use the following code:

Save this code in a file named hello.php and make it executable using chmod +x hello.php . To execute the script like a shell script, use /usr/bin/php hello.php .

It is important to be careful with elevating privileges to the PHP script. Avoid running the script with root permissions unless it is absolutely necessary.

How to Execute PHP Codes in Linux through Command Line

This video explains how to Execute PHP Codes through Command Line. PHP is designed for Duration: 6:04

Executing commands via shell

You can use the shell_exec function to execute commands via shell and return output as a string. For example, to execute the ls command and return the output, you can use the following code:

$output = shell_exec('ls'); echo $output; 

You can also use exec() to execute system-related commands or programs from the PHP script. For example, to execute a Python script named script.py , you can use the following code:

exec('python script.py', $output); print_r($output); 

It is important to use escapeshellarg to escape shell metacharacters when passing arguments to shell commands. This helps to prevent security issues.

Running PHP scripts at regular intervals

To run a PHP script at regular intervals, you can use a shell script with a loop and the command php /path/to/script.php . For example, to run a PHP script named script.php every 5 seconds, you can use the following code:

#!/bin/bash while true; do php /path/to/script.php sleep 5 done 

Use wait with no arguments to wait for all background processes to complete. The PHP script can be called from a shell script to run at regular intervals.

Other code examples for running PHP files on Linux shell

In Php , for example, run shell script from php file code sample

echo shell_exec('sh /home/scripts/fix-perm.sh'); 

In Php , for example, how to execute php in linux code example

Conclusion

Running PHP code from the Linux command line provides a convenient way to execute scripts and automate tasks. By using the php command, Running PHP scripts as shell scripts, executing commands via shell, and running scripts at regular intervals can all be achieved. However, it is important to be careful with elevating privileges to the PHP script and always escape shell metacharacters to avoid security issues. With these tips and tricks, you can take your PHP development skills to the next level.

Источник

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