Introduction, Setup and “Hello World” - LEARN PHP IN A DAY: The Ultimate Crash Course to Learning the Basics of PHP in No Time (2015)

LEARN PHP IN A DAY: The Ultimate Crash Course to Learning the Basics of PHP in No Time (2015)

Chapter 1. Introduction, Setup and “Hello World”

Introduction to PHP

PHP stands for PHP: Hypertext Preprocessor (originally it meant Personal Home Page) and is one of the most widely used web programming languages (installed on more than 250 million websites). It is similar to languages such as C. But let’s not spend too much time in historical references and get to work!

Setting up Our Work Environment

In order to start working with PHP on your local computer, you need to install a development environment. Why is this so? PHP is a server-side language; so in essence, you need to install a “local server” on your computer in order to run PHP code. There are many solutions that provide PHP packages, but the most common form is known as a LAMP bundle. There are many LAMP bundles out there that fit different operating systems. The most common are:

· XAMPP – an Apache distribution with PHP, MySQL and Perl. Support available for Windows, Linux and Mac;

· WAMP – specific for Windows;

· MAMP – specific for Mac;

Pick one, type the name in Google, follow the download link and instructions and you’ll be all set to go.

Also, you will need some form of text editor. There are many free and paid programs you can find. The one we are using for this course is called Notepad++.

Our Very First PHP file

After having installed your LAMP bundle, you undoubtedly want to create your very first PHP file. If you haven’t already, make sure that you have Apache and MySQL active. To check, go to the control panel of your LAMP setup and you should see something like this:

Navigate to the installation directory of your LAMP bundle. In my case it looks something like: “C:\xampp\htdocs”. “htdocs” is the directory that contains all of your websites. Generally, for the sake of clarity, I like creating a “websites” directory inside of “htdocs” and working from there. In either case, create a new folder for your website and give it a name. Try to refrain from using spaces (use _ instead). Inside that folder, create an “index.php” file. Open the file in your text editor and type the following lines of code:

All PHP code is wrapped within the PHP tags (<?php ?>). Notice that we say all PHP code, not all PHP files. That is, you can have things such as HTML inside a.php file. More on that in another chapter, though.

Some more rules to keep in mind in terms of syntax:

- In PHP every statement ends with a semicolon.

- Comments:

Comments in PHP can be single-line or multi-line. Single line comments start with two backslashes (//). Anything placed after these slashes is considered a comment and will not be executed by the compiler. Multi-line comments start with a forward slash and a star and end with a star and forward slash (/* Comment goes here*/). Again, the compiler will ignore anything placed inside of these symbols.

Here is an example of comments in code:

Multi-line comments are usually used for longer descriptions of blocks of code, while single-line comments are used (as in the example above), when you want to describe what a certain line of code does. Multi-line comments can be also useful for debugging. For example, if you have a portion of code that is not working the way you want it to, you can “comment it off” by using multi-line comments and tests bits of it to see what exactly goes wrong.

There is no rule in PHP that tells you whether to use comments or not to, but it is considered good practice to use comments. If you are going to share your code with other people, you should definitely (and by definitely, we mean always!) use comments. What seems obvious to you will not be so obvious to another coder. Furthermore, even if you are not going to be sharing your code, but are instead just writing some file that you know will be for yourself, you should still make use of comments (even if you don’t comment as extensively as you would if you were sharing). Very often you will find yourself needing to revisit old code and when that happens you would want to quickly be able to remember what you were doing and why you were doing it. Although they don’t add functionality to your code, comments are just as important as the code you write, so don’t ignore them!

Okay, after that digression, let’s return to our first PHP file. Open your web browser of choice and enter the following URL: “localhost/websites/your_folder_name_here/index.php” or remove the “websites” if your new website is in the “htdocs” folder.

You should see the words “Hello World!” being displayed at the top of the web page.

Hurray! Our very first PHP file is done! But that wasn’t very interesting, was it? Let’s move on to chapter two where we learn about the different types of variables inside PHP and start having some more fun!