PHP Tip #1 – PHP via the Command Line


This is the first of many (hopefully) tips and tricks in PHP that I've learned over the years. Some/many/all may seem trivial to you (depending on your level of PHP knowledge), but I wanted a way to keep track of things that I have found interesting that I want to remember. There will be no particular order to these articles, they will simply be numbered by the order they are written in.

It seems fitting to begin with a trick that can be used to try out other tricks, so I decided to start off this “journal”, by describing a simple (but many times useful) trick that allows you to run PHP scripts via your command line.

Coding in the Command Line

Sometimes I wonder what PHP does in certain situations. For example, what does PHP do when you try casting an empty variable to an integer? Well, I could've searched the interwebs to find the answer; but I really just wanted to try it and see what happened. In order to do this without having to put it into an existing PHP file that I would have to render in my browser (or create a new PHP file to render), I searched for a way to run a few lines of PHP directly in my console. Lo and behold, I found this article that explained exactly how to do what I was after. So let's see how it's done and figure out what PHP does with empty variables casted as integers...

First, open up your terminal (I have a Mac, so I simply used in the built-in Terminal application). Second, make sure you have PHP installed (just in case that wasn't quite obvious). Third, run php -r '. You should see something like the following:

missing image :/

Cool! Now you can start writing PHP code. When you're done, simply type in a closing single quote (') and hit enter to see your PHP run.

missing image :/
Note the newline "\n" to make things more readable in our console.

So let's see what PHP does when casting empty variables to integers. Type (or copy and paste) the following into your console:

php -r ' $emptyString = ""; echo "emptyString: " . (int) $emptyString . "\n"; unset($emptyString); echo "unset emptyString: " . (int) $emptyString . "\n"; '

The output should be:

emptyString: 0 unset emptyString: 0

Sweet! Now you know how to evaluate PHP code in your terminal and that PHP converts empty and unset variables to 0 when type casted to integers.

Run a PHP File via the Command Line

I think it's super cool that you can type PHP code into your terminal and see what happens, but it's also cool that you can evaluate a PHP file via the terminal. Let's take the same example used above and place it into a php file:

// emptyToInt.php <?php $emptyString = ''; echo "emptyString: " . (int) $emptyString . "\n"; unset($emptyString); echo "unset emptyString: " . (int) $emptyString . "\n";

Now, run that file and see the magic php -f [filename]

missing image :/

Voila! Now you know how to run a php file without using your browser.


  1. Make sure you have PHP installed
  2. Open your terminal
  3. Run a script directly in the command line by typing php -r ' [enter], followed by your script, followed by ' [enter]
  4. Execute a PHP file in the terminal by typing php -f [filename] [enter]

Hopefully this was helpful (or at least interesting)—let me know if you thought this was useful or if there is anything that I missed or that is incorrect!

Written 20 Aug 2013 by Caleb