CodeIgniter – Common Functions

CodeIgniter – Common Functions

CodeIgniter library functions and helper functions need to be initialized before they are used but there are some common functions, which do not need to be initialized.

These common functions and their descriptions are given below.

Parameters$version (string) − Version number
ReturnTRUE if the running PHP version is at least the one specified or FALSE if not
Return Typevoid
DescriptionDetermines if the PHP version being used is greater than the supplied version number.


Parameters$file (string) − File path
ReturnTRUE if the path is writable, FALSE if not
Return Typebool
Descriptionchecks to see if file is writable or not.


Parameters$key (string) − Config item key
ReturnConfiguration key value or NULL if not found
Return Typemixed
DescriptionThis function is used to get the configuration item


Syntaxset_status_header($code[, $text = ”])
Parameters$code (int) − HTTP Response status code

$text (string) − A custom message to set with the status code

Return Typevoid
DescriptionThis function permits you to manually set a server status header.


Syntaxremove_invisible_characters($str[, $url_encoded = TRUE])
Parameters$str (string) − Input string

$url_encoded (bool) − Whether to remove URLencoded characters as well

ReturnSanitized string
Return Typestring
DescriptionThis function prevents inserting NULL characters between ASCII characters


Parameters$var (mixed) − Variable to escape (string or array)
ReturnHTML escaped string(s)
Return Typemixed
DescriptionThis function acts as a native PHP htmlspecialchars() function.


ReturnAn associative array of file types
Return Typearray
DescriptionThis function returns a reference to the MIMEs array fromapplication/config/mimes.php.


ReturnTRUE if currently using HTTP-over-SSL, FALSE if not
Return Typebool
DescriptionReturns TRUE if a secure (HTTPS) connection is used and FALSE in any other case (including non-HTTP requests).


ReturnTRUE if currently running under CLI, FALSE otherwise
Return Typebool
DescriptionReturns TRUE if the application is run through the command line and FALSE if not.


Parameters$function_name (string) − Function name
Return Typebool
DescriptionReturns TRUE if a function exists and is usable, FALSE otherwise.


Given below is an example, which demonstrates all of the above functions.


Here we have created only one controller in which we will use the above functions. Copy the below given code and save it atapplication/controller/CommonFun_Controller.php.

   class CommonFun_Controller extends CI_Controller { 
      public function index() {
         echo is_php('5.3')."<br>"; 
         echo config_item('language')."<br>"; 
         echo remove_invisible_characters('This is a ‌test','UTF8')."<br>"; 
         $str = '< This > is ' a " test & string'; 
         echo html_escape($str)."<br>"; 
         echo "is_https():".var_dump(is_https())."<br>"; 
         echo "is_cli():".var_dump(is_cli())."<br>"; 
         echo "get_mimes():".print_r(get_mimes())."<br>"; 
      public function test() { 
         echo "Test function"; 

Change the routes.php file at application/config/routes.php to add route for the above controller and add the following line at the end of the file.

$route['commonfunctions'] = 'CommonFun_Controller';

Type the following URL in the address bar of your browser to execute the example.

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.