PHP Variable Handling Functions
Sno
|
Function |
Description |
1 |
boolval() |
Returns
the Boolean value of a variable |
2 |
debug_zval_dump() |
Dumps
a string representation of an internal zend value to output |
3 |
doubleval() |
Alias
of floatval() |
4 |
empty() |
Checks
whether a variable is empty |
5 |
floatval() |
Returns
the float value of a variable |
6 |
get_defined_vars() |
Returns
all defined variables, as an array |
7 |
get_resource_type() |
Returns
the type of a resource |
8 |
gettype() |
Returns
the type of a variable |
9 |
intval() |
Returns
the integer value of a variable |
10 |
is_array() |
Checks
whether a variable is an array |
11 |
is_bool() |
Checks
whether a variable is a boolean |
12 |
is_callable() |
Checks
whether the contents of a variable can be called as a function |
13 |
is_countable() |
Checks
whether the contents of a variable is a countable value |
14 |
is_double() |
Alias
of is_float() |
15 |
is_float() |
Checks
whether a variable is of type float |
16 |
is_int() |
Checks
whether a variable is of type integer |
17 |
is_integer() |
Alias
of is_int() |
18 |
is_iterable() |
Checks
whether the contents of a variable is an iterable value |
19 |
is_long() |
Alias
of is_int() |
20 |
is_null() |
Checks
whether a variable is NULL |
21 |
is_numeric() |
Checks
whether a variable is a number or a numeric string |
22 |
is_object() |
Checks
whether a variable is an object |
23 |
is_real() |
Alias
of is_float() |
24 |
is_resource() |
Checks
whether a variable is a resource |
25 |
is_scalar() |
Checks
whether a variable is a scalar |
26 |
is_string() |
Checks
whether a variable is of type string |
27 |
isset() |
Checks
whether a variable is set (declared and not NULL) |
28 |
print_r() |
Prints
the information about a variable in a human-readable way |
29 |
serialize() |
Converts
a storable representation of a value |
30 |
settype() |
Converts
a variable to a specific type |
31 |
strval() |
Returns
the string value of a variable |
32 |
unserialize() |
Converts
serialized data back into actual data |
33 |
unset() |
Unsets
a variable |
34 |
var_dump() |
Dumps
information about one or more variables |
35 |
var_export() |
Returns
structured information (valid PHP code) about a variable |
PHP boolval() Function
The boolval() function returns the boolean value of a variable.
Syntax
boolval(variable);
Example
<?php
echo "0: " .(boolval(0) ? 'true' : 'false') . "<br>";
echo "4: " .(boolval(42) ? 'true' : 'false') . "<br>";
echo '"": ' .(boolval("") ? 'true' : 'false') . "<br>";
echo '"Hello": ' .(boolval("Hello") ? 'true' : 'false') . "<br>";
?>
Output :-
0: false
4: true
"": false
"Hello": true
PHP empty() Function
- The empty() function checks whether a variable is empty or not.
- This function returns false if the variable exists and is not empty, otherwise it returns true.
Syntax
empty(variable);
Example
: Check whether a
variable is empty. Also check whether the variable is set/declared:
<?php
$a = 0;
// True because $a is empty
if (empty($a)) {
echo "Variable 'a' is
empty.<br>";
}?>
Output:-
Variable 'a' is empty.
PHP floatval() Function
- The floatval() function returns the float value of a variable.
- The float value of the variable on success, 0 on failure. An empty array will return 0, and a non-empty array will return 1
Syntax
floatval(variable);
Example
Return the float value of different variables:
<?php
$a = "1234.56789";
echo floatval($a)
. "<br>";
?>
Output:-
1234.56789
PHP intval() Function
- The intval() function returns the integer value of a variable.
Syntax
intval(variable, base);
Example
Return the integer value of different variables:
<?php
$a = 32;
echo intval($a)
. "<br>";
$e = array("red", "green", "blue");
echo intval($e) . "<br>";
?>
Outputs: -
32
1
PHP is_array() Function
- The is_array() function checks whether a variable is an array or not.
- This function returns true (1) if the variable is an array, otherwise, it returns false/nothing.
Syntax
is_array(variable);
Example:-
Check whether a
variable is an array or not:
<?php
$a = "Hello";
echo "a is " .
is_array($a) . "<br>";
$b = array("red", "green", "blue");
echo "b is " .
is_array($b) . "<br>";
?>
Outputs:-
a is
b is 1
PHP is_bool() Function
- The is_bool() function checks whether a variable is a boolean or not.
- This function returns true (1) if the variable is a boolean, otherwise, it returns false/nothing.
Syntax
is_bool(variable);
Example:-
Check whether a
variable is a boolean or not:
<?php
$a = 1;
echo "a is " .
is_bool($a) . "<br>";
$c = true;
echo "c is " .
is_bool($c) . "<br>";
?>
Outputs:-
a is
c is 1
PHP is_callable() Function
- The is_callable() function checks whether the contents of a variable can be called a function or not.
- This function returns true (1) if the variable is callable, otherwise, it returns false/nothing.
Syntax
is_callable(variable, syntax_only, name );
Example
:- Check whether the contents
of a variable can be called as a function or not:
<?php
function test1(){
}
echo "test1 is callable: " . is_callable("test1");
echo "<br>";
echo "test2 is callable: " . is_callable("test2");
?>
PHP is_countable() Function
- The is_countable() function checks whether the contents of a variable are a countable value or not.
- This function returns true (1) if the variable is countable, otherwise, it returns false/nothing.
Syntax
is_countable(variable);
Example
:- Check whether the contents
of a variable is a countable value or not:
<?php
$a = "Hello";
echo "a is " .
is_countable($a) . "<br>";
$b = array("red", "green", "blue");
echo "b is " .
is_countable($b) . "<br>";
?>
PHP is_float() Function
- The is_float() function checks whether a variable is of type float or not.
- This function returns true (1) if the variable is of type float, otherwise, it returns false.
Syntax
is_float(variable);
Example
:- Check whether a
variable is of type float or not:
<?php
$a = 32;
echo "a is " .
is_float($a) . "<br>";
$c = 32.5;
echo "c is " .
is_float($c) . "<br>";
?>
PHP is_int() Function
- The is_int() function checks whether a variable is of type integer or not.
- This function returns true (1) if the variable is of type integer, otherwise, it returns false.
Syntax
is_int(variable);
Example
:- Check whether a
variable is of type integer or not:
<?php
$a = 32;
echo "a is " . is_int($a)
. "<br>";
$d = "32";
echo "d is " .
is_int($d) . "<br>";
$e = true;
echo "e is " .
is_int($e) . "<br>";
?>
PHP is_iterable() Function
- The is_iterable() function checks whether the contents of a variable is an iterable value or not.
- This function returns true (1) if the variable is iterable, otherwise it returns false/nothing.
Syntax
is_iterable(variable);
Example
:- Check whether the
contents of a variable is an iterable value or not:
<?php
$a = "Hello";
echo "a is " .
is_iterable($a) . "<br>";
$b = array("red", "green", "blue");
echo "b is " .
is_iterable($b) . "<br>";
?>
PHP is_null() Function
- The is_null() function checks whether a variable is NULL or not.
- This function returns true (1) if the variable is NULL, otherwise, it returns false/nothing.
Syntax
is_null(variable);
Example
:- Check whether a
variable is NULL or not:
<?php
$a = 0;
echo "a is " .
is_null($a) . "<br>";
$b = null;
echo "b is " .
is_null($b) . "<br>";
?>
PHP is_numeric() Function
- The is_numeric() function checks whether a variable is a number or a numeric string.
- This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.
Syntax
is_numeric(variable);
Example
:- Check whether a
variable is a number or a numeric string, or not:
<?php
$a = 32;
echo "a is " .
is_numeric($a) . "<br>";
$c = 32.5;
echo "c is " .
is_numeric($c) . "<br>";
$e = true;
echo "e is " .
is_numeric($e) . "<br>";
?>
PHP is_object() Function
- The is_object() function checks whether a variable is an object.
- This function returns true (1) if the variable is an object, otherwise, it returns false/nothing.
Syntax
is_object(variable);
Example
:- Check whether a
variable is an object or not:
<?php
function get_cars($obj)
{
if (!is_object($obj)) {
return false;
}
return $obj->cars;
}
$obj = new stdClass();
$obj->cars = array("Volvo", "BMW", "Audi");
var_dump(get_cars(null));
echo "<br>";
var_dump(get_cars($obj));
?>
PHP is_string() Function
- The is_string() function checks whether a variable is of type string or not.
- This function returns true (1) if the variable is of type string, otherwise, it returns false/nothing.
Syntax
is_string(variable);
<?php
$a = "Hello";
echo "a is " .
is_string($a) . "<br>";
$c = 32;
echo "c is " .
is_string($c) . "<br>";
$d = "32";
echo "d is " .
is_string($d) . "<br>";
?>
PHP isset() Function
- The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL.
- This function returns true if the variable exists and is not NULL, otherwise, it returns false.
Syntax
isset(variable, ....);
Example
:- Check whether a
variable is empty. Also check whether the variable is set/declared:
<?php
$a = 0;
// True because $a is set
if (isset($a)) {
echo "Variable 'a' is
set.<br>";
}
?>
PHP print_r() Function
The print_r() function prints the information about a variable in a more
human-readable way.
Syntax
print_r(variable, return);
Example
:- Print the
information about some variables in a more human-readable way:
<?php
$a = array("red", "green", "blue");
print_r($a);
?>
PHP serialize() Function
- The serialize() function converts a storable representation of a value.
- To serialize data means to convert a value to a sequence of bits so that it can be stored in a file, a memory buffer, or transmitted across a network.
Syntax
serialize(value);
Example
:- Convert a storable
representation of a value:
<?php
$data = serialize(array("Red", "Green", "Blue"));
echo $data;
?>
PHP settype() Function
The settype() function converts a variable to a specific type.
Syntax
settype(variable, type);
Example
Convert variables to specific types:
<?php
$a = "32"; // string
settype($a, "integer"); // $a is now integer
$b = 32; // integer
settype($b, "string"); // $b is now string
$c = true; // boolean
settype($c, "integer"); // $c is now integer (1)
?>
PHP strval() Function
The strval() function returns the string value of a variable.
Syntax
strval(variable);
Example
:- Return the string
value of different variables:
<?php
$a = "Hello";
echo strval($a)
. "<br>";
$b = "1234.56789";
echo strval($b)
. "<br>";
$d = "Hello1234.56789";
echo strval($d)
. "<br>";
?>
PHP unset() Function
The unset() function unsets a variable.
Syntax
unset(variable, ....);
Example
: -Unset variables:
<?php
$a = "Hello
world!";
echo "The value of variable 'a' before unset: " . $a . "<br>";
unset($a);
echo "The value of variable 'a' after unset: " . $a;
?>
PHP var_dump() Function
- The var_dump() function dumps information about one or more variables.
- The information holds the type and value of the variable(s).
Syntax
var_dump(var1, var2, ...);
Example
:- Dump information
about different variables:
<?php
$b = "Hello
world!";
echo var_dump($b)
. "<br>";
$c = 32.5;
echo var_dump($c)
. "<br>";
$d = array("red", "green", "blue");
echo var_dump($d)
. "<br>";
?>
PHP var_export() Function
- The var_export() function outputs or returns structured information about a variable.
- This function works similar to var_dump(), except that the returned value for this function is valid PHP code.
Syntax
var_export(variable, return);
Example
Output structured information about variables:
<?php
$b = "Hello
world!";
echo var_export($b)
. "<br>";
$c = 32.5;
echo var_export($c)
. "<br>";
$d = array("red", "green", "blue");
echo var_export($d)
. "<br>";
?>
=================================================
0 Comments