PHP is loosely typed,means automatically determines the datatype assigned to each variable.
PHP data types:
Data Type | Description | Example |
Integer | Whole numbers | $age = 30; |
Float/Double | Numbers with decimal points | $price = 19.99; |
String | Sequence of characters | $name = "John"; |
Boolean | Represents true or false | $isStudent = true; |
Array | Collection of elements | $colors = ['red', 'blue', 'green']; |
Object | Instance of a class | $car = new Car(); |
Null | Represents the absence of a value | $var = null; |
Resource | External resource handle (e.g., database) | $file = fopen('file.txt', 'r'); |
Callable | Represents functions/methods as variables | $func = function() {...}; |
Closure | Anonymous functions as objects | $closure = function($x) {...}; |
Scalar data types(integer,float/double,string,boolean) hold single values.
Composite or compound data types (arrays,object) can hold multiple values or a collection of values.
Special data types (null,resource,callable/callbacks,closure ).
In PHP, there are four special data types that are worth mentioning:
- Resource: The resource data type represents an external resource, such as a database connection, file handle, or network connection. It is a placeholder for a reference to an external resource managed by PHP's underlying extensions. Resources are typically created using functions like
fopen()
for files ormysqli_connect()
for database connections.
Example:
$fileHandle = fopen('example.txt', 'r');
// $fileHandle is now a resource representing the open file.
- NULL: The NULL data type represents a variable that has no value or an uninitialized variable. It is commonly used to indicate that a variable does not contain any valid data.
Example:
$variable = null;
// $variable is now NULL, indicating it has no value.
It's important to note that both resource and NULL types are not scalar types because they don't hold single values. Instead, they have special purposes related to external resources and absence of values, respectively.
3 .Callback: In PHP, a callback refers to the ability to pass a function as an argument to another function and then call that function inside the receiving function. It allows you to create more flexible and reusable code because you can dynamically change the behavior of a function by passing different functions as arguments.
<?php
function callBack($p){
return $p * 2;
}
function func($arr, $cb){
foreach ($arr as $v){
echo $cb($v)." ";
}
}
$a = [1,2,5];
func($a, 'callBack');
?>
Let's break down the given PHP code step by step:
We have a function called
callBack($p)
, which takes a parameter$p
and returns its value multiplied by 2.Next, there's a function called
func($arr, $cb)
, which takes two parameters: an array$arr
and a callback function$cb
.The
func()
function iterates through each element of the given array using aforeach
loop.Inside the loop, it calls the callback function
$cb
with the current element$v
as an argument and echoes the result followed by a space.The variable
$a
is assigned an array[1, 2, 5]
.Finally, the
func()
function is called with the array$a
and the string'callBack'
as arguments.
Now, let's see how the code works in practice:
When func($a, 'callBack')
is called:
The
foreach
loop iterates through each element of the array$a
.For each element, it calls the
callBack()
function with the element's value as an argument.callBack($p)
takes the value of$p
(current array element) and multiplies it by 2.The result of the multiplication is echoed.
The loop continues until all elements in the array have been processed.
Output:
2 4 10
Explanation:
For the first element (1),
callBack(1)
returns 2, so '2' is echoed.For the second element (2),
callBack(2)
returns 4, so '4' is echoed.For the third element (5),
callBack(5)
returns 10, so '10' is echoed.
So, the final output is 2 4 10
.
- Closure: In PHP, a closure, also known as an anonymous function, is a special type of function that doesn't have a name and can be defined inline within another function or assigned to a variable. Closures are quite powerful as they allow you to create functions on-the-fly without the need for a formal function declaration.
Think of closures as little helpers or mini-functions that you create whenever you need them and use them right away without bothering to give them a name.
<?php
// Our closure
$double = function($a) {
return $a * 2;
};
// This is our range of numbers
$numbers = range(1, 5);
// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers = array_map($double, $numbers);
print implode(' ', $new_numbers);
?>
Sure! Let's break down the PHP closure function step by step:
- We define a closure (anonymous function) and store it in the variable
$double
. The closure takes a parameter$a
and returns the result of multiplying$a
by 2.
$double = function($a) {
return $a * 2;
};
- Next, we create an array of numbers using the
range()
function. In this example, the array contains numbers from 1 to 5.
$numbers = range(1, 5);
- We use the
array_map()
function to apply the closure$double
to each element of the$numbers
array. Thearray_map()
function takes a callback (in this case, our closure$double
) and an array (in this case,$numbers
), and it returns a new array with the results of applying the callback to each element of the original array.
$new_numbers = array_map($double, $numbers);
- Finally, we print the elements of the
$new_numbers
array, separated by a space, using theimplode()
function.
print implode(' ', $new_numbers);
Output:
2 4 6 8 10
Explanation:
The
$numbers
array contains numbers from 1 to 5:[1, 2, 3, 4, 5]
.The
array_map()
function applies the closure$double
to each element of the$numbers
array.For each number, the closure
$double
doubles its value. So, after applying the closure, we get a new array$new_numbers
with values[2, 4, 6, 8, 10]
.The
implode()
function prints the elements of$new_numbers
as a string with space as the separator, resulting in the output:2 4 6 8 10
.
In summary, the closure allows us to define a custom operation (in this case, doubling the value) and then apply it easily to each element of an array using array_map()
. This demonstrates the power and flexibility of closures in PHP.