Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constant already defined in php

Tags:

php

constants

I have a function that I am trying to run but it shows the message as CONSTANT already defined.

I tried to put a condition saying "if defined" about the function but still nothing. Is there any method to ignore this and see the output?

like image 444
JDesigns Avatar asked May 04 '11 17:05

JDesigns


People also ask

Is constant defined PHP?

A PHP constant is the opposite of a variable in that once it has been defined it cannot be changed. Constants are particularly useful for defining a value that you frequently need to refer to that does not ever change.

What are the constants in PHP?

PHP constants are name or identifier that can't be changed during the execution of the script except for magic constants, which are not really constants. PHP constants can be defined by 2 ways: Using define() function. Using const keyword.

Can we define constant array in PHP?

Yes, You can define an array as constant. From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.


2 Answers

Replace this:

define('constant', 'value'); 

with this:

if (!defined('constant')) define('constant', 'value'); 
like image 154
OZ_ Avatar answered Sep 20 '22 13:09

OZ_


define()

Example:

/* Note the use of quotes, this is important.  This example is checking  * if the string 'TEST' is the name of a constant named TEST */ if (defined('TEST')) {     echo TEST; } 
like image 38
Phill Pafford Avatar answered Sep 18 '22 13:09

Phill Pafford