Php Data Types
PHP Data Types
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:
* Scalar Types(predefined):
It holds only single value. There are 4 scalar data types:
1. Boolean: The simplest data type works a like switch. It holds only two values: TRUE(1) or FALSE(0). It is often used with conditional statements.
2. Integer: It holds only whole number i.e. without decimal points. It can be either positive or negative.
3. Float: Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.
4. String: A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters. String values must be enclosed either within single quotes or in double quotes.
* Compound Types(user-defined):
It can hold multiple values. There are 2 compound data types:
1. Array: It can store multiple values of same data type in a single variable.
2. Object: Objects are instances of user-defined classes that can store both values and functions. They must be explicity declared.
* Special Types:
There are 2 special data types:
1. Resource: Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources.
2. Null: Null is a special data type that has only one value: NULL. It is case-sensitive.
Basic Programming in PHP
Example: A program to calculate sum of two numbers
<!DOCTYPE HTML>
<html>
<body>
<?php
$x=10;
$y=6;
echo $x + $y;
?>
</body>
</html>
Output:
16