In this lesson will be talking about PHP Data Types and Variables which is an important topic.
Data types identifies the type of the data we are dealing with in our PHP programs ( Or in any other programming language. ). PHP is supporting two data types, Scalar and Compound. Both are explained below.
Scalar Data Types: Scalar data types have only one value at a time. There are four scalar types in PHP which are int, float, strings and boolean.
Float and Int represent numeric values. Numeric values can be decimal, octal, hexadecimal and exponential. PHP support all of these values for different purposes.
Strings on the other hand seems to be text and it is the case but not exactly true in PHP. In PHP strings are binary data which may be text, content of an image and so any other binary data. But for simplicity at this point just consider Strings as text.
Boolean has only two value, TRUE ( true ) and FALSE ( false ). 1 is also considered is true and 0 is considered as false.
PHP variables are used to hold data. The data may be a result value of a mathematical operation, an integer number, an expression or text. PHP is a loosely typed language means there is no need to declare a variable before it is used. Declaring a variable means to define its data type that why type of data this variable can hold. A PHP variable can hold strings, integer number, float numbers etc etc .
There are some rules for PHP variables.
- PHP variable starts with the $ sign followed by the variable name e.g. $results
- PHP variable name may contain alpha-numeric characters and underscore e.g. $_res_90
- PHP variable name cant be started with a number like $9abc ( Incorrect ). Variable name must start with an underscore or a character.
- PHP variables are case sensitive, means $results and $Results both are different variables.
- PHP variable names should not have spaces like $sum results . If such a variable name is needed, then replace the spaces with the underscore ” _ ” like $sum_results .
Look at the below example and see some variables.
<?php $text = "This is a test text"; $number = 1452.45; ?>
The PHP Lesson 2 : PHP Data Types and Variables ends here, and next lesson is about Arithmetic operations.