ctroyp Posted January 13, 2008 Share Posted January 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/ Share on other sites More sharing options...
twostars Posted January 13, 2008 Share Posted January 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437681 Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Share Posted January 13, 2008 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437683 Share on other sites More sharing options...
twostars Posted January 13, 2008 Share Posted January 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437684 Share on other sites More sharing options...
Daukan Posted January 13, 2008 Share Posted January 13, 2008 you cant really use a constant that but you can do this <?php if(defined('_GROUP1') ){ ?> edited: _GROUP1 needed to be wrapped in quotes Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437687 Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Share Posted January 13, 2008 you can't because you have no argument for the Or statement it shouldn't randomly pick on its own Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437690 Share on other sites More sharing options...
twostars Posted January 13, 2008 Share Posted January 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437692 Share on other sites More sharing options...
Daukan Posted January 13, 2008 Share Posted January 13, 2008 You can do this <?php define('_GROUP1', $group == "Silver Member" ? "Silver Member" : "Gold Member"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437695 Share on other sites More sharing options...
twostars Posted January 13, 2008 Share Posted January 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437696 Share on other sites More sharing options...
Daukan Posted January 13, 2008 Share Posted January 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437698 Share on other sites More sharing options...
ctroyp Posted January 13, 2008 Author Share Posted January 13, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437705 Share on other sites More sharing options...
Daukan Posted January 13, 2008 Share Posted January 13, 2008 An array would do it probably. <?php $user['group'] = 'group2'; $GLOBALS['groups'] = array('group1', 'group2', 'group3', 'group4'); if(in_array($user['group'], $GLOBALS['groups']) ) { echo 'You are in a group'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437715 Share on other sites More sharing options...
ctroyp Posted January 13, 2008 Author Share Posted January 13, 2008 I decided to go the array route similar to Daukan's post. Thanks to all for the help. Quote Link to comment https://forums.phpfreaks.com/topic/85757-solved-simple-constant-problem/#findComment-437744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.