Control statements and data types in php
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: Show
PHP Data Types: Scalar TypesIt holds only single value. There are 4 scalar data types in PHP.
PHP Data Types: Compound TypesIt can hold multiple values. There are 2 compound data types in PHP.
PHP Data Types: Special TypesThere are 2 special data types in PHP.
PHP BooleanBooleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE. Example: Output: PHP IntegerInteger means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points. Rules for integer:
Example: Output: Decimal number: 34 Octal number: 163 HexaDecimal number: 69 PHP FloatA floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign. Example: Output: Addition of floating numbers: 73.812 PHP StringA 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. But both are treated differently. To clarify this, see the example below: Example: Output: Hello Javatpoint Hello $company PHP ArrayAn array is a compound data type. It can store multiple values of same data type in a single variable. Example: Output: array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" } Array Element1: Royal Enfield Array Element2: Yamaha Array Element3: KTM You will learn more about array in later chapters of this tutorial. PHP objectObjects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared. Example: Output: Bike Model: Royal Enfield This is an advanced topic of PHP, which we will discuss later in detail. PHP ResourceResources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource. This is an advanced topic of PHP, so we will discuss it later in detail with examples. PHP NullNull is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as it is case sensitive. The special type of data type NULL defined a variable with no value. Example: Output: 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 is object data type in PHP?In PHP, Object is a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class. In addition to properties, class defines functionality associated with data.
What is the default data type in PHP?PHP's variables are dynamic, and change depending on the data inside them. So they have no datatype by default.
What are the four scalar types of PHP?There are 4 scalar data types in PHP.. boolean.. integer.. float.. string.. What are the different data types?data type. |