PHP Variable Handling Functions Example
Teams applying “PHP Variable Handling Functions Example” in a production project can use the implementation notes to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.
Hi guys,
Today i will explained how to use variable handling functions in php. This example is so easy to use in php.
This functions is provided to the php language. So you can use easily in your php code. Today i will explaind to the boolval(),empty(),floatval(),gettype(),is_array() to explainded.
So let's start to the example.
boolval() Function
The boolval() is a return to the boolean value of the variable.
<?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>";
echo '"0": ' .(boolval("0") ? 'true' : 'false') . "<br>";
echo "[3, 5]: " .(boolval([3, 5]) ? 'true' : 'false') . "<br>";
echo "[]: " .(boolval([]) ? 'true' : 'false') . "<br>";
?>
Output
0: false
4: true
"": false
"Hello": true
"0": false
[3, 5]: true
[]: false
empty() Function empty() is check to the variable is a empty or not. empty() function is return false if variable is exist and is not empty otherwise return true.
<?php
$value = 0;
if (empty($value)) {
echo "Variable 'value' is empty.<br>";
}
if (isset($value)) {
echo "Variable 'value' is set";
}
?>
Output
Variable 'value' is empty.
Variable 'value' is set.
floatval() Function floatval() function returns the float value of the variable.
<?php
$value1 = "1234.56789";
echo floatval($value1) . "<br>";
$value2 = "1234.56789Hello";
echo floatval($value2) . "<br>";
$value3 = "Hello1234.56789";
echo floatval($value3) . "<br>";
?>
Output
1234.56789
1234.56789
0
gettype() Function gettype() function is returns to the type of variable.
<?php
$value1 = 3;
echo gettype($value1) . "<br>";
$value2 = 3.2;
echo gettype($value2) . "<br>";
$value3 = "Hello";
echo gettype($value3) . "<br>";
$value4 = array();
echo gettype($value4) . "<br>";
$value5 = array("red", "green", "blue");
echo gettype($value5) . "<br>";
$value6 = NULL;
echo gettype($value6) . "<br>";
$value7 = false;
echo gettype($value7) . "<br>";
?>
Output
integer
double
string
array
array
NULL
boolean
is_array() Function is_array() function checks whether a variable is an array or not. This function is return the variable is array then true otherwise return false.
<?php
$value = "Hello";
echo "value is " . is_array($value) . "<br>";
$value2 = array("red", "green", "blue");
echo "value2 is " . is_array($value2) . "<br>";
$value3 = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "value3 is " . is_array($value3) . "<br>";
$value4 = "red, green, blue";
echo "value4 is " . is_array($value4) . "<br>";
?>
Output
value is
value2 is 1
value3 is 1
value4 is
Now you can check your own.
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example