PHP Variables
|
PHP variables provide a way to temporarily store data used in a script. Passing PHP variables between functions is a cornerstone of PHP programming; fully understanding their rules and usage in PHP is paramount.
Naming Conventions
Variables in PHP must abide by certain naming conventions. Specifically, they
- start with the dollar sign
- must contain only letters, numbers, and the underscore
- cannot start with a number
- are case sensitive
This last rule can trip up many programmers as $var and $Var are two separate variables. To minimize confusion and errors, it's best to stick to one naming scheme. The two most common styles are
- Use all lowercase, separating words with underscores: $my_var, $first_name, etc.
- Use primarily lowercase, separating words with capital letters: $myVar, $firstName, etc.
A variable's name should also clearly indicate its purpose and meaning. It is recommend that comments are also used to document variables.
Good PHP development tools, like NuSphere's PhpED provide syntax highlighting for variables (see the variables in bold in the image at right).
Variable Types
PHP supports eight types of variables. Half of these are scalar types, meaning they hold only a single value. These are: Booleans (True/False), integers, floating-point numbers, and strings (any quoted character or sequence of characters). PHP supports two non-scalar types: arrays and objects.
Finally, PHP supports two special types: resources and NULL. Resource types provide a pointer-like reference to database connections, opened files and directories, dynamically-generated images, etc. The NULL type is represented by:
- the constant NULL
- a variable not assigned a value
- a variable that has been unset
In addition to providing the built-in data variable types, PHP provides various mechanisms for PHP variable conversion, alowing you to convert data stored in a variable from one format to another.
|
Article continues:
next page>>
|
|
Download NuSphere PHP IDE
Download a free trial of the fast PHP EDitor and robust Integrated Development Environment for PHP.
|
Buy NuSphere PhpED® now
|
Special Team4 Offer
Get 4 copies of PhpED for the price of 3!
Optimum solution for development teams.
|
|
Need more than 4 licenses? Contact Us for more quantity discounts, please use "Ordering/Payment issue" subject on the form.
|
|
"To be honest its bloody awesome, I have looked at loads of PHP editors and this is THE only one that actual works straight out of the box!!! Brilliant, well done."
Andrew Breward, Director of Technology caboodal.com
|
|