Variables
Tutorials

PHP prefixes all variable names with the $ dollar sign. You can think of it as just part of the naming convention, and you'll soon get used to it.

Variables can be assigned values, and have other operations performed on them.

<?php
$price = 20;
$discount = 10;
$priceToPay = $price - $discount;

Variable Types

In PHP, every variable has a type, such as a string, int, float, boolean, a class, or many more.

You don't declare a type directly when you create a variable in most code blocks, instead, it is based on the contents of what you put in it.

<?php
$age = 35;          // this is an int type
$age = "35";        // this is a string type
$isPhpGreat = true; // this is a bool type

PHP will try and automatically convert between types in response to certain operations.

For example, as long as each string contains a strict number, you can use the mathematical operators on them:

<?php
echo "5" + "10"      // result is 15
To Top