TUTSCODE PHP 004: DATA TYPES IN PHP


Posted on 2015-07-08 07:56:12


Although PHP supports many types of data, but in this lesson, I introduce to you 7 data types most commonly used are: string, integer, float, boolean. Here we will go into the details of each type of data to know when to use them and how to use it properly.

1. String

String is defined simply as a string of consecutive transplants. For example, "Hello". Every time I see one paragraph text or a character string is written in quotes or single quotes, we will think of it as a string. For example in PHP:
1
<! -? Php $ str = "Hello all of you! I'm Blanka" ; echo $ str ; ? ->
In the code above, $ str storage of a string value.

2. Integer

Integer, we were familiar with more sets of integers and so I will not say too much about this part. Only a few rules: + Integer must have at least one digit from 0-9 + Integer not marked, "" + Integer not marked. "" Like the decimal. + integer can be positive or negative, a negative sign "-" in front. + The former is a special case of fractions.

3. Float (double hay real number)

For example:
1
2
3
4
5
$firstNumber = 1.234;
$ SecondNumber = 1.23e3
$ ThirdNumber = 7E-10;
The size of a float depending on the platform (platform), the maximum value is approximately 1.8e308

4. Boolean

Variable of type boolean value to true only (TRUE) or false (FALSE). For example:
1
2
3
$x = true;
$ Y = false;
Type boolean values ​​will be used a lot in the conditional expression as if else, so while ...

5. Array

An array is a data type that can store a lot of value in a variable. For example:
1
$a  =  array(1, 4, 5, 5, 6, 7, 56, 67);
or:
1
$b = array("blue", "red", "green");
We can use 1-dimensional array or multidimensional array arbitrary. In the example above, $ a and $ b is 2 dimensional array, we can access the first element of the array: $ a [3]. Examples of 2-dimensional array:
1
2
3
4
5
<!--?php $c = array( array("color"=-->"red", "length"=>"80", "hobby"=>"sport")
);
?>
Access to one element: $ c [0] ["length"].

6. Object

Objects defined in object-oriented programming, it is shown by a class. In PHP, an object must be explicitly declared. First, we must declare an object class using the class keyword. A class is a structure may contain attributes and methods. After declaring class we must initialize the object before using it.
1
2
3
4
5
6
7
8
<!--?php class Car { var $color; function Car($color="green") { $this--->color = $color;
}
function what_color()
{
return $this->color;
}
}
?>

7. Null

As the value type represents no value changes. For example:
1
2
3
$x = null;
var_dump($x);
Results will not print anything. Need to distinguish the value null to empty string. * Conclusion:  Ending the lesson we learned about how the differences between the basic data types, thereby using it more accurately. If you have any questions regarding this section may put questions to us via here.