WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : PHP CLI and Cron
Categories : PHP, CLI, Cron
devarticles
devarticles
Date : 2004-05-02
Grade : 0 of 5 (graded 0 times)
Viewed : 3746
Search : More Articles by devarticles
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


In this article, Jason will explain what CLI and Cron are and how to use them together within the PHP environment.

What is PHP CLI?

CLI stands for command line interpreter. What does this do for you? This allows you to run your PHP scripts at command line similar to Perl or any other scripting language used in *nix command line environment.

Why PHP CLI?

There a several advantages why one would want the ability to run PHP at command line

No need to learn another scripting language such as Perl, Bash or Awk

Ability to reuse existing components. For example say you've written a set of database access objects or functions you now can re-use them instead of re-writing them in Perl, Java or some other language.

Cross-platform, you can easily run PHP on *nix environment or windows environment or in fact any OS with PHP CLI installed

Making GUI applications, this is out of scope for this article visit http://gtk.php.net to learn more about PHP and desktop applications.

One may ask why do I need to run PHP at command line in the first place? I'm a web developer. Lots of times when making and running web applications with database functionality you will probably find yourself in position where you need to regularly clean up the database tables.

Another example maybe you need to send out regular web reports generated by your application to the appropriate administrators. Whatever the case may be, rather then trying to remember to run these tedious tasks on a regular basis isn't it better to have a batch process where these tasks are automatically done at specified times. This is where you would use Cron and now that you know the advantages of using PHP at command line these two make a great pair.

There really isn’t much too it, it is just a matter of using the 2 different technologies together. The following sections will introduce you to both PHP CLI and Cron. After it will be pretty clear how to use them together, but incase you still don’t get it there is section demrating the use of PHP and Cron.

How to Install PHP CLI

Pre-Installed / Package Manager

If you are using Linux distribution with PHP already pre-installed or if you are using a package manager such as RPM you may have to do some fishing around to find the PHP executable. Mostly likely its under /usr/bin, as friendly reminder you might want to run the find command to find the PHP executable. Navigate you way to the root of your file system and type in the following find . -name php -print 2>&1 /dev/null.

Install from Using tar/source

For those of you who like to install from source, it is almost the same as installing PHP with apache except without APXS. To install PHP CLI you need to install it purely as CGI and with the --enable-cli option included. For those of you who still want to use PHP for the web you will have to install a separate installation of PHP.

Everything should work fine because with the APXS Apache when compiled with the right options will know where to find the PHP APXS version and for the other version of PHP all you have to do is set the correct PATH to access the PHP CLI version at command line. Here is step-by-step install of PHP CLI

Download the latest stable release of PHP


$ tar- xvzf php-version.tar.gz
$ cd php-version/
$ ./configure --prefix=/usr/local/php
--enable-cli
--bindir=/usr/bin


…. any other options you normally put, for example --with-mysql if you are going to use mySQL database with PHP


$ make
$ make install




Note --bindir indicates where you want the PHP binary to be installed

Hello World

Create a test file called phpcli.php. This file will contain a simply your standard hello world


#!/usr/bin/php -q
<?php
echo "Hello World with PHP CLI";
?>


Change the permission on the file to be executable, chmod 755 phpcli.php or chmod +x phpcli.php

Now run it, assuming you are in the same directory and the Path is indeed set for /usr/bin.


$ ./phpcli.php
Hello World with PHP CLI
$


You will notice the difference from web php script there is the line #!/usr/bin/php, this indicates that the PHP executable is used to parse the file.
Reading in Command Line Arguments
Like most scripting languages you are able to pass in command line arguments. Create a file named testargs.php


#!/usr/bin/php -q
<?php
echo "Test Read Arguements:\n\n";
echo
$_SERVER["argc"];
echo
"\n";
echo
$_SERVER["argv"][0];
?>


$_SERVER["argc"] will give the number arguments enter including. Note the script itself is an argument.

$_SERVER["argv"] is an array of arguments. To access the first argument it will be at index 1, $_SERVER["argv"][1]. Remember the file name itself is at index 0, $_SERVER[argv"[0].

Notice as well now that we aren't making a web script we need to apply CLI specific code in this case to indicate next line we type in the escape character followed by n. Instead of <br> which is normally used for html.

More Hello World

Another way to run php at command line is directly right at the command line, for example


$ php -r "echo 'Hello World'";
Hello World
$



Getting more Help with PHP CLI

Type in the php executable with the switch -h, php -h. It should output something similar to the following:

Usage: php [options] [-f] <file> [args...]
php [options] -r <code> [args...]
php [options] [-- args...]

-s Display colour syntax highlighted source.
-w Display source with stripped comments and whitespace.
-f <file> Parse <file>.
-v Version number
-c <path>|<file> Look for php.ini file in this directory
-a Run interactively
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-z <file> Load Zend extension <file>.
-l Syntax check only (lint)
-m Show compiled in modules
-i PHP information
-r <code> Run PHP <code> without using script tags <?..?>
-h This help
args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin

Read More...









Generating One-Time URLs with PHP
Categories : PHP, URLs
10 PHP Functions I Bet You Didn't Know About!
Categories : PHP, PHP Functions, Filesystem, Arrays, Errors and Logging
Data, its presentation and user interface forms
Categories : PHP, XML, User Interface
Using the .NET Assembly in PHP
Categories : PHP, .NET
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
Web Development With PHP FastTemplate
Categories : PHP, Templates, FDF
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Building XML Web Services with PHP NuSOAP
Categories : PHP, NuSOAP, XML
Building a Counter
Categories : PHP, Cookies
Building A Persistent Shopping Cart With PHP and MySQL
Categories : PHP, MySQL, Databases, Ecommerce
Who's Linking?
Categories : PHP, Beginner Guides, To PHP
Sockets and PHP
Categories : PHP, Sockets
Creating Auto-incrementing ID Fields with PHP and Oracle
Categories : PHP, PHP options/info, Databases, Oracle
File And Directory Manipulation In PHP (part 1)
Categories : PHP, Directories, Filesystem