PHP Datatypes:- A datatype simply describes the way how does a data will be handled by a compiler in a programming lanuage.
PHP supports total eight primitive data types: Integer, Floating point number or
Float, String, Booleans, Array, Object, resource and NULL. These data types are used to construct variables. Now let’s discuss each one of them in detail.
PHP Datatypes | Example
- Integers
- Strings
- Floating Point Numbers
- Booleans
- Arrays
- Objects
- NULL
- Resources
Let us understand few of them with examples because here we are assuming that you know some programming knowledge before.
PHP Booleans
Boolean datatype is a data type which contains only two values either true or false.
Following is an example for how to write a boolean in php.
<?php $value=true; var_dump($value); ?> |
Integer
Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).
Example // Integer
<?php $a = 1234; // decimal number $a = -123; // a negative number $a = 0123; // octal number (equivalent to 83 decimal) $a = 0x1A; // hexadecimal number (equivalent to 26 decimal) $a = 0b11111111; // binary number (equivalent to 255 decimal) echo $a; echo "<br>"; echo $b; echo "<br>"; echo $c; echo"<br>"; echo $d; echo "<br>"; echo $e; ?> |
Strings
Strings are like sentences. They are formed by a list of characters, which is really an “array of characters”. Strings are very useful when communicating information from the program to the user of the program. They are less useful when storing information for the computer to use.
Below are some examples of creating strings in php language.
Example
<?php $str="1x@Y"; $name="William_Smith"; echo $str; echo "<br>"; echo $name; ?> |
PHP Floating Point Numbers or Doubles
Floating point numbers (also known as “floats”, “doubles”, or “real numbers”) are decimal or fractional numbers, like demonstrated in the example below.
The range of floating point numbers in PHP is equivalent to the range of the double type in C, Double can range from 1.7E-308 to 1.7E+308. A double may be expressed either as a regular number with a decimal point or in scientific notation.
Example
<?php $a = 1.234; var_dump($a); echo "<br>"; $b = 10.2e3; var_dump($b); echo "<br>"; $c = 4E-10; var_dump($c); ?> |
PHP NULL
PHP null is a special datatype which contains no value.In following example we can see how does it changes value of any variable.
Example // file_get_contents() Method
<?php $number=10; echo $number; echo "<br>"; $number=null; var_dump ($number); ?> |
Resources
Resource identifiers are acting as a reference or identifier to access resource data provided by a third party external application. These resource data will be return while performing the operations like connecting the database or opening an external file. When we connect the database with the required configuration details like,
$conn = mysqli_connect(localhost,”root”,”admin”,”animals”);
The function will return a resource type data to be stored into $conn variable.
With reference to the resource created, we can get required list of data items from the database.Later we will use this $conn resource variable for many operations on database like for closing a connection for example:- mysqli_close($conn);
Example // file_get_contents() Method
<?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Create database $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) { echo "Database created successfully"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?> |