Creating your own codeigniter helper

CodeIgniter helpers are groups of utility functions you may need anywhere in your code. CodeIgniter has more than 20 helpers. You may be familiar with the URL, email, form or file helpers. These have functions that simplify common tasks when working with URLs, emails and files.

A CodeIgniter helper is a PHP file with multiple methods. It is not a class

Create a file application/helpers/ new_helper.php and put the following code into it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('my_first_function'))
{
    function my_first_function($params)
    {
        //Your code here
    }
}

if (!function_exists('my_second_function'))
{
    function my_second_function($params)
    {
        //Your code here
    }
}

After saving your helper file, you can load it in the same way you load a native helper:

$this->load->helper(‘new’) ;

Using the Helper


1
echo my_first_function('Hello World');