[thread=729156]Previous Tip: Boolean storage[/thread]
Forgive me for the delay - work and life interfered. I’ll try to remember to get these out on weekdays as intended. PM me for suggestions, reminders to make a post and suggestions.
Anyway, the subject of datatypes is one of those arcane necessities of programming that makes life difficult when you first start programming. At the beginning conceptualizing a variable as a “box” you can put stuff in is tricky, and advanced programmers forget how tricky. Worrying about datatype, well that’s right out.
PHP tries to help by being a “loosely-typed” language. What this means is that, as a language PHP doesn’t enforce datatype matching and casting until you ask it to. This has the effect of making life a little more difficult for experienced programmers, but the trade off is that the language is easier to learn.
In PHP all variables are prefixed with $. I once read or was told that the $ stood for scalar. What’s a scalar? Well, it’s a variable construct that can be anything.
You see, while PHP can handle any datatype in any variable, most languages can’t. C++ certainly can’t, and the PHP engine deals with this.
The main reason for the $ is speed - when looking up a variable for the current scope the engine does not have to traverse the constants, function and class names. This is important in an interpreted language, and less so for a compiled one - but I digress.
Eventually you will need to know the type of data you are working with, or insure that a variable is of a certain type. The most frequent time you will need to do this is with the database because the database fields are set up to deal with data by type. A varchar(20) field is meant for string data between 0 and 20 characters. An int(11) field is for 11 digit integers, and so on.
When you cast a variable, you force it’s datatype to become the type specified. There are two ways to do it. The older way
$a = '003';
echo $a; // '003';
$a = intval($a);
echo $a; // '3';
There are strval, boolval, and so on functions. The newer way is to set the type with a casting operator
$a = '003';
$a = (int) $a;
echo $a; // '3';
The current casting operators are:
[list]* or (integer) to cast to an integer
* or (real) to cast to a floating-point number
* to cast to a string
* or (boolean) to cast to a boolean
* to cast to an array
* to cast to an object
[/list]
With PHP 5.4 this will become more important as type hinting becomes more prevalent. Right now a function can require that its arguments match a certain datatype. The catch is the types supported in 5.3 are user defined objects and the array type, like so.
function ( myClass $a, array $b ) {}
If you pass an object (or anything) that isn’t an instance of myClass as the first argument to the function above it will throw a catchable fatal error. In PHP 5.4 the primitive datatypes will be type hintable (This is a pending feature addition and might be removed before final).
function ( bool $a, int $b ) {}
Now you might be wondering why you’d want to do that. Well, knowing the datatype makes the code easier to test and easier to write once you are used to it. It also opens the door to function overloading in future versions of PHP, at least at a syntax level. Function overloading allows different functions to be called depending on the datatype of the arguments. Example
// Theoretical code - will not run.
function validate ( int $a ) {}
function validate ( string $a) {}
function validate ( array $a ) {}
function validate ( $a ) {}
Here we have 4 functions with the exact same name - impossible under current PHP. In function overloading though the datatype determines the function that gets used - if the argument is a string the 2nd function is invoked. If the datatype isn’t an int, string or array the final function with the unspecified datatype is invoked.
C++ and several other languages already allow this sort of behavior.
Well, that’s enough for a simple tips post. Hope this is useful.