New Website

I've made a new website, as lynx.io is dead. You can find it here: http://macr.ae/—it's similar in format to lynx.io, but has better articles, and they're all written by me.

JavaScript disabled

While it will still mostly work, a lot of this site's functionality relies on JavaScript - please enable it for the best experience.

Running PHP applications from the command line

I've always found it useful to be able to run PHP applications from the command line: not only does it allow me to quickly view the output of my code from the command line - where I do the majority of my developing - but it also allows me to make proper command line applications from within PHP. Okay, quite a lot of stuff is not possible, but it's still fun. This applies to Unix only, I believe.

The first way (the old way):

Run this from bash:

php index.php

That's not an application, as it is just telling php to run index.php.

To be able to run it simply by typing the file name, follow these steps:

1. Run whereis php in a terminal and copy what it prints.

2. Add the following code to the top of your php file:

#! /usr/bin/php

/usr/bin/php is what whereis php gave me, replace it with what it gave you. Basically, this is telling the command line the location to PHP, and where to run the application. It is called a hashbang due to the #!.

3. Make the application an application by running the following code in the command line (where index.php is your application):

chmod +x index.php

You can then run your application by running the following:

./index.php

It's that simple!

About Callum Macrae:

Callum Macrae is the founder of lynx.io and a JavaScript developer from the United Kingdom. He is currently writing his first book, to be published by O'Reilly Media.

You can view more articles by this author here.

Tags: php cli

Comments

No comments; leave one below.

says:

Add comment

 

You can use markdown in comments (press "m" for a cheatsheet).

Enable JavaScript to post a comment

Markdown Cheat Sheet

# This is an <h1> tag
## This an <h2> tag
###### This is an <h6> tag

Inline markup: _this text is italic_, **this is bold**, and `code()`.

[Link text](link URL "Optional title")
[Google](http://google.com/ "Google!")

![Alt text](image URL)

![This is a fish](images/fish.jpg)

1. Ordered list item 1
2. Ordered list item 2

* Unordered list item 1
* Unordered list item 2
* Item 2a
* Item 2b

And some code:

// Code is indented by one tab
echo 'Hello world!';

Horizontal rules are done using four or more hyphens:

----

> This is a blockquote

This is an <h1> tag

This an <h2> tag

This is an <h6> tag

Inline markup: this text is italic, this is bold, and code().

Link text Google

This is a fish

  1. Ordered list item 1
  2. Ordered list item 2
  • Unordered list item 1
  • Unordered list item 2
    • Item 2a
    • Item 2b

And some code:

// Code is indented by one tab
echo 'Hello world!';

Horizontal rules are done using four or more hyphens:


This is a blockquote

Toggle MarkDown / HTML (t), full reference or close this