Variable Related Functions
Let's look at the functions associated with variables.
| Variable Functions | meanings |
|---|---|
|
gettype(variable) |
Return data type |
|
settype(variable, Data type to change) |
Change data type |
|
intval() |
Return data type as integer value |
|
strval() |
Return data type as character value |
|
isset() |
Determine if variable is declared |
|
unset() |
Remove variable |
|
empty() |
True if the value of the variable is empty, false otherwise |
|
is_array() |
Determine if array |
|
is_double() |
Determine if variable type is double |
|
is_bool() |
Determine if variable type is boolean |
|
is_integer() |
Determine if variable type is integer |
|
is_null() |
Determine if variable type is null |
|
is_numeric() |
Determine if variable is number |
|
is_object() |
Determine if variable type is object |
gettype
This function finds what type a variable is.
For example, a string function is a string.
If it is a number, it is a number. And so on. If the number is inside '', do you know that the computer recognizes it as a letter?
<?php
$var = '3434';
echo gettype($var).'<br />';
$var = 3434;
echo gettype($var).'<br />';
?>
settype
This function changes the type of a variable.
For example, you can change a function of a string to a number and a function of a number to a string.
The example below is a source that replaces the string 98 with a number.
<?php
$var1 = '98';
settype($var1, "integer");
echo gettype($var1);
?>
intval
This function converts a variable value to an integer type.
<?php
$var1 = 33.232;
echo intval($var1);
?>
strval
A function that returns a variable as a character value.
<?php
$var1 = 33;
$var2 = strval($var1);
echo gettype($var2);
?>
isset
This function checks whether a variable exists.
<?php
if(isset($var)){
echo 'exists variable';
}else{
echo 'non-existent variable'';
}
?>
unset
Function to delete a variable.
<?php
$var = 'variable';
if(isset($var)){
echo 'exists variable';
}else{
echo 'non-existent variable';
}
echo '<br />';
echo '<br />';
unset($var);
echo 'after using unset <br />';
echo '<br />';
echo '<br />';
if(isset($var)){
echo 'exists variable';
}else{
echo 'non-existent variable';
}
?>
empty
This function returns true if the value does not exist and false if it exists.
<?php
$var = ''; // or $var = null; or $var = false
if(empty($var)){
echo "empty";
}else{
echo "not empty";
}
?>
is_array
Checks whether a variable type is an array. Returns true if it is an array, false if it is not an array.
<?php
$arr = array();
if(is_array($arr)){
echo "this is array";
}else{
echo "not array";
}
?>
is_double
Returns true if the variable type is double(decimal) and false otherwise.
<?php
$var = 23234.343;
if(is_double($var)){
echo 'Decimal.';
}else{
echo "not";
}
?>
is_bool
Returns true if the variable type is boolean(logical speaker), false otherwise.
<?php
$var = true;
if(is_bool($var)){
echo 'This is a boolean.';
}
echo '<br />';
$var = false;
if(is_bool($var)){
echo 'This is a boolean.';
}
?>
is_integer
Returns true if the variable type is integer(integer) and false otherwise.
<?php
$var = 1212;
if(is_integer($var)){
echo 'integer type.';
}
?>
is_null
Check if the variable is empty.
<?php
$var = null;
if(is_null($var)){
echo 'The variable has no value.';
}
?>
is_numeric
Check if the variable is empty.
<?php
$var = 2323.34;
if(is_numeric($var)){
echo 'The value of the variable is a number.';
}
?>
is_object
Check if the variable is empty.
<?php
class Hello
{
public function helloOutput(){
echo 'hello world';
}
}
$hello = new Hello;
if(is_object($hello)){
echo 'This is an object.';
}else{
echo 'Not an object';
}
?>
is_string
Determine if a variable is a string
<?php
$var = 'I am a string';
if(is_string($var)){
echo 'This is a string.';
}else{
echo 'Not a string';
}
?>