Wael Assaf Wael Assaf - 4 months ago 5
PHP Question

Constant function in PHP

Can anyone tell me what's wrong here? I defined a constant named 'VALUE' and its value is 'STAR', then I created a variable

$var = 10;
and I used constant function in this line:

$data = constant ('VALUE'. $var);


and I decided to echo the program

echo VALUE10;


and I encountered this errors :

enter image description here

The code :

<?php
define ('VALUE' , 'STAR');
$var = 10;
$data = constant ('VALUE'. $var);
echo VALUE10;
?>

Answer

The constant function is used for getting the value of a defined constant. To define a constant use the define function.

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

constant() is unnecessary, all you have to write is the constants name:

define("TEST_CONSTANT", 24, false);
echo TEST_CONSTANT;

If you want to check if a constant has been defined use define()

if(defined("TEST_CONSTANT")) {
    // Do stuff
}

Hope this is the answer you were looking for.