PHP 101: Easy Get Started Guide For Beginners
PHP is a widely used open source scripting language that has gained popularity over the years. In this article we will learn the basics so you can quickly get started with coding in the language and add it to your toolbox.
What is PHP?
PHP stands for Hypertext Preprocessor. It was originally developed and released by Rasmus Lerdorf in 1995. Since then, PHP has become one of the most commonly used web programming languages on the server side. Today, PHP powers websites such as Facebook, Twitter, Wikipedia, WordPress, Shopify, Instagram, and many other big brands.
Brief History of PHP
PHP is a server-side scripting language that was originally developed by Rasmus Lerdorf in 1994. It has been used for web development since 1995 (in the beginning PHP stood for “Personal Home Page”) and is now the most popular programming language on the Internet, with over 100 million websites using it.
The popularity of PHP has grown steadily since its inception, and today there are more than 1 billion active users worldwide.
Why Learn PHP?
There are several reasons why you should learn PHP:
1) You want to know how to code.
2) You want to start a career in software engineering.
3) You want to be able to create dynamic websites.
4) You want to build an online portfolio or resume.
5) You want to use PHP to automate repetitive tasks.
Whatever the reason may be, learning PHP will help you develop skills that are essential to any developer’s toolkit.
PHP for Beginners – Learn the Basics
In this section, we’ll cover some basic concepts about PHP and introduce you to the syntax. We’ll also take a look at what makes PHP different from other programming languages. Finally, we’ll give you a brief introduction to the VS Code IDE.
What You should Know Before Starting to Learn PHP
Before starting to learn PHP, make sure you have the following knowledge:
• Basic HTML & CSS
• An understanding of basic coding concepts like variables, loops, functions, arrays, etc.
• Some experience with JavaScript
• Understanding of the difference between client-side and server-side technologies
To Be Able To Code PHP You Need a Text Editor
A text editor is a program that allows you to write code. There are many different editors available but one of the best free ones is called Visual Studio Code from Microsoft. It’s easy to use and comes with lots of features.
You can download VS Code here or run it online without downloading at vscode.dev
If you want to know more about VS Code, check out our article on how to get started using Visual Studio Code.
PHP Quick start – How to Install PHP
Installing PHP on Windows With XAMPP
1. Download XAMPP
The XAMPP installer contains all the necessary components required to run a web server and PHP scripts.
You can download XAMPP for Windows here
2. Extract and Install XAMPP
After downloading the installer, extract the contents of the archive into a folder of your choice. For example, if you choose C:\xampp\, then you would have extracted the files under the xampp directory. Run the installer.
3. Start the Control Panel
Start the XAMPP control panel by clicking the XAMPP icon
4. Start Apache Server
Start the Apache Server. We do not need the other services running for learning to code in PHP.
Installing PHP on Mac With XAMPP
1. Download XAMPP for Mac
Download XAMPP for Mac from the official website.
You can download XAMPP for Mac here
2. Unzip XAMPP
Unzip the downloaded file. The unzipped file will contain two folders named xampp and xampp-macos. Copy both these folders to your Applications folder.
3. Start the Control Panel
Open Finder and navigate to the xampp application. Click on the XAMPP icon. This will launch the XAMPP control center.
4. Start Apache Server
Click on the Apache button in the list of services. We do not need the other services running to learn PHP.
Installing PHP and Apache on Linux
There are many different Linux operating systems and every system has many different ways of installing software. That said, the steps below are generic enough to work across most distributions.
Install LAMP Stack With APT
Step 1: Update Your System
sudo apt-get update
Step 2: Install Apache server
sudo apt-get install apache2 apache2-doc apache2-npm-prefork apache2-utils libexpat1 ssl-cert
Step 3: Install Mysql
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
Step 4: Install PHP
sudo apt install php8.1
Step 5: Install PHPmyadmin
PHPmyadmin is a tool writen in PHP to manage MySQL databases. It can be used as an alternative to the command line interface (CLI). To install it, type:
sudo apt install phpmyadmin php-mbstring php-gettext
How to Code Your First PHP Program – Learn Basic Syntax With “Hello World” in PHP
In this tutorial, we’re going to show you how to code your first PHP program using the “hello world” script.
First things first, let’s create a simple text document called hello_world.php. Open up your favorite text editor and paste the following code inside:
Example 1: “Hello World” in PHP
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Save the file on your webserver and open it up in your browser. You should see something like this:
Hello World!
You’ve just created your first PHP program! Now, let’s take a look at some more basic syntax.
The Three Basic Programming Constructs in PHP
1. Sequence
As we mentioned in the guide to coding, sequence refers to a set of instructions that allows a computer to carry out an action. Step by step.
If you for example want to make a application that greeets the user with his or her name you need an algorithm with a function that is executed and a sequence of at minimum two statements.
- Let the user input its name and store the name for later use.
- Output the text so that the user can read it.
Example 3: PHP – Input and Output
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="post">
Enter Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
/*
This example will read the value from the "inputText" input field.
Then store the value in the variable called $name.
*/
$name = $_POST["name"];
?>
Hello <?php echo $name; ?>
</body>
</html>
Notes:
- In PHP comments to the code is made with // or #
- Comments more then one line start with /* and end with */
2. Selection (Conditional Statements)
Selection is a type of statement that lets you check whether one condition is met before taking another action:
If this -> do that
In this example we will check if the name the user entered is Santa.
Example 4: Selection in PHP
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="post">
Enter Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
$name = $_POST["name"];
if ($name == "Santa") {
echo "Hi Santa, I promise I have been good this year!";
} else {
echo "Hello " . $name . "!";
}
?>
</body>
</html>
Notes:
- We here use the if statement to do our test to see if the variable “name” is equal to santa.
- In PHP “=” is used to assign values to variables. To compare values you need to use “==” instead. To check if something is not equal to you can use “!=“
- In PHP we use . to combine strings and variables (In languages like JavaScript we instead use +)
Exercise:
Write a program that asks for a password.
- Store the password in a variable and when the button is clicked compare the password with the input.
- Congratulate the user if the password is correct.
- Let the user know if the password is incorrect.
3. Iteration (Loops)
Iteration is a sequence of instructions that are executed over and over again.
We will here use the PHP for statement to make a loop that will print the name in a number of times defined by the user.
Example 5: Iteration in PHP
Note:
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="post">
Enter Name: <input type="text" name="name">
How many times: <input type="number" id="times"
<input type="submit">
</form>
<?php
$name = $_POST["name"];
$numberoftimes = $_POST["times"];
$html_out = "";
for ($i = 0; $i < $numberoftimes; $i++) {
$html_out .= "I ❤️ " . $name . "!<br>";;
}
echo $html_out;
?>
</body>
</html>
- The variable i (or x) is often used as a incrementor. Something we can store a value in that we can add to for every run in the loop. This is what i++ does. It ads 1 to i for every time the loop is run.
- We use the html_out variable to store what will be sent to the user at the end.
PHP Resources
There are many great PHP resources online to help you get started with programming. Here are a few links to get you started:
Online PHP Courses
- freeCodeCamp: freeCodeCamp is one of the best places to learn how to code. They have tutorials on everything from HTML to CSS to JavaScript to Python. If you’re looking to start learning web development and PHP, I highly recommend checking them out.
- W3Schools: W3Schools has a ton of information about PHP. From beginner to advanced, they cover all aspects of the language.
- Codecademy: Codecademy offers interactive lessons on topics like PHP, HTML, CSS, Javascript, Ruby, Java, iOS Development, Android Development, and much more.
Communities and Websites
- Stack Overflow: Stack Overflow is by far the largest community of programmers on the internet. You’ll find tons of questions and answers there.
- Reddit: Reddit is a social news website where users submit content that gets voted up or down based on quality and popularity. The site is split into different sub-reddits which range from technology to politics to gaming.
- Quora: Quora is a question and answer website similar to Yahoo Answers. There’s a lot of good information on the site.
- CodePen: CodePen is an awesome resource for building your own websites. You can write code directly in their editor and preview it instantly in your browser.
- GitHub: GitHub is a place where people share projects and collaborate on open source software.
- YouTube: There are tons of videos on YouTube related to programming in PHP.
Top 5 Reasons Why We Love PHP
Here are some reasons why we love PHP.
1. Easy to Learn
Learning PHP is easy because it uses a simple syntax. When you first start using PHP, you may feel overwhelmed by the amount of things you need to do. But once you understand the basics, you’ll see that it’s actually pretty straightforward.
2. Great Community
PHP has a huge community behind it. There are lots of forums, blogs, and other ways to connect with others. And if you ever get stuck, you can always ask someone else!
3. Lots of Free Tools
If you want to build something cool, chances are you’re going to need a tool. Luckily, PHP comes with a bunch of tools built right in. For example, you can create databases, upload files, send emails, and even make websites and apps.
4. Popular
PHP is used everywhere on the web. Sites like Facebook use PHP to run their backend. It’s also commonly used to develop WordPress sites.
5. Reliable
PHP was designed to be secure and stable, it makes sure that your data stays safe. But you still need to take precautions when working with it. A stable coding language does not help if the code written on it is unstable and insecure.
Conclusion
We hope this guide helped you get started and decide whether or not you should learn more about coding in PHP.