Getting Started with PHP
Updated on by Jeff Caldwell,
PHP is a server-side, or backend language. It is embedded directly into HTML in
Creating PHP Scripts
A basic PHP script begins and ends with script delimiters that begin with
<?php and end with
<?php echo "Hello, world!"; ?>
?> can be left off if the script contained in the delimiters is the only script on the page. In fact, some code formatters may strip it out for you.
Also, see the semicolon at the end of the
echo statement up there? That’s required.
Like HTML, if you’re in a directory, the file
index.php will automatically be picked up by the server as the correct file to serve at that directory’s URL. So, if you’re at the root of your web server,
index.php will be what’s served to any client who visits the site’s home page.
Speaking of servers:
You can start a development server with PHP in the terminal. Navigate to the directory you want to serve and type:
php -S localhost:<portnumber>. So if you enter:
php -S localhost:8080
you’ll be able to see your work at
Variables and Constants
Variable identifiers must start with
$ and are case sensitive. An identifier can contain uppercase and lowercase letters, numbers, or underscores. The first character after the
$ must be a letter. Identifiers can’t contain spaces.
let myNumber; // do some stuff myNumber = 123;
That’s not possible in PHP:
$myNumber = 123;