How to Read Php File in Linux Command

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

Run PHP Codes in Linux Command Line
Run PHP Codes in Linux Command Line – Part 1

A PHP Syntax is very similar to Syntax in C, Coffee and Perl Programming Language with a few PHP-specific feature. PHP is used by some 260 Million websites, as of now. The electric 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, still yous will be surprised to know that you lot can execute a PHP in a Linux Concluding without the need of a web browser.

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

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

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

Side by side thing, we do is to test a php (if installed correctly or not) commonly as by creating a file infophp.php at location '/var/world wide web/html' (Apache2 working directory in most of the distros), with the content <?php phpinfo(); ?>, simply by running the below control.

# echo '<?php phpinfo(); ?>' > /var/www/html/infophp.php        

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

Check PHP Info
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 Control Line as:

# php -f /var/www/html/infophp.php        
Check PHP info from Commandline
Check PHP info from Commandline

Since the output is too large we tin pipeline the to a higher place output with 'less' command to become 1 screen output at a time, only as:

# php -f /var/www/html/infophp.php | less        
Check All PHP Info
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 direct on the Linux command-line without the demand of calling it from a file, merely equally:

# php -r 'phpinfo();'        
PHP Debugging Tool
PHP Debugging Tool

Here the choice '-r' run the PHP Lawmaking in the Linux Terminal directly without tags < and >.

three. Run PHP in Interactive mode and exercise some mathematics. Hither pick '-a' is for running PHP in Interactive Manner.

# php -a  Interactive beat out          php >          repeat 2+3; 5          php >          echo nine-half-dozen; iii          php >          echo 5*4; twenty          php >          repeat 12/three; 4          php >          echo 12/5; 2.iv          php >          echo 2+iii-i; 4          php >          echo 2+3-1*iii; ii          php >          leave        

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

Enable PHP Interactive Mode
Enable PHP Interactive Style

4. Yous can run a PHP script simply as, if it is a trounce script. Get-go Create a PHP sample script in your current working directory.

# echo -eastward '#!/usr/bin/php\n<?php phpinfo(); ?>' > phpscript.php        

Detect we used #!/usr/bin/php in the first line of this PHP script as nosotros utilise to do in beat out script (/bin/fustigate). The showtime line #!/usr/bin/php tells the Linux Command-Line to parse this script file to PHP Interpreter.

2nd get in executable equally:

# chmod 755 phpscript.php        

and run it as,

# ./phpscript.php        

5. You volition be surprised to know you can create simple functions all by yourself using the interactive vanquish. Here is the step-by step educational activity.

Kickoff PHP interactive manner.

# php -a        

Create a function and name it improver. Likewise declare two variables $a and $b.

          php >          function add-on ($a, $b)        

Utilise curly braces to ascertain rules in between them for this part.

          php >          {        

Define Rule(s). Here the dominion say to add together the ii variables.

php { echo $a + $b;        

All rules defined. Enclose rules by closing curly braces.

php {}        

Exam office and add together digits 4 and iii simply as :

          php >          var_dump (addition(4,iii));        
Sample Output
7NULL        

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

          php >          var_dump (addition(a,b));        
          php >          var_dump (add-on(9,three.iii));        
Sample Output
12.3NULL        
Create PHP Functions
Create PHP Functions

Yous may run this office till y'all quit interactive mode (Ctrl+z). As well you would have noticed that in the above output the data blazon returned is NULL. This tin can be fixed by request php interactive shell to render in place of echo.

Simply supplant the 'repeat' statement in the to a higher place function with 'return'

Supervene upon

php { echo $a + $b;        

with

php { render $a + $b;        

and residual of the things and principles remain same.

Hither is an Instance, which returns appropriate data-type in the output.

PHP Functions
PHP Functions

Always Remember, user defined functions are not saved in history from crush session to shell session, hence in one case you go out the interactive shell, information technology 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. Similar ans share us and help us become spread.

Read Likewise: 12 Useful PHP Commandline Usage on Linux Terminal – Part 2

If You Appreciate What We Practice Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying u.s. a java ( or 2 ) equally a token of appreciation.

Support Us

We are thankful for your never ending support.

wilsongayses.blogspot.com

Source: https://www.tecmint.com/run-php-codes-from-linux-commandline/

0 Response to "How to Read Php File in Linux Command"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel