Common Functions in CodeIgniter
0 1869
- Codeigniter provides some functions that are used globally defined.
- We can use these functions anywhere throughout the application.
- Unlike libraries and helper functions, these functions do not require initialization before use.
Related Topics:
Codeigniter Interview Questions
Cookie Management in Codeigniter
Models in Codeigniter
CodeIgniter Common Functions:
Some common functions are listed below:
1 is_php(): This function is used to get the information about PHP versions. This function returns boolean TRUE if the installed version of PHP is equal to or greater than the supplied version number and returns FALSE otherwise.
Syntax:
is_php($version);
Parameter description:
- $version (string) − Version number
The return type of this function void and this function returns TRUE if the running PHP version is at least the one specified or FALSE if not.
2 is_really_writable(): This function determines if a file is actually writable or not.
Syntax:
is_really_writable();
Parameter description:
The return type of this function bool and this function returns TRUE if the path is writable, FALSE if not.
3 config_item(): This function is used to access configuration information.
Syntax:
config_item($key);
Parameter description:
The return type of this function mixed and this function returns Configuration key-value or NULL if not found.
4 set_status_header(): This function is used to set a server status header manually.
Syntax:
set_status_header($code[, $text = '']);
Parameter description:
The return type of this function void.
5 remove_invisible_characters(): This function prevents inserting NULL characters between ASCII characters, like Java\0script.
Syntax:
remove_invisible_characters($str[, $url_encoded = TRUE])
Parameter description:
The return type of this function string and this function returns sanitized string.
6 html_escape(): This function provides the functionality of PHP native function htmlspecialchars().
Syntax:
html_escape($var);
Parameter description:
The return type of this function mixed and this function returns HTML escaped string(s).
7 get_mimes(): This function returns a reference to the MIMEs (Multipurpose Internet Mail Extensions) array from application/config/mimes.php.
Syntax:
get_mimes();
The return type of this function array and this function returns an associative array of file types.
8 is_https(): This function returns TRUE if a secure (HTTPS) connection is used and FALSE in any other case (including non-HTTP requests).
Syntax:
is_https();
The return type of this function bool and this function returns TRUE if currently using HTTP-over-SSL, FALSE if not.
9 is_cli(): This function is used to determine the application is run through the command line or not.
Syntax:
is_cli();
The return type of this function bool and this function returns TRUE if currently running under CLI, FALSE otherwise.
10 function_usable(): This function is used to check a function exists and is usable or not.
Syntax:
function_usable($function_name);
Parameter description:
The return type of this function bool and this function returns TRUE if the function can be used, FALSE if not.
Example:
Step 1 Open the application/controllers directory and create a new controller file Common_controller.php.
<?php
class Common_controller extends CI_Controller {
public function index() {
set_status_header(200);
echo is_php('5.3')."<br>";
var_dump(is_really_writable('./Form.php'));
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>";
var_dump(function_usable('test'))."<br>";
echo "get_mimes():".print_r(get_mimes())."<br>";
}
public function test() {
echo "Test function";
}
}
?>
Step 2 Open the given URL into the browser to see the result.
http://localhost/ci/index.php/Common_controller
Share:
Comments
Waiting for your comments