PHP Basic Syntax

Where to Begin? Lets Start With PHP Basic Syntax.

PHP is leaps and bounds above where it started. It’s a modern programming language that has Object Oriented capabilities. It’s the backbone of popular frameworks like Laravel. It’s great for the web and it makes writing web-based applications enjoyable. It can be used to produce both full-stack and back-end applications. With back-end applications, you have a choice for your front-end library or framework. With the Laravel backend framework, front end libraries like React or Vue tie in nicely.

I still see countless arguments against PHP, so I finally had enough. I’m going to show you how elegant this language can actually be. If you’re a beginner to programming in general, follow along and not only will you learn PHP, you’ll learn Computer Science concepts along the way.

If you are going to be following along, it’s probably beneficial for you to have a local development set up on your computer. PHP runs on the server, so you’ll need to make your computer think it’s the server. You can do this easily by downloading one of the following virtual servers and installing it onto your computer.

  • Windows: WAMP Server
  • Mac: MAMP Server
  • Linux: LAMP Server

I’m sure that you already guessed what the W, M, and L stand for: Windows, Mac, Linux. The AMP stands for Apache, MySQL, PHP, where Apache is the server software, MySQL is the database management system, and PHP is the programming language that we’re discussing. Just google your favorite stack and download/install them onto your machine. You can check out the video at the bottom of this article for a how-to-guide.

You’ll also need some sort of IDE like JetBrains PHP Storm (paid) or Visual Studio Code (Free). I use both. There is nothing that you’ll read over the next 100ish articles that you will not be able to do with Visual Studio Code.

Create a new file in your IDE and name it index.php. In order for your scripts to run, they need to end with the .php extension. Why index? That’s the default name that the Apache server will look for when you visit your site.

Your PHP code needs to be surrounded by PHP tags:

 

<?php 
Your Code Here;
?>

 

The first piece of code that we’ll write is a simple echo statement. Echo just means, take this string and output it to the screen. Each statement needs to end with a semicolon in order to be considered valid PHP syntax.

 

<?php
echo "Hi my name is Dino Cajic";
?>

 

Save your file in the folder where you installed your local server. For example, C:/wamp/www/. If you’re developing on your local machine, head over to http://127.0.0.1 or http://localhost. You will not need to specify index.php after the localhost since Apache already looks for it. If you named your file about.php, you will need to specify it: http://localhost/about.php.

The following example is what gives PHP a bad name so we’ll try to avoid it as much as humanly possible. We can embed PHP into HTML. Let’s create a standard HTML script and inside the body create a PHP variable that we’ll call $name. We’ll cover variables shortly but for now just know that they are storage containers that have the capability of storing numerous different data types. They also have to start with a dollar sign followed by a letter or underscore, followed by any combination of letters, numbers, and underscores.

We’ll be storing our name as a string. If you’re familiar with other programming languages, like Java, you might know that with those languages you have to explicitly state what type of data type you’re storing. So, if you specify that you’re storing a string and you try to store an integer, an error will be thrown. In PHP, we don’t have to do that. PHP will figure it out, which is sometimes good and sometimes bad. Before we dwell too deep into that, lets create a variable.

 

$name = "Dino Cajic";

 

We can jump in and out of PHP as much as we want. Inside of the <body> tag, we’ll start by typing some standard text and them jump into PHP.

 

<body>
  Hello there <?php echo $name; ?>
</body>

 

See the full example below. If we save the file and open it up in our browser, we get: Hello there Dino Cajic.

If you inspect the code, you’ll see that the HTML is the only code that’s displayed. To inspect the code, right click and click View Page Source.

Where’s the PHP? It was interpreted by the Apache server and once the server was done processing it, it sent HTML code to the browser. All of your PHP code will be processed on the server.

That’s it for the introduction. We’ll be building on from here. We’re going to cover most of the PHP concepts in what I predict to be a hundred or so articles. I’ll be posting these each Tuesday/Thursday/Saturday/Sunday. I’m trying to keep up with writing Algorithms articles every Monday/Wednesday/Friday. If you haven’t seen those, and Algorithms interest you, make sure to click on my profile and go through a few of them.

I’m also going to be doing a video walkthrough to accompany each of these articles. If something is not making sense, check out the video for a more visual explanation. All of the code will be posted on my GitHub account, so if you need access to it, grab it from there.

 

PHP Tutorial Series

Continue your PHP Learning.

PHP Basic Syntax

Where to Begin?

PHP – P1: Basic Syntax

PHP is leaps and bounds above where it started. It’s a modern programming language that has Object Oriented capabilities. It’s the backbone of popular frameworks like Laravel. To get us started, let’s learn the syntax.

PHP Comments

Should You Document Your Code?

PHP – P2: Comments

PHP has a few different options when it comes to comments. Frequently, it’s based on preference on which comment style you use, but other times it makes sense to use one over the other.

PHP Variables

Can You Even Store Data?

PHP – P3: Variables Intro

Variables are just storage containers. You can store any data type that you would like inside of them.

Leave a Reply