Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

Getting PHP to Run on Your Computer 653

24


Statements in PHP, as in JavaScript, are terminated with a semicolon. (You can see the
semicolon at the end of the statement in the example.) There’s no reason why you can’t
include multiple statements within one PHP tag, like this:


<?php
echo "Hello ";
echo "world!";
?>


PHP also provides a shortcut if all you want to do is print the value of something to a
page. Instead of using the full PHP tag, you can use the expression tag, which just echoes
a value to the page. Instead of using


<?php echo "Hello world!"; ?>


You can use th is:


<?= "Hello world!" ?>


Replacing php with = enables you to leave out the call to the echo function and the semi-
colon. This style of tag is referred to as a short tag. Not all PHP installations have short
tags enabled, so be sure to test these on your server before you release your PHP pages.


Getting PHP to Run on Your Computer


Before you can start writing your own PHP scripts, you need to set up a PHP environ-
ment. The easiest approach is probably to sign up for a web hosting account that provides
PHP support. Even if you do so, though, there are some advantages to getting PHP to
work on your own computer. You can edit files with your favorite editor and then test
them right on your own computer rather than uploading them to see how they work.
You’ll also be able to work on them even if you’re not online. Finally, you can keep from
putting files on a server that your users will be able to see without your having tested
them first.


To process PHP pages, you need the PHP interpreter and a web server that works with
the PHP interpreter. The good news is that PHP and the most popular web server,
Apache, are both free, open source software. The bad news is that getting PHP up and
running can be a bit of a technical challenge.


Fortunately, if you’re a Windows or Mac user, someone else has done this hard work
for you. A tool called XAMPP, available for both Windows and OS X, bundles up ver-
sions of Apache, PHP, and MySQL (a database useful for storing data associated with
web applications) that are already set up to work together. (The last P is for Perl, another
scripting language.) You can download it from https://www.apachefriends.org/
index.html.

Free download pdf