Earl Higgins
Earl Higgins

Reputation: 129

PHP general constants

I understand that wanting to update or remove a constant is the exact opposite if what it is meant for, but here is my problem.

I want to be able to format a value in same same fashion that a constant does. i.e.: echo foo;

Where it can just be plain text, instead of echoing a variable like $foo.

It may seem like a silly thing to want to do, but I am hoping to create use out of it. However, if this is not possible, I guess that is that.

PS. I 'would' just define it as a constant, however I want it to be able to update (like re-defining it during a foreach).

Upvotes: 2

Views: 97

Answers (3)

chaoskreator
chaoskreator

Reputation: 914

According to http://www.php.net/manual/en/function.define.php#92327, you can redefine constants on the fly if necessary.

define("FIRST_CONSTANT", 'my 1st value', true);
echo FIRST_CONSTANT;
// my 1st value

define('FIRST_CONSTANT', 'my 2nd value');
echo FIRST_CONSTANT;
// my 2nd value

Upvotes: 1

Jayy
Jayy

Reputation: 14728

You could use the name of a function like this:

<?php 
function hello(){ return "bla"; }
$a = hello;
$b = $a();
echo $b;
?>

Upvotes: 0

Ry-
Ry-

Reputation: 224857

Sort of. The first one must be case-insensitive:

define("myConstant", "blah", true);
print myConstant; // blah
define("myConstant", "xxxx"); // No warning outputted
print myConstant; // xxxx

But, DON'T DO THIS! The whole point of a constant is that it's constant. Although you seem to sort of recognize this, what is wrong with that extra $ sign, really, versus a much more clear code style?

Upvotes: 3

Related Questions