DEV Community

loading...
Cover image for Getting started with PHP

Getting started with PHP

Noel A Rodriguez
Play Minecraft, code, eat, sleep, repeat. Indie game dev. Storyteller.
・3 min read

Hi there! When you start with web development, at some point, you realize that HTML and CSS are not enough to achieve your project goals.

There are a lot of technologies to build dynamic websites and PHP is one of the easiest ones and perfect if you want to build simple backends. Besides, it is a good option for beginners' self-taught web developers. It is really easy, and there is a lot of documentation.

This is the fist of a series of posts in which we will learn the basics of programming logic using PHP.

What is PHP?

Paraphrasing the omniscient Wikipedia, PHP is a programming language specially created for web development, and it is usually processed on a web server by a PHP interpreter implemented.

In simple words, it is a programing language to write server side web applications.

What do I need?

  • A computer, obviously, with Windows, Linux or Mac.
  • You need to install either WAMP, LAMP, MAMP or XAMPP
  • Basic shell commands
  • Your favorite text editor

Hello, World!

Once you install the Apache server and PHP we can start coding. Open the command line, go to your server root (Usually C:\wamp\www\ on Windows, /var/www/ on Linux, and /Applications/MAMP/htdocs/ on Mac) to create a new folder and our first PHP file.

mkdir my-first-php-script

cd my-first-php-script

touch helloworld.php
// On windows
type nul > helloworld.php
Enter fullscreen mode Exit fullscreen mode

Open the file you just created and add the following lines

<?php
echo 'Hello, World!';
?>
Enter fullscreen mode Exit fullscreen mode

Now, if you are using WAMP or LAMP, go to http://localhost/my-first-php-script/helloworld.php or http://127.0.0.1/my-first-php-script/helloworld.php. If you are using MAMP, go to http://localhost:8888/my-first-php-script/helloworld.php And you will see the message "Hello, World!" in your browser.

What happened here? I think you noticed the delimiters <?php ?>. They are required to insert the PHP code. Consider that you can mix PHP with HTML so these delimiters tell the server interpreter that this is PHP code. Let's see.

touch index.php
Enter fullscreen mode Exit fullscreen mode

Open the file and add the following

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>PHP Hello World</title>
</head>
<body>
  <?php
    echo 'Hello, World!';
  ?>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

This will be rendered as a simple HTML page but we are sending the "Hello, World!" from PHP, using the delimiters.

Let's continue. The command echo is a language constructor that allows us, in this case, to print a string in the browser. Notice that we are using a semicolon at the end of the line. This is really important in PHP. Each operation line must be ended by a semicolon, there are exceptions that we will see then.

To ease coding, we can abbreviate the echo command this way.

<?= 'PHP Hello World' ?>
Enter fullscreen mode Exit fullscreen mode

Let's see it working.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- Abreviated `echo` command` -->
  <title><?= 'PHP Hello World' ?></title>
</head>
<body>
  <?php
    echo 'Hello, World!';
  ?>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Now you can check in your browser and see how the title stays the same. Notice that I didn't add a semicolon at the end of the line. You can add it, but it is not required in this case.

Comments

PHP supports the same kind of comments that use languages like C, C++, and Shell. Let's see

touch comments.php
Enter fullscreen mode Exit fullscreen mode

Open the file and add the following

<?php
echo 'This is an Example' // One line comment.
/* 
  Multiline comment
*/
# Like shell comment.
?>
Enter fullscreen mode Exit fullscreen mode

We have to be careful with multiline comments. A simple error can crash your whole script.

<?php
echo 'This is OK'
/* 
  The multiline comments have delimiters.
  If you write something after closing, PHP will
  try to execute it as code and as obviously it 
  won't make sense, the script will crash
*/ <- Close delimiter.

echo 'This is not right'
?>
Enter fullscreen mode Exit fullscreen mode

This is all for now. Next time we will learn about variables. I hope y you have found this useful. Please like and share and if you think something is missing, please let me know in the comments below.

Discussion (3)

Collapse
rivj_ux profile image
Juan Rivera

Saving this as well. Currently learning PHP :)

Collapse
alexsc6955 profile image
Noel A Rodriguez Author

Cool. Any specific thing you need, please let me know.

Collapse
jonesey712 profile image
Jonesey712

Saving this for later! I have to learn PHP for work so this will be a good read for when I start!