Jump to content

[SOLVED] Simple Constant Problem?


ctroyp

Recommended Posts

I have a constant that I am passing into an if statement.  Unfortunately, it is not evaluating correctly. 

 

// Constant
define('_GROUP1', '$group == "Silver Member" || $group == "Gold Member"');

 

...

 

$group = "Silver Member";

if( _GROUP1 ){
echo "Success";
}

 

"Success" does not print.

 

Thanks for any help...I know this is trivial.

Link to comment
Share on other sites

I have a constant that I am passing into an if statement.  Unfortunately, it is not evaluating correctly. 

 

// Constant
define('_GROUP1', '$group == "Silver Member" || $group == "Gold Member"');

 

...

 

$group = "Silver Member";

if( _GROUP1 ){
echo "Success";
}

 

"Success" does not print.

 

Thanks for any help...I know this is trivial.

Can you even use constants that way? I didn't think you could.

Link to comment
Share on other sites

I have a constant that I am passing into an if statement.  Unfortunately, it is not evaluating correctly. 

// Constant
define('_GROUP1', '$group == "Silver Member" || $group == "Gold Member"');

 

You can't define a constant that way, secondly its very poor strucutre thirdly you are trying to call a constant by variable value.

why not just say

<?php
if($group == "Silver Member"){
//Do this
}
elseif($group == "Gold Member"){
//do that
}
?>

Link to comment
Share on other sites

I have a constant that I am passing into an if statement.  Unfortunately, it is not evaluating correctly. 

// Constant
define('_GROUP1', '$group == "Silver Member" || $group == "Gold Member"');

 

You can't define a constant that way, secondly its very poor strucutre thirdly you are trying to call a constant by variable value.

why not just say

<?php
if($group == "Silver Member"){
//Do this
}
elseif($group == "Gold Member"){
//do that
}
?>

You can define the constant that way.

 

However, I agree - you really can't use it like that.

Link to comment
Share on other sites

you can't because you have no argument for the Or statement it shouldn't randomly pick  on its own

Its not going to parse the statement, so whatever is in it will be completely irrelevant, but the constant itself would be quite valid, which is the point I'm trying to make.

 

But yeah, the if ( _GROUP1 ){ wouldn't work at all.

Link to comment
Share on other sites

You can do this

<?php
define('_GROUP1', $group == "Silver Member" ? "Silver Member" : "Gold Member");
?>

In which case $group would need to be declared and defined prior to the define.

 

Oh I assumed he was seeding $group via database or cookie. Either way that makes for some odd coding.

 

Link to comment
Share on other sites

My constant is defined in a single file and will be accessed from multiple files. 

 

Here is what I am ultimately trying to do...  I need to be able to define a number of items to a variable with global access.  And in any given file, I want to see if the logged user's group is in the list (a large list) of items in that global variable.  So, do I need to use an array or is there a better way to achieve this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.