Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

How to set up and learn PHP in OS X

Lucy Hattersley | July 6, 2016
Create great web pages using PHP on an Apple Mac

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:

Source: Macworld 

 

Previous Page  1  2 

Sign up for CIO Asia eNewsletters.