Table of Contents
Inside this article we will see the concept to define global constant in codeigniter 4. Global constant simply means variables which we can access anywhere inside application at any point. Value for constants is fixed for lifetime. We can’t alter values for those.
List of all available global constants in CodeIgniter 4, Click here for article.
Here we will discuss to create global constant step by step and how to use in codeigniter 4 application.
Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.
Let’s get started.
Download & Install CodeIgniter 4 Setup
We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.
Here are the following ways to download and install CodeIgniter 4 –
- Manual Download
- Composer Installation
- Clone Github repository of CodeIgniter 4
Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.
Here is the command to install via composer –
composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
Turn Development Mode On
When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env
Open project in terminal
cp env .env
Above command will create a copy of env file to .env file. Now we are ready to use environment variables.
CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.
# CI_ENVIRONMENT = production // Do it to CI_ENVIRONMENT = development
Now application is in development mode.
What is a Global Constant?
Global constants are also termed as core constants of application. It simply means theses –
- Variables which can accessed through out application means globally available to use.
- Values of constants can’t be altered upto it’s lifetime.
- We can define constant using PHP function define()
Define Global Constant in Application
Global constants can be define in two ways inside Codeigniter 4 application. These are the following ways –
- By using .env file
- By using Constants.php file
Global Constant using .env (environment) file
Open .env file from application root. To define global constants, simply define like this
... MY_CONSTANT="This is a global constant defined in .env file" ...
How To Use?
To call inside application files, use getenv() function of codeigniter 4.
<?php echo getenv('MY_CONSTANT') ?> OR <?= getenv('MY_CONSTANT') ?>
Global Constant Using Constants.php File
Open Constants.php file from /app/Config folder. Here, to define global constants we use php function define()
Add this line at footer of file –
... defined('MY_CONSTANT') || define('MY_CONSTANT', 'This is a global constant defined in Constants.php file'); ...
defined(‘MY_CONSTANT’) It is checking first that constant is already exists or not. If not then using define() it is creating.
How To Use?
To call inside application files, use like this.
<?php echo MY_CONSTANT; ?> OR <?= MY_CONSTANT ?>
We hope this article helped you to learn about How To Define Global Constant in CodeIgniter 4 Tutorial in a very detailed way.
Find More on CodeIgniter 4 here
- CodeIgniter 4 Cookie Helper Tutorial
- CodeIgniter 4 CRUD Application Tutorial
- CodeIgniter 4 CRUD REST APIs Tutorial
- CodeIgniter 4 CSRF Token in AJAX Request
- Database Query in CodeIgniter 4 Tutorial
- CodeIgniter 4 Ajax Form Data Submit
- CodeIgniter 4 Form Validation Tutorial
- CodeIgniter 4 Image Upload with Form Tutorial
- Multi language in CodeIgniter 4 Tutorial
- Stripe Payment Gateway Integration in CodeIgniter 4
- CodeIgniter 4 CSRF Token Tutorial
- CodeIgniter 4 Basics Tutorial
- CodeIgniter 4 Spark CLI Commands Tutorial
- Migration in CodeIgniter 4 Tutorial
- Seeders in CodeIgniter 4 Tutorial
Hi, I am Sanjay the founder of Online Web Tutor. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.