Hey everybody I want to let you know that I have undertaken the grueling task of getting the heck away from WordPress. I was so sick of the problems and updates I had to do all the time. I am now using my ezbloo system and I am integrating all my old posts into the new system. It sucks, but in the end, I will save bundles of time. I needed to keep things simple and that is why I created ezbloo. I'll have more on this later for you guys after I am done with the total integration of my old posts here. So if you are looking for a post and need it faster, shoot me an email and I will make it a priority. [email protected]

PHP 7 Constants are like variables. The only difference is that they cannot be changed. Once you set a "Constant" you cannot change it. PHP Constants need to hold unique information. Here are the files from the last lesson.

PHP 7 Constants Basic Example

In this Php 7 Constants example, we are setting a constant called "WELCOME". After setting the constant, we then echo it out onto the page. Create another file in your website project called constants.php. Insert the following code between the header and footer includes.
<?php/* A CONSTANT WORKS LIKE A VARIABLE BUT CANNOT BE CHANGED ONCE IT IS SET */ 
define("WELCOME", "Welcome to Php7, WE LIKE LEARNING PHP7"); echo WELCOME; ?>
  It should look like this. constants in php 7

Notice: Constant already defined

If you get, "Notice: Constant already defined" then you are calling in the constant somewhere else on your script. It will look something like this. notice: constant already defined

Case Sensitive Constants in Php7

If you add the parameter of "true" at the end of a constant then it will make it "case sensitive". Consider this example. We are using the constant of "ROCKS" and "rocks". Normally we would not be able to do this but since we used "true" in the second constant we made it case sensitive and can use it.
<?php /* WHEN WE USE THE PARAMETER OF TRUE WE ESTABLISH THAT THE NAME IS CASE SENSITIVE. BY DEFAULT DEFINE USES LOWER CASE */ 
define("ROCKS", "Php7 ROCKS"); 
echo ROCKS; 
define("rocks", "Php7 ROCKS, YEAH", true); 
echo rocks; ?>
  It will look like this, notice both constants are being called and there are no errors. case sensitive constants

PHP 7 Constants Are Global

PHP 7 constants are global, meaning that you can use them site wide. Notice below when we use the function. Inside the function we are just using a constant. The function will then echo out the sentence we coded in the constant.
/*
CONSTANTS ARE GLOBAL WHICH MEANS THAT YOU CAN USE THEM THROUGHOUT THE ENTIRE WEBSITE. THE WEBSITES HAVE TO BE TIED TOGETHER WITH "INCLUDES(FILE.PHP)" FOR GLOBALS LIKE THIS TO WORK.

IN THE FOLLOWING EXAMPLE NOTICE THAT WE ARE JUST ECHOING OUT THE CONSTANT IN THE FUNCTION. THE FUNCTION WILL THEN PRINT OUT THE STATMENT DEFINED IN THE CONSTANT
*/

define("PHP7", "I AM A PHP 7 WIZARD");

function wizard() {
echo PHP7;
}
wizard();
  It would look like this. php7 constants are global

Video For PHP Constants Lesson


If you've worked with PHP for any length of time, you may have come across the "Notice: Constant already defined" error message. This error can be confusing, especially if you're not familiar with constants in PHP. In this blog post, we'll take a closer look at what constants are, how to define them, and what causes the "Constant already defined" error.

What are constants in PHP?

In PHP, constants are variables that cannot be changed once they are defined. They are similar to variables, but they have a fixed value that remains the same throughout the script. Constants are typically used for values that should never change, such as mathematical constants like pi or values that are used throughout an application, like database connection details.

Defining constants in PHP

To define a constant in PHP, you use the define() function. The syntax for defining a constant is as follows:
define(name, value, case-insensitive);
The first parameter is the name of the constant, the second parameter is its value, and the third parameter is optional and specifies whether the constant should be case-insensitive. For example, to define a constant named "MY_CONST" with a value of 10, you would use the following code:
define('MY_CONST', 10);
You can also define constants using the const keyword, but this method only works in PHP 5.3 or later. The syntax for defining a constant using const is as follows:
const name = value;
For example:
const MY_CONST = 10;

Why do you get the "Constant already defined" error?

The "Constant already defined" error occurs when you try to define a constant that has already been defined. This can happen for several reasons, including:
  1. Defining the constant twice in the same script: If you define a constant twice in the same script, you'll get the "Constant already defined" error. To avoid this error, you should define each constant only once.
  2. Defining the constant in a different case: If you define a constant with a different case than it was defined previously, you'll get the "Constant already defined" error. For example, if you define a constant named "MY_CONST" and then try to define a constant named "my_const", you'll get the error.
  3. Defining the constant in a different file: If you define a constant in one file and then try to define it again in a different file, you'll get the "Constant already defined" error. To avoid this error, you should define all constants in a single file and include that file in any scripts that need to use the constants.

How to fix the "Constant already defined" error

To fix the "Constant already defined" error, you need to make sure that each constant is defined only once and that it is defined with the correct case. If you're defining constants in different files, you should define them in a single file and include that file in any scripts that need to use the constants. If you're still getting the error after making these changes, you can use the defined() function to check if a constant has already been defined. The syntax for the defined() function is as follows:
bool defined(string $name);
The function takes the name of the constant as its parameter and returns true if the constant has already been defined, and false otherwise. You can use this function to check if a constant has already been defined before defining it. For example:
if (!defined('MY_CONST')) {
    define('MY_CONST', 10);
}
This code checks if the constant named "MY_CONST" has already been defined. If it hasn't been defined, it defines the constant with a value of 10. This ensures that the constant is only defined once, even if the script is run multiple times.
In conclusion, constants are an important feature in PHP that allow you to define values that should never change. However, it's important to be careful when defining constants to avoid the "Constant already defined" error. By defining each constant only once, ensuring that it is defined with the correct case, and checking if it has already been defined before defining it, you can avoid this error and use constants in your PHP scripts with confidence.