Introduction to Functions in PHP
Same like all other programming languages PHP also have functions. Functions are some part of the code which is written separately and by calling them we use that coding gain and again.
In PHP there are two types of functions:
- User-defined function
- Built-in function
User-Defined Functions:
In PHP as there is no concept of data type so while declaring function we have not to tell its data type or return type.
The function keywords indicate that it’s a function so in PHP functions are created followed by function keyword and after this functions name and then its parameter list and then the body.
1 2 3 4 5 6 | <?php function add($a,$b){ return $a+$b; } echo add(32,44); ?> |
Built-In Functions:
There is a collection of built-in functions in PHP. These functions are pre-defined by default in PHP and as in other languages we have to import or include libraries to use some function but in PHP all functions are available in all parts of the virtual directory, but they have some conditions like for using SQL function you must have an active database connection.
We already covered almost all important built-in function of MYSQLi and SQL.
Some other important functions are listed below with example:
include()
This function is used to include a file in another file. It’s same like in other languages we import some other files or libraries.
index.php
1 2 3 | <?php include "head.php"; ?> |
head.php
1 2 3 | <?php echo '<h1>this is head file</h1>'; ?> |
include_once()
This is same as include but if we include the same file two times include_once function ignores the duplicated file.
1 2 3 | <?php include_once "head.php"; ?> |
echo
This is used to print output. It has no return values. It can take multiple parameters as shown in the example.
1 2 3 4 5 | <?php $id = 123; $name = "usman siddique" echo '<p>My ID = '.$id.' and My name is = '.$name; ?> |
It is also used to show output the main difference between echo and print is it has a return value, and it can’t take multiple parameters.
1 2 3 | <?php print("I live in Islamabad"); ?> |
header
This function is used to redirect the execution to some other file. This function takes the location of the targeted file.
1 2 3 | <?php header ('location:home.php'); ?> |
isset()
This function is used to check the required property is set or not. It takes one single parameter.
1 2 3 4 5 6 | <?php if(isset($_POST['submit'])) { } ?> |
empty()
It is used to check either the variable is empty or not.
1 2 3 4 5 6 | <?php if(!empty($_POST['submit'])) { } ?> |
is_null()
This function checks the variable in parameter is null or not.
1 2 3 4 5 6 | <?php if(!is_null($_POST['submit'])) { } ?> |
mail()
This function is used to send a mail in PHP.
Syntax
mail (receiver, subject, your message);
Example
1 2 3 4 | <?php $msg = "This is a test mail"; mail("m7u786@gmail.com","Test mail",$msg); ?> |
wordwarp()
This function is used to wrap the text line in PHP. It will cut or break the line into new line when some specifically given length is reached.
1 2 3 4 | <?php $string = "This line is too big and we want to brake that line after 20 characters."; echo wordwrap($string,20); ?> |
explode()
This function breaks a PHP string into a string array.
1 2 3 4 | <?php $name = "hafiz muhammad usman siddique."; echo (explode(",",$name)); ?> |
htmlspecialchars()
This function converts HTML tags special characters to their XML code. It will convert the predefined characters which are used as start and end of HTML tags “<” (less than) and “>” (greater than) to HTML entities.
The predefined characters are:
- & (ampersand) becomes &
- ” (double quote) becomes "
- ‘ (single quote) becomes '
- < (less than) becomes <
- > (greater than) becomes >
For more PHP pre-defined function