Prerequisites
Before you begin with this lesson, you will need a development environment setup with PHP installed. You can learn how to do this by following our development environment setup guide for PHP. You will also need a code editor for writing PHP. We recommend one of the following: Visual Code, PHPStorm or Notepad++ or you could use an online code editor like vscode.dev. You will also need basic understanding of HTML, CSS and JavaScript as we will use this later in this series.
Creating our first script
For writing our first bit of PHP we need a file where our code is written. All languages have there own file extension, for PHP it is “.php“. a php file can contain PHP, HTML, CSS and JavaScript. Begin by creating an file named index.php. How you create a file can very by editor.
Writing our first line of code
In PHP files we can write PHP code but we can also write HTML, JavaScript or CSS. To let the webserver know we are writing in PHP we need to write a starting and closing tag of PHP.
A starting tag of PHP looks as following:
<?php
A closing tag of PHP looks as following:
?>
Combining these together you should have a file containing:
<?php
// Your php code will be written here between the starting and closing tag.
?>
Now we can start writing our first like of code between the starting tag and closing tag of PHP. For this first exercise we will use the echo function of php. echo lets us display a string to the webbrowser. We will display “Hello World” to the webbrowser we can do this with:
echo "Hello World";
As you can see we use the echo command and pass the string Hello World with the command using the quotes, we end with a semicolon to let PHP know its the end of our command. When opening our php file in the browser we can now see Hello World being displayed.
Using variables in PHP
Variables are values stored in memory that you can use in different places of your code, this can help your organize your code. In PHP variables start with a $ after that you can give them a name, spacial characters or spaces are not allowed in the name of a variable however this has an exception for the underscore this is allowed in a variable name and could be used to replace spaces. The variable name is also case sensitive, meaning the variable $a and $A are not the same but can store different values.
Lets start by moving our string “Hello World” to a variable name called textToDisplay.
$textToDisplay = "Hello World";
As you can see we have created the variable $textToDisplay we asign the string value “Hello World” with the equals symbol and close the line with a semicolon to let PHP know we are finished.
Now we have created the variable lets use the variable in the echo command. The end result of our file will be as below but the result in the webbrowser is exactly the same.
<?php
$textToDisplay = "Hello World";
echo $textToDisplay;
?>
Currently we are using the variable once, while be could use it as many times as we want. We could for example display the text “Hello World” multiple times by duplicating the echo statement.
<?php
$textToDisplay = "Hello World";
echo $textToDisplay;
echo $textToDisplay;
echo $textToDisplay;
?>
the above code will display “Hello World” 3 times after each other making it look like Hello WorldHello WorldHello World to avoid this we can modify the variable $textToDisplay after the first time we echo it with adding a space in front of it. This will change the output for the second and third time we echo the variable $textToDisplay but not the first time as PHP runs from left to right, top to bottom.
<?php
$textToDisplay = "Hello World";
echo $textToDisplay;
$textToDisplay = " Hello World";
echo $textToDisplay;
echo $textToDisplay;
?>
The above code will now resolve our issue of World and Hello being connected, the result will look like: Hello World Hello World Hello World.
But what is we want every “Hello World” on a new line? We can use HTML to achieve this. If you are not familiar with HTML we could recommend reading our [HTML] My first website series where you can learn HTML.
In HTML we can use the break tag <br> to start a new line. We can add this directly in our variable $textToDisplay or we can add it after the $textToDisplay variable in our echo statement.
Solution 1
<?php
$textToDisplay = "Hello World<br>";
echo $textToDisplay;
echo $textToDisplay;
echo $textToDisplay;
?>
In this solution we add the html tag <br> directly in the variable $textToDisplay meaning when ever we use the variable $textToDisplay we always will have a new line after it.
Solution 2
<?php
$textToDisplay = "Hello World";
echo $textToDisplay . "<br>";
echo $textToDisplay . "<br>";
echo $textToDisplay;
?>
In this solution we add the html tag <br> after our variable $textToDisplay in the echo command using a dot this connects 2 strings to each other like glue. This results into us needing to write more code but giving us the freedom to determine if we need the new line when we use the variable. So on our last echo command we do not need a new line so we did not add the <br> tag.
Witch of these solutions would you use, let us know in the comments. In our next lesson we will dive into operations like the math symbols (+, -, * and /) or the dot we used today to glue 2 strings together.
Leave a Reply