Skip to main content

PHP FROM COMMAND LINE

Hello guys, today we will learn how to write PHP programs through command line. Generally, PHP is use for web applications but  there are occasions where we can use command line such as automatic mail sending, log tracking, cron jobs etc.
PHP supports CLI SAPI(Command Line Interface Server API) for executing the script from command line. This SAPI will differ from other interfaces based on the IO practices, configuration defaults, buffering and more.
For example,
  • IO practices – it will not support PHP request method (GET, POST). Instead, it uses IO streams.
  • Command line execution time –  In the command line, the execution time limit is unlimited by default.
  • Buffering – implicit_flush is enabled to display output. It also supports output buffering.
Some of the PHP CLI SAPI commands:

CommandDescription
-iTo display command line PHP info.
-vTo show version details.
-rTo execute PHP script without delimiters
-fTo execute PHP file.
-lTo check the syntax of the given file.
-wTo strip comments, white spaces from given file and print.
-aTo run in the interactive shell environment.

Note : Try with -h  to know more about them.
PHP command line examples:
Now, let us see some simple examples to interact PHP from the command line.These examples deal with PHP where the file name or code is given.
Executing PHP file:
Program to print array of strings:
1
2
3 
4
<?php
$fruits = array('apple','mango','grapes');
print_r($fruits);
?>
save this file as index.php. Now you can execute the program from command line by going to the file path and specifying  php index.php.
This command will execute the index.php file and print the output as below
Executing PHP code:
You can directly execute PHP code in command line. In this example, PHP exho statement is executed from command line.
Hope you understand the tutorial.If you have any doubts please do comment .Please share this post with your friends .Thank you.Have a good day :)

Comments

Popular posts from this blog

Reading and Generating QR codes in Python using QRtools

What are QR codes? A Quick Response (QR) code is a 2 dimensional barcode that is used due to its fast readability and relatively large storage capacity.  2 dimensional barcodes are similar to one dimensional barcodes, but can store more information per unit area. Installation and Dependencies Linux:   qrtools can be installed on debian based linux systems with the following commands $sudo apt-get update $sudo apt-get install python-qrtools The following dependencies must be installed as well [sudo] pip install pypng [sudo] pip install zbar [sudo] pip install pillow Windows:   qrtools can be installed on windows by downloading the file from here(https://pypi.python.org/pypi/qrtools/0.0.1). On downloading and extraction, run the following command from inside the folder python setup.py install Generating a qrCode: qrtools contains a class QR (can be viewed in the source code), for which we must initially create an object. The object takes the ...

MYSQL CONNECTION USING PDO WITH PHP

Hello guys, In the previous tutorial we have seen how to connect to MySQL database with PHP using MySQLi. Today we will learn how to connect with PDO. As a PHP programmer we have learned how to access database by using either MySQL and MySQLi. As of PHP 5.1, there's a better way. PHP Data Objects(PDO) provide methods for prepared statements and working with objects that will make you far more productive! PDO Introduction :        PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. Database Support:       The extension can support any database that a PDO driver has been written for. The following drivers currently implement the PDO interface: PDO_CUBRID Cubrid PDO_DBLIB FreeTDS / Microsoft SQL Server / Sybase PDO_FIREBIRD Firebird PDO_I...