CodeIgniter Common Functions

CodeIgniter provides a set of common functions that are globally available across your application.

These functions make coding faster, easier, and more consistent since you don’t need to re-write basic PHP logic.

Examples include:

  • Checking if a file exists
  • Debugging variables
  • Generating random strings
  • Reading config values

They are defined inside:

system/core/Common.php

And are always loaded automatically by CodeIgniter.

🔹 Commonly Used Functions in CodeIgniter

1. show_error()

Displays a custom error message.

show_error("Something went wrong!", 500, "Error Title");

2. show_404()

Shows a 404 page when a resource is not found.

show_404();

3. log_message()

Used for logging errors, debugging, or info messages.

log_message('error', 'An error occurred');
log_message('debug', 'Debugging info here');
log_message('info', 'Just some info');

Logs are stored in:

application/logs/

4. get_mimes()

Returns the MIME types defined in application/config/mimes.php.

print_r(get_mimes());

5. config_item()

Retrieves a value from the configuration file.

$site_name = config_item('base_url');
echo $site_name;

6. is_php()

Checks the running PHP version.

if (is_php('7.4')) {
    echo "Running PHP 7.4 or higher!";
}

7. is_loaded()

Checks if a certain class is already loaded.

print_r(is_loaded());

8. set_status_header()

Sets the HTTP status code in response.

set_status_header(200); // OK
set_status_header(403); // Forbidden


🔹 Example: Using Common Functions

class CommonDemo extends CI_Controller {

    public function index()
    {
        // Logging
        log_message('info', 'User visited CommonDemo controller.');

        // Error example
        if (!file_exists('somefile.txt')) {
            show_error('The requested file was not found.', 404);
        }

        // Config value
        echo "Base URL is: " . config_item('base_url');
    }
}



Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.