Scotts Web Dev Banner
Did you notice... every article on this site has an associated video? Consider subscribing to Scotts Web Dev on YouTube! :)

Learn PHP Basics: PHP For Beginners

If you’re looking to learn PHP code, let’s dive right in. This article assumes you have a php enabled web server and a code editor.

Use .php File Extension To Run PHP Code

By giving a file a .php file extension you can run PHP code on your php enabled web server. Simply create a file called file.php and run it in your browser. It will output HTML even if there’s no PHP code in it.

If you’re on Windows and can’t see or edit file extensions, here’s how to enable them. Open a folder on your computer and navigate to View -> Show and check the file name extensions box. You should now be able to rename files and edit their file extensions.

How To Enter And Exit PHP Code With Opening & Closing Tags

To begin writing PHP you’ll need to create an opening PHP tag, write your PHP code, and then close it with a closing PHP tag.

That looks like this:

<?php

/**
* Your code goes here
*/

?>

Always use the long version of <?php. And, if your php code is at the end of a file (there’s no HTML after it), don’t put a closing HTML tag. You can, but there can be weird, hard to track errors if you have any characters, spaces, or new lines after the closing PHP tag.

How To Write PHP Comments

You’ll notice in my example above I used a comment to tell you that your code goes here. Comments are plain text and are not shown on your webpage or even in the source of your webpage. They are private and viewable only to the people who have access to the source code of your .php files.

There are two types of comments: single line and multi line. A single line comment only can span one line and a new line breaks out of the comment. It can even go after your code.

A single line comment looks like this, begining with a double forward slash:

// this is a comment

$number = 5; // this is a comment, too

A multiple line comment can span multiple lines and starts with /* and ends with */. Like this:

/*
this is a multiple line comment
it can span multiple lines
and won't close until I put */

Use a lot of comments, especially as a beginner. As you progress into your PHP coding, you’ll begin using comments as documentation, describing variables and functions and even WordPress templates and files.

Making PHP Variables

You create a variable in PHP starting with the dollar sign $. A variable can consist of letters, numbers, and underscores. It cannot start with a number.

Here are some variable examples:

// correct variables
$variable = 'value';
$variable_two = 'value';
$variable2 = 'value';
$_variable3 = 'value';

//incorrect, will cause errors
$2variable = 'value';
$my-variable = 'value'; // has a hyphen

Variables are storage containers for data. You assign a value to a variable using the equals sign. You can assign any type of data to a variable. In this tutorial, we are dealing with strings and integers.

After assigning a variable we will put the semi-colon (;). This signals the end of a line of code in PHP. Every command (not structures like if/else) will end with a semi-colon. Forgetting a semi-colon will cause an error in PHP.

How To Print To The Screen In PHP

The echo command will be one of the most commonly used commands that you will write. You’ll use it everywhere and it prints to the screen. If I have:

echo 'hi';

It will print hi to the screen.

You can also echo variables. When you print a variable to the screen, the variable name is not printed, but rather the value of that variable is printed.

<?php
$my_name = "Scott";
$my_favorite_number = 5;
?>
<p>My name is: <?php echo $my_name; ?>, and my favorite number is: <?php echo $my_favorite_number; ?></p>

If we change the value of the variable we can print different values to the screen. This is where PHP starts to show it’s value! You can pull data from anywhere (a file, a database) and print dynamic data to the screen, having potentially different data for all of your visitors or users.

Dealing With Data Types Strings & Integers

You may have noticed that I put quotes around my name and not around my favorite number. That’s because my name is a string and my favorite number is an integer. Strings are denoted by being surrounded with quotes. You can use single quotes or double quotes. A string is just a piece of data and technically a number could be a string, just by putting it in quotes.

Don’t Mix Quote Types – It Will Cause A Syntax Error.

You can’t have double quotes inside of double quotes or single quotes inside of single quotes. This will cause a syntax error:

$my_name = "My Name Is "Scott"";

You have to use the opposite quote (single or double) inside of a string. To fix this we would write:

$my_name = 'My Name Is "Scott"';

If we have to use the same type of quotes, we can escape the quote mark with a forward slash, like this:

$my_name = "My Name Is "Scott"";

Double Quoted Strings Can Hold Variables

If you have a double quoted string, you can put variables inside of that string. Double quotes interpret variables while single quotes do not.

$my_name = "Scott";
echo "My name is $scott"; // this will work
echo 'My name is $scott'; // this will not, this will print My name is $scott

String Concatenation

You can concatenate (put together) two strings by using a period (dot) character.

$first_name = "Scott";
$last_name = " J";

echo $first_name . $last_name; // concatenates the two strings and prints them

echo 'My First Name Is ' . $firt_name . ' and my last name is' . $last_name; // concatenating variables in a string with single quotes

How To Use If/Else To Write Conditional Code

We can use the if/else structure to write conditional code. This is the basis of a programing language and allows you to perform conditional logic. If this, then that.

$my_number = 4;

if ($my_number < 5) {
  echo "My number is less than 5";
} else {
  echo "My number is greater than 5";
}

We have “if” followed by an expression in parenthesis, then curly braces. Inside the curly braces is code we want to execute if the expression is true. If the expression is not true the else code will be ran.

In the above example, since $my_number is less than 5, the if block will get ran and “My number is less than 5” will get printed to the screen.

If we change the number to 7, the expression evaluates to false and the else block is ran.

PHP Expressions Evaluate To True or False

Every expression always ultimately evaluates to true or false. This is called a boolean data type. When you run an if statement, it only gets ran if the expression evaluates to true;

PHP Has Lots Of Built In Functions

We can use PHP functions to manipulate data or create new functionality. For example we can use mt_rand() to generate a random number between 1 and 10 and run it through our if/else block from above.

$my_number = mt_rand(1, 10);

if ($my_number < 5) {
  echo "My number is less than 5";
} else {
  echo "My number is greater than 5";
}

Functions return a value. And we are assigning the value of function mt_rand to the $my_number variable. Now we are able to use a function to generate a random number and check if it is less than 5 or not.

This is a very basic example but it exemplifies the power of PHP and conditional logic.

Let Me Know What You Need Help On

Please leave a comment on my Youtube Video for Learn PHP Basics In 18 Minutes: PHP For Beginners and let me know how you did with this tutorial and what you need help on. It’ll give me ideas for future videos.