PHP Variables
How do you declare a PHP variables?
In PHP, a variable starts with the *$* sign, followed by the name of the variable. Example: <?php *$txt* = "Hello world!"; *$x* = 5; *$y* = 10.5; echo *$txt*; echo "<br>"; echo *$x*; echo "<br>"; echo *$y*; ?>
To output/print a variables use the "____" statement.
The PHP *echo* statement is often used to output data to the screen. Example: <?php $name = "My name is Andy."; *echo* $name ?>
What are variable scopes?
The scope of a variable is the part of the script where the *variable can be referenced/used*. The scope will determine how a variable will be accessed. PHP has *three* different variable scopes: • *local* • *global* • *static*
When declaring a variable, you cannot start with...
A numeric value. A variable name must start with a letter or the underscore character. Correct syntax (e.g.): <?php *$age* = "My age is 28"; echo *$age* ?> Incorrect (e.g.): <?php $28age = "My age is 28"; echo $18age; ?>
Are variables case-sensitive?
Answer: *YES!* Variable names are case-sensitive ($age and $AGE are two different variables)