Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a constant or a text string in PHP -- is eval() the way to go

Tags:

php

eval

I have a row in a mysql table whose column contains:

This is a test.

I have another row in the same mysql table whose same column contains:

K_IM_A_CONSTANT

Earlier on in the PHP script, this line of code exists:

define(K_IM_A_CONSTANT, 'This is a constant.');

How can I echo the contents of the column whereby, the returned value would either be "This is a test." or "This is a constant.", depending on the row selected?

Is eval() the way to do it? If so, how might the eval() syntax look? I have been getting too many errors trying to get eval() to work.

Thanks for helping.

like image 722
H. Ferrence Avatar asked Dec 28 '22 21:12

H. Ferrence


1 Answers

Use the constant function:

if(defined($row['column_name']))
{
    echo constant($row['column_name']);
}
like image 81
Tim Cooper Avatar answered Dec 30 '22 11:12

Tim Cooper