DevPush

How to use variables in PHP

Introduction

This guide is especially for newbies and beginners who are new to programming as the explanations used here are for people who aren’t at the technical level of a programmer just yet.

The first fundamental thing you will need to learn when starting to program with PHP is variables. In this guide, we will cover what they are and how they are used, as well as a practical example of you running actual PHP code and seeing the result of that code.

What is a variable

Simply put, a variable is a way to store data when writing code.

You can do certain actions with those variables such as output data, so for example a variable could contain the title of a blog and you would output it when showing the HTML of a website.

Types of variables

Below are the different types of data you can use and store in a variable, with the word in brackets representing the actual programming name of the type of data.

  • Text (String)
  • Numbers (Integer)
  • Decimal numbers (Float)
  • True or False (Boolean)
  • A list of data (Array)
  • A thing made up of data and functions bundled together (Object)

How to define variables

Every programming language has a different way of how variables are created, in PHP you would use the $ symbol followed by the name of the variable you want to use.

You then use the = symbol to assign data to that variable.

This can be done as follows…

$customerName = "John Doe";

The above example is where you assign the text (String) “John Doe” to the customer name variable.

Here are examples of how to define variables in the different types listed above.

Here is how to define a number that represents a customer's age.

$customerAge = 25;

Here is an example of a decimal number (float).

$price = 9.99;

You can use true or false (boolean) to indicate whether a specific piece of data should be true or false. For example, the below example will indicate whether an Amazon customer is currently a Prime member or not.

$isAmazonPrimeMember = true;

Here is how a list of data would be setup, the below variable contains a list of two names represented as text (strings).

You use Square brackets to create a list (array) and add multiple types of data separated by a comma.

$customerNames = ["John Doe", "Jane Doe"];

In terms of objects that will be covered in a blog post on PHP classes as you would need to know how to learn what and how classes work.

Run some PHP code

In this section, you will do some actual PHP coding, go to onlinephp.io which is an online PHP editor that you can use to write and execute PHP code.

Once the website has loaded copy the below code and then replace the default code in the text area with the copied code.

<?php

$customerName = "John Doe";
echo $customerName;

The echo command will output the value of the customer name variable to the result box on the right-hand side.

Use the Execute Code button to run the actual code, you should see the name printed out as the result.

php-variables-2.png

If this is the first time you are working with PHP then congratulations as you just ran some PHP code.

Try changing the customer name to something else and clicking the Execute Code button again to see the new name being output.

Conclusion

After going through this guide you should be in a position where you understand the following.

  • What a PHP variable is
  • The different types of variables
  • How to code variables

You will understand more about how variables get used when going through other topics on PHP coding, for now knowing what they are is enough at this point.