In the previous post, we talked about some data types – variables, strings, string functions and numbers. In this post we will talk about more data types which are floats, arrays, array functions, booleans and constants. We will also talk about type casting. So, without wasting time, let us start!
Floating Point Numbers
As we have already talked about numbers, we will now talk about floating point numbers (also known as floats, doubles and real numbers). An example of float is 2.1 or 10.3145 etc. Of course I can’t give the whole list of functions for floats, but I will discuss some of them. You can even look for more at PHP.net. You can view the highlighted source here or the online demo here. Here is a list of all math functions.
Array is a very useful data type when writing codes in PHP. You can think of an array as a series of variables that we can assign values to or as a lot of variables put together in another variable. There is no limit of values you can enter in it. They can contain anything which a variable can contain like strings, numbers, or even another array. It’s a very important concept so please understand it carefully. You can refer an array value by its position number called index. Index of the values in the array starts at 0 (0, 1, 2, 3…). So the index of the 1st value is 0, of the 2nd value it is 1 and so on. Here is a code explaining you how you can use arrays. You can also view the online demo here.
Booleans are used for the concept of true or false. True is also displayed as “1” (one) and false is displayed as “0” or “” (zero or null). They are mainly used in logical expressions (if statements) which we will study later. Also note that there is a difference between the booleans true and false and the strings “true” and “false”. Booleans aren’t wrapped with quotes. You can view a sample code of booleans here and the online demo of that code here.
Constants are just like variables just:
- Their values can’t be changed after being defined.
- Constant names use all capital letters.
- They do not contain a dollar sign (“$”) in the beginning of their names.
PHP tries its best to convert between types (strings, number, arrays, etc.). Sometimes, it makes guesses for what you want to do. Type casting in PHP works much as it does in C. You can view a sample code of Type Casting here and the online demo of that code here. You can view more information on this here.
So, we are all finished with data types! In the next post we will talk about logical expressions (if statements).
Image Credits: NetTuts+