rockstardev
rockstardev

Reputation: 13537

PHP: Can't assign a constant in a function?

I have a function called getSources(); In this function I want to easily assign numbers to constants. I figured this would work:

const A = 1;
const B = 2;
const C = 3;
const D = 4;

And I could just do this:

$someValue = A;

But it doesn't work. What am I missing? I don't want these variables to be used outside of the scope of this function.

Upvotes: 0

Views: 103

Answers (4)

Mohit Verma
Mohit Verma

Reputation: 79

you can do like this

function getSources()
{
    define(A,1);
    define(B,2);
    ....
 }

like this you can solve it

Upvotes: 0

Dino Babu
Dino Babu

Reputation: 5809

try

define('myname', 'myvalue');
echo myname;

// Output

myvalue

Upvotes: 1

alex
alex

Reputation: 490413

You need to use the scope resolution operator (::) to access them (if they're setup as const for a class).

Otherwise, you need to use define() which makes the identifiers global.

Upvotes: 0

sectus
sectus

Reputation: 15464

Use define instead.

define('A', 1);

Upvotes: 3

Related Questions