How many data types are in php?
PHP Data TypesVariables can store data of different types, and different data types can do different things. Show PHP supports the following data types:
PHP StringA string is a sequence of characters, like "Hello world!". A string can be any text inside quotes. You can use single or double quotes: Example$x = "Hello world!"; echo $x; Try it Yourself » PHP IntegerAn integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. Rules for integers:
In the following example $x is an integer. The PHP var_dump() function returns the data type and value: PHP FloatA float (floating point number) is a number with a decimal point or a number in exponential form. In the following example $x is a float. The PHP var_dump() function returns the data type and value: PHP BooleanA Boolean represents two possible states: TRUE or FALSE. Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial. PHP ArrayAn array stores multiple values in one single variable. In the following example $cars is an array. The PHP var_dump() function returns the data type and value: Example$cars = array("Volvo","BMW","Toyota"); Try it Yourself » You will learn a lot more about arrays in later chapters of this tutorial. PHP ObjectClasses and objects are the two main aspects of object-oriented programming. A class is a template for objects, and an object is an instance of a class. When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties. Let's assume we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties. When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Exampleclass Car { $myCar = new Car("black", "Volvo"); Try it Yourself » PHP NULL ValueNull is a special data type which can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it. Tip: If a variable is created without a value, it is automatically assigned a value of NULL. Variables can also be emptied by setting the value to NULL: PHP ResourceThe special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP. A common example of using the resource data type is a database call. We will not talk about the resource type here, since it is an advanced topic. Data Types define the type of data a variable can store. PHP allows eight different types of data types. All of them are discussed below. There are pre-defined, user-defined, and special data types. The predefined data types are:
The user-defined (compound) data types are:
The special data types are:
The first five
are called simple data types and the last three are compound data types: 1. Integer: Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point. They can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The range of integers must lie between -2^31 to 2^31. Example: PHP
2. Double: Can hold numbers containing fractional or decimal parts including positive and negative numbers or a number in exponential form. By default, the variables add a minimum number of decimal places. The Double data type is the same as a float as floating-point numbers or real numbers. Example: PHP
Output 705.11 float(705.11) 3. String: Hold letters or any alphabets, even numbers are included. These are written within double quotes during declaration. The strings can also be written within single quotes, but they will be treated differently while printing variables. To clarify this look at the example below. Example: PHP
Output The name of the Geek is Krishna The name of the geek is $name string(7) "Krishna" 4. Boolean: Boolean data types are used in conditional testing. Hold only two values, either TRUE(1) or FALSE(0). Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean. Apart from NULL, 0 is also considered false in boolean. If a string is empty then it is also considered false in boolean data type. Example: PHP
Output This condition is TRUE 5.
Array: Array is a compound data type that can store multiple values of the same data type. Below is an example of an array of integers. It combines a series of data that are related together. PHP
Output First Element: 10 Second Element: 20 Third Element: 30 array(3) { [0]=> int(10) [1]=> int(20) [2]=> int(30) } We will discuss arrays in detail in further articles. 6. Objects: Objects are defined as instances of user-defined classes that can hold both values and functions and information for data processing specific to the class. This is an advanced topic and will be discussed in detail in further articles. When the objects are created, they inherit all the properties and behaviours from the class, having different values for all the properties. Objects are explicitly declared and created from the new keyword. PHP
Output This is an example of Object Data Type! 7. NULL: These are special types of variables that can hold only one value i.e., NULL. We follow the convention of writing it in capital form, but it’s case-sensitive. If a variable is created without a value or no value, it is automatically assigned a value of NULL. It is written in capital letters. Example: PHP
8. Resources: Resources in PHP are not an exact data type. These are basically used to store references to some function call or to external PHP resources. For example, consider a database call. This is an external resource. Resource variables hold special handles for files and database
connections. Note:
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks
and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. What are the types of data type in PHP?PHP supports the following data types:. String.. Integer.. Float (floating point numbers - also called double). Boolean.. Array.. Object.. Resource.. How many data types are there?Most modern computer languages recognize five basic categories of data types: Integral, Floating Point, Character, Character String, and composite types, with various specific subtypes defined within each broad category.
What are the 7 data types?Integer (int) It is the most common numeric data type used to store numbers without a fractional component (-707, 0, 707).. Floating Point (float) ... . Character (char) ... . String (str or text) ... . Boolean (bool) ... . Enumerated type (enum) ... . Array. ... . What are the 11 data types?Data type. Boolean (e.g., True or False). Character (e.g., a). Date (e.g., 03/01/2016). Double (e.g., 1.79769313486232E308). Floating-point number (e.g., 1.234). Integer (e.g., 1234). Long (e.g., 123456789). Short (e.g., 0). |