PHP Variables

Summary: in this tutorial, you will learn how to use PHP variables to store data in programs.

Define a variable

A variable stores a value of any type, e.g., a string, a number, an array, or an object.

A variable has a name and is associated with a value. To define a variable, you use the following syntax:

$variable_name = value;Code language: PHP (php)

When defining a variable, you need to follow these rules:

  • The variable name must start with the dollar sign ($).
  • The first character after the dollar sign ( $) must be a letter (a-z) or the underscore ( _ ).
  • The remaining characters can be underscores, letters, or numbers.

PHP variables are case-sensitive. It means that $message and $Message variables are entirely different.

The following example defines a variable called $title:

<?php
$title = "PHP is awesome!";Code language: HTML, XML (xml)

To display the values of variables on a webpage, you’ll use the echo construct. For example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Variables</title>
</head>
<body>
    <?php
        $title = 'PHP is awesome!';
    ?>
    <h1><?php echo $title; ?></h1>
</body>
</html>Code language: HTML, XML (xml)

If you open the page, you’ll see the following message:

PHP is awesome!

Another shorter way to show the value of a variable on a page is to use the following syntax:

<?= $variable_name ?>Code language: HTML, XML (xml)

For example, the following shows the value of the $title variable in the heading:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Variables</title>
</head>
<body>
    <?php
		$title = 'PHP is awesome!';
	?>
    
    <h1><?= $title; ?></h1>
</body>
</html>Code language: HTML, XML (xml)

Mixing PHP code with HTML will make the code unmaintainable, especially when the application grows. To avoid this, you can separate the code into separate files. For example:

  • index.php – store the logic for defining and assigning value to variables.
  • index.view.php – store the code that displays the variables.
  • Use the require construct to include the code from the index.view.php in the index.php file.

The following shows the contents of the index.view.php file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Variables</title>
</head>
<body>
    <h1><?= $title ?></h1>
</body>
</html>Code language: HTML, XML (xml)

And the following shows the contents of the index.php file:

<?php

$title = 'PHP is awesome!';
require 'index.view.php';Code language: HTML, XML (xml)

If you open the index.php file on the web browser, you’ll see the same output.

By doing this, you separate the code responsible for logic and the code responsible for displaying the file. This is called the separation of concerns (SoC) in programming.

Summary

  • A variable stores a value, and its name always starts with $ sign.
  • Use the separation of concerns principle to separate the PHP logic from HTML.
Did you find this tutorial useful?