Find out how to learn PHP on Mac in our complete guide to mastering the scripting language and adding new features to your website.

Learning PHP is a good idea. PHP (a recursive acronym for PHP Hypertext Preprocessor) is a scripting language. It's the power behind some of the biggest websites on earth, including Facebook, Wikipedia, and Wordpress.

Learning PHP puts you in good stead as a programmer. You'll come across PHP all the time when working on websites, and it's a great way to add programming oomph to your HTML and CSS-based website. PHP enables you to script virtually anything.

PHP is the quintessential marmite of programming languages. Many professionals rate it quite badly, partly because it's so easy for beginners to use (which leads to a lot of mistakes); also it carries a lot of legacy code, which makes it clunky compared to Ruby or Javascript.

Don't let the naysayers distract you. If you want to create code online, then you must spend time learning how PHP works.

See also

Learn to code: How to set up and learn Python coding on a Mac

How to write apps with Apple Swift 2

Best Mac for app development

How to make an iOS game

How to set up and learn PHP in OS X: what is PHP?

Install PHP in OS X

PHP is a general-purpose scripting language. While HTML displays text and CSS styles it up, PHP is a programming language that can automate tasks. It's often used to get information from the user, or make API (Application Programming Interface) calls to other websites and online services. You can control program flow and logic gates in PHP, and use it as a programming language.

PHP is slightly different from Javascript (a similar language) in that the code runs on your server, and then HTML is sent to the browser. So the browser gets the results, but can't see the code. Running code on the server side makes PHP good for security, as the browser doesn't get to see the code. It also means PHP isn't dependent on browser speed or setup.

How to learn PHP: Setting up PHP in OS X

Mac OS X comes with PHP prepackaged. So there isn't much you need to do to install PHP. Open a Terminal window and enter php --version to check which version you have installed. If you're running OS X El Capitan, you will see PHP 5.5.31 (or later). Ours says "PHP 5.5.31 (cli) (built: Feb 20 2016 20:33:10)".

The latest stable release is PHP 7.0, which you can install via Homebrew using brew install php70. If you don't have Homebrew, then you can install it using the script at the Homebrew website.

However, we'd advise you to stick with PHP 5.5 in the short term. While PHP 7.0 adds a lot of cool new features, most tutorials use PHP 5.5, and it's still the version commonly used in most programming environments.

How to learn PHP: Hello World

PHP interactive mode

Like Python, you can run PHP in an interactive shell from inside Terminal. Open Terminal and enter php -a to start interactive mode. You should see "Interactive shell" and "php >". Enter echo "Hello World!"; to create your first line of code.

Not the ";" at the end of the line. This indicates that you've finished entering a line of code. If you don't use a ";" at the end, it'll move to the next "php >" line so you can add more code. (You can enter ";" and press Return to run the line of code).

Type exit to leave interactive to mode.

How to learn PHP: Creating an interactive script in PHP

Create PHP script

Now that you've used the PHP interactive shell to enter commands, you should create a PHP script in a text file. Open your text editor and enter the following code:

<?php

if ($argc !== 2) {

   echo "Usage: php hello.php [name].\n";

   exit(1);

}

$name = $argv[1];

echo "Hello, $name\n";

Save the file as hello.php in your Home folder and enter php hello.php to run it. You should see "Usage: php hello.php [name]." This is because the program requires an argument, "your name" after it. Enter php hello.php John Appleseed and it'll display "Hello John Appleseed" at the command line.

How to learn PHP: PHP learning resources

Now that you've set up PHP and started to learn how to experiment with the interactive shell and files, you'll need to learn PHP properly. Here are some valuable PHP training resources: