Since 2007 phptpoint is the best php training institute in Noida and successfully trained 10000+ students in php and other programming languages like python ,HTML, Mysql and all.
Today we will discuss about types of an array and data types in php.
PHP Data Types
In PHP we use data types to hold different types of data or values. PHP supports eight primitive data types can be categorized further in 3 types:
- Scalar Types
- Compound Types
- Special Types
PHP Data Types: Scalar Types
In PHP there are 4 scalar data types.
- boolean
- integer
- float
- string
PHP Data Types: Compound Types
In PHP there are 2 compound data types.
PHP Data Types: Special Types
In PHP there are two special data types in PHP.
Let’s Understand about Array in PHP!!
- Arrays in PHP is a type of data structure which allows us to keep or we can say multiple elements of similar data type under a single variable with we can save much time and effort of creating a different variable for every data. The arrays are helps to create a list of elements of similar types, which can be accessed using their index or key. Like, we want to store five names and print them accordingly. This could be easily done by the use of five different string variables. But if instead of five, the number rises to hundred, then it would be really difficult for the users to create many different variables. Here array comes into play and helps us to keep and store every element within a single variable and also allows easy access using an index or a key. An array is created using an array()function with PHP.
- C program to find minimum or the smallest element in array. This prints the location or index in which the minimum element occurs in an array.
Example-
- If you have a lists of few items (a list of bikes names, for example), storing the cars in single variables could look like this:
Lets practice this!!
-----------------------------------------------------------------------------------------
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
How Many types of Array we have in PHP?
There are three types of Array.
- Indexed or Numeric Arrays:An array with numeric index place where values are stored linearly.
- Associative Arrays:An array with string index place where instead of linear storage, each value can assigned a specific key.
- Multidimensional Arrays:An array who contains single or many array within it and can accessed via multiple indices.
What is an Indexed or Numeric Arrays?
- Indexed or Numeric type of arrays can be used to store any type of elements, but an index is always a number.
- By default, index starts at zero.
What is Associative Arrays?
- Associative type of arrays is almost same to the indexed arrays but instead of linear storage, every value can be assigned with user-defined key of string type.
What is Multidimensional Arrays?
Multi-dimensional arrays are such arrays which stores another array at every index instead of single element. In another words, we can define multi-dimensional arrays as array of arrays. As the name suggests, every element in this array can be an array and they can also hold other sub-arrays within. Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions.
I hope you all are bit clear about the DATA TYPE and ARRAY in PHP to learn more visit at www.phptpoint.com