A variable can must be of a certain type but in PHP types are not explicitly defined, instead the variable type is determined by the context in which the variable is used. This means therefore that variables become a type when they are assigned a value that has a type.
As you can see to cast a variable you need to use place the type you want to cast to in front of the variable in brackets. This then applies that type to the variable being cast. Note that you should be careful about what you cast as it can have some unexpected results. For instance casting a fraction to a integer will round the fraction to the nearest integer.
Did you know you can add a sentance and a number together to form another number? Well in PHP you can! Unfortunatley this can lead to unexpected results so you need to be careful how you handle this.
As you can see when adding a string to a number if the first part of the string is a number this is then added together. Any other numbers are ignored. If you add a string with a float in it to a integer the result is a float. Again the correct way to solve this problem is to detect and or cast the variable to the correct type.
A variable type can be identified by using the below functions simply pass the variable to the required function and it will return true if it is of that type. This is the a technique for defensive programming.
This is important as once a particular variables type has been identified you can then be certain of the effect of any operations performed on it.