Ad space (banner)
๐Ÿ˜PHP Lessons
Lesson 3 / 68

Working with Variables

In this lesson you will learn the basic way to use variables in PHP and get used to its distinctive $ notation. This is for anyone searching for "PHP variables how to use" or "PHP dollar sign".

A variable is like a labelled box that stores a value under a name. PHP variables always start with $. To join strings together you use . (a dot). You never have to declare a type - assigning a value is enough to start using it.

The sample code declares two variables, $name and $age, then builds a greeting by joining strings and variables with the . operator. Unlike JavaScript's + or Python's +, PHP uses . for string concatenation, which is one of its most noticeable traits.

A common stumbling block for beginners is forgetting the leading $. Without it the name is not recognised as a variable and gets mistaken for a constant or a function name, which causes an error. It is one of the first things people coming from other languages have to get used to.

In real-world development everything - user input, values fetched from a database - lands in a variable before it is processed. The distinctive $ notation also makes PHP code easy to spot at a glance.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)