PHP: variables
A PHP variable gives a name to a value so server-side code can read, change, and pass that value around.
What you will learn
- How to assign and read a variable
- Why variable names and types matter
- How user input differs from trusted application data
Basic example
<?php $name = 'Aki'; $count = 3; echo $name . ' has ' . $count . ' items.'; ?>PHP variables begin with $. The value can be a string, number, boolean, array, or another kind of data.
Common mistakes
- Do not confuse a string such as
'3'with the number3. - Choose names that describe the value, such as
$userNameor$totalPrice. - Treat request data as untrusted; validate it before using or displaying it.