PHP Tutorial: Part 2 – Printing and Variables

September 27, 2021 / Web Development

In our second PHP tutorial, we explain how to print information to the browser screen and explain how you can use variables to hold information.

* Please note that in coding terms, ‘print’ means to display information in the browser, not send it to a printer. 

Printing (displaying) text

Displaying text in your browser is simple to do using PHP script. There are a variety of ways to achieve this but for this tutorial, we’ll be using the print function. Using print will allow you to output text, variables or a combination of the two so that they display on the screen.

Anyone who has a WordPress website will know that it comes with the default text, ‘Hello world!’. This text can be displayed on the screen using the following line of PHP:

print(“Hello world!”);

In this piece of code, the term ‘print’ is the command that tells the script to display the text inside the brackets. Additionally, because you want to output the text, it is also enclosed inside quotation marks. Finally, as with nearly every line in a PHP script, it must end in a semicolon. You would, of course, have to enclose this in your standard PHP tags, making the following code:

print(“Hello world!”);
?>

Doing this would result in Hello world! being displayed in the browser.

PHP Variables

In programming, a variable is a value that can change, depending on conditions or on information inputted into the program. For example, using variables enables a website to display the correct date or time.

With PHP, you can define what the variables are and set the instructions over which variable to print and under what conditions. The most common variable type in PHP is called a string. A string can hold text and numbers. All strings begin with a $ sign. To assign text to a string, you would use the following code:

$welcome_text = “Hello and welcome to my website.”;

This is quite a simple line to understand as everything inside the quotation marks will be assigned to the string.

When creating strings, there are a few important rules you need to remember:

  • Strings are case-sensitive so $Welcome_Text is not the same as $welcome_text
  • String names can contain letters, numbers and underscores but cannot begin with a number or underscore
  • When assigning numbers to strings you do not need to include the quotation marks, so:

$user_id = 987

would be allowed.

Outputting PHP variables

When displaying a PHP variable on the screen, PHP uses the same print code as it does to display text – however, it is expressed in a slightly different way, putting the string inside the brackets rather than the text. For example, the following code would display your welcome text:

$welcome_text = “Hello and welcome to my website.”;
print($welcome_text);
?>

The other difference is that you do not need quotation marks inside the brackets if you are printing a variable.

Formatting text for browser display

As a server-side language, PHP code is executed before the page is sent to the browser. This means that the output from a PHP script will only display text in the browser’s default font. If you want to specify a particular type of font, you will need to format this using HTML.

Today, most website themes have the ability to change font type, colour and size in their options without the need to write any script. However, if you want to use something not included in the theme’s customisable options you can always edit the site’s stylesheet.

Conclusion

Hopefully, from reading this tutorial, you’ll understand the basics of creating PHP scripts that print text to the browser. You’ll also know how to create variables and use these to display text on the browser.

For more information about our hosting solutions for PHP based platforms, like WordPress, visit eUKhost.

Spread the love