Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string to a defined constant in PHP

Tags:

Let's say I have defined the constant ERROR_0 as follows:

define("ERROR_0","An error occurred.") 

Now, let's say I have the string "ERROR_0" but I want to convert that into the constant ERROR_0 such that I can get the string "An Error occurred." How can I do that?

Thanks!

like image 559
Deets McGeets Avatar asked Sep 29 '11 05:09

Deets McGeets


People also ask

How do you define a constant in PHP?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

How do I convert a string to PHP?

Answer: Use the strval() Function You can simply use type casting or the strval() function to convert an integer to a string in PHP.

What is the purpose of constant () function in PHP?

The constant() function returns the value of a constant. Note: This function also works with class constants.

Can you redefine a constant in PHP?

No, you cannot redefine a constant (except with runkit_constant_redefine), that's why is called CONSTANT.


1 Answers

Use constant function

echo constant("ERROR_0"); 
like image 90
Andris Avatar answered Sep 21 '22 15:09

Andris