Introduction to PHP
Introduction
WordPress is written with PHP, so itâs worth knowing PHPâs basics to work with WordPress files.
The first thing to note about PHP is that itâs a server-side scripting language, so it needs to run on a server which is why we have Xampp (or another local server) running when developing locally.
PHP is written in .php files that the server reads and converts to HTML displayed in the browser.
Getting started
To start working with PHP, we need to create a .php file. This .php file needs to be on a server. You can place it into your âhtdocsâ just like your WordPress installation.
To write PHP in our .php file, we need to write our PHP in between tags that start with
A common statement you will see in PHP is âechoâ which outputs a string (often HTML) to a page.
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h1>Hello World!</h1>";
?>
</body>
</html>
In the example above, youâll see that a PHP file looks a lot like an HTML file; the only difference is it uses the PHP tags to add PHP to the page.
Here weâre adding an h1 to the page saying âHello world!â. Copy this code and run it in a .php file on your local server. You can place it in a folder in your htdocs folder and then visit the page in your browser, e.g. http://localhost/phptest/index.php
Variables
A variable name in PHP must start with the $ symbol followed by the variableâs name, and then an equals symbol to set the variableâs value. Then close the line with a semi-colon. Semi-colons arenât optional in PHP.
<?php
$name = "John Doe";
$age = 35;
?>
As you can see, we use quotation marks to create a string, while number variables donât need quotation marks.
Adding this code to the page will only create the variables. If we want to show these variables on the page, we will use the echo statement like this:
<?php
$name = "John Doe";
$age = 35;
echo $name;
echo $age;
?>
Concatenation
If we want to combine variables and HTML, we could use concatenation. To do this, we use the dot operator (.). The dots act like plus signs in JavaScript.
Letâs say we wanted to add a line to the page that says âJohn Doe is 35 years oldâ. We could write the following:
<?php
$name = "John Doe";
$age = 35;
echo "<p>" . $name . " is " . $age . " years old</p>";
?>
See how we use â.â to concatenate variables and write HTML directly into the echo to build the HTML page.
Activity
WATCH
Thereâs plenty to learn about PHP, but for now, these are the absolute basics and will help you better understand whatâs happening in WordPressâs PHP files.
If you would like to learn more about PHP, you can watch the following video: PHP Essential Training
Lesson Task
Brief
Get some experience working with PHP. Having a basic understanding of PHP will help when youâre working with WordPress. More often than not, if you run into WordPress problems, there will be a forum post answering your issue. However, implementing many of the fixes and changes will require some understanding of PHP.
Level 1 Process
- Turn on your local server.
- Go to your âhtdocsâ folder and create a folder called âcms-task2-1.â
- Inside this folder create an index.php file and open it in your browser as it runs on your local server. The file path will probably be localhost/cms-task2-1/index.php, but it depends on which local server youâre running.
- Open index.php in your code editor.
- Create a basic HTML framework, and add PHP tags into the body of the page.
- Create variables for two numbers and add them together and then echo the result onto the page.
- Create a variable for the userâs name and echo that with the string âHello ____. Welcome backâ.