In PHP programming language which is one of the back end or server-side programming languages, functions are used at every step to ensure that the code is compiled and executed successfully.
A function in PHP is an already defined or compiled piece of code that does or performs a specific action when it is called in a block of code.
There are two types of functions that are supported in the PHP programming language, that is in-built functions and user-defined functions.
Please note that functions in PHP must have () after the name of the function, the syntax for a PHP function is function_name()
In-built functions in PHP
In-built PHP functions are functions that are provided by PHP language that when called, execute depending on the purpose of the defined function. Some examples of inbuilt functions in PHP language are as follows;
- strlen() is used to determine the length of a string. View how you can calculate the length of a string in PHP
- str_replace() which is used to remove white spaces from a string. Learn how str_replace() function is used to remove white spaces from a string
- move_uploaded_file() function that is used to upload a file to a server. Learn how you can upload multiple files using PHP and jQuery
The number of in-built functions is not clearly defined but it is estimated that they are more than 1000 in-built functions that are supported by PHP.
User-defined functions in PHP
User-defined functions in PHP are those functions that a developer decides to create so that they can be reused across different files in the same program or application
In most cases, if you want to keep your code simple and clean, it is recommended that you create functions and store them in different files such that when you need to use the code, you will not have to re-write the code again rather you will call the function name and pass parameters to it and then it will return the result
User-defined functions in PHP are also used when you do not keep repeating the same piece of code over and over again
The structure of a user-defined function in PHP is as follows;
<?php
// user defined function
function test_user_defined_function() {
echo "This is an example of a user defined function!";
}
// calling the function
test_user_defined_function();
?>
When you execute the above program, the text defined in the function will be executed
An Example of a user defined function which passes a variable
<?php
// user defined function function
test_user_defined_function($name) {
echo "This is an example of a user defined function written by ".$name;
}
// calling the function
$name="PHP DEVELOPER";
test_user_defined_function($name);
?>
When you execute the above piece of code, the variable name will be added to the text that will be printed on the screen as shown below
This is an example of a user defined function written by PHP DEVELOPER
isset, empty and null functions in PHP
Having understood what functions are in PHP, how they work and the different types of functions that are supported by PHP language, it’s now time we look at the above functions.
All isset, empty and null functions are inbuilt functions which do specific purposes when called in PHP codes.
PHP isset() function
The isset() function in PHP is used to check whether a variable exists meaning that the variable must be defined or declared.
The isset() function works together with is not NULL, that is for isset() to return true, the variable must be defined or declared and must not be NULL else the result will be false
Note that, a variable is unset using the unset() function which is also an inbuilt function in PHP.
Mostly, isset() function is used when submitting data like in forms so as to ensure that the PHP code will not produce an error when a certain variable is called yet it is not defined. Also, it ensures that a PHP code will not execute unless certain variables are defined
The syntax for isset() function is
isset(variable);
For example, the below code will return false or No since the variable $name is not defined or declared
<?php
if(isset($name)){
echo "Yes";
}
else{
echo "No";
}
?>
For this example, it will return true or Yes as the variable $name is despite it being empty. Note that empty and null is not the same as we will discuss below
<?php
$name="";
if(isset($name)){
echo "Yes";
}
else{
echo "No";
}
?>
Using isset() function with POST variables is as shown below
<?php
if(isset($_POST["name"])){
}
?>
This is mostly used when you are submitting form data using the POST method for processing by PHP
PHP empty() function
The empty() function in PHP checks whether a declared variable has data or not. For the empty() function to work without errors it must be declared first meaning before using the empty() function you need to use the isset() function to check whether it is declared
If the variable declared has no data in it, the empty() function will return true else it will return false.
The syntax for empty() function is empty(variable)
The example below will return false since the defined variable does not have any data in it
<?php
$name="";
if(empty($name)){
echo "Yes";
}
else{
echo "No";
}
?>
If the variable has either 1 or more characters defined, the empty() function will return false since there will be values that are part of the variable
<?php
$name="John";
if(empty($name)){
echo "Yes";
}
else{
echo "No";
}
?>
PHP null function
The null function in PHP is used to check whether a variable defined is null or not. NULL is a special type of data type that can only contain only one value which is NULL
The syntax for null function is written as is_null(variable)
An example to show how is_null() function is used is as below
<?php
$name=NULL;
if(is_null($name)){
echo "Yes";
}
else{
echo "No";
}
?>
Please note the difference between
$name=NULL and $name="NULL"
The one without quotes will return true but the other one will return false
That’s it for this article, how you have learned the differences between isset(), empty(), and is_null() functions in PHP, different types of functions in PHP, and what are functions in PHP. Follow us for more articles and happy coding