0

I am working on a function to validate variables and constants.

function checkVars($var){
 if(isConstant($var)){ <--here is my problem, how do I check if its a Constant and or a string?
  return defined($var);
 }else{
  //use other functions to check if its a valid variable
 }
}

Is there a way to check if its a string or a constant?

EDIT: The idea behind this is to save lines of code on recurring tasks like:

if(defined('CONS')){
 if(CONS > 0 && CONS !== false){
  //and so on..
 }
}

This is just an example but you could have 4 lines of code just to validate a constant or a variable to fit specific application needs.

The idea is to save all that work with a single function call that returns true or false:

if(isValid('CONS')){
 //do stuff on true
}else{
 //do stuff on false
}
8
  • is_string() didnt work for you here? Commented Aug 27, 2020 at 13:30
  • 1
    I don't think you can check if a value is a constant. If a constant is passed in, it would be the value of the constant. Unless the value being passed in is the name of the constant itself. Commented Aug 27, 2020 at 13:33
  • 1
    Please provide an example of how you are calling this function, and what you expect it to return. Commented Aug 27, 2020 at 13:37
  • 2
    @aynber you can test to see if you have a constant with defined(name_of_constant) and you can check the value of the constant using constant(name_of_constant) Commented Aug 27, 2020 at 14:01
  • 1
    @JayBlanchard Thanks. Learned something new today :-D Commented Aug 27, 2020 at 14:07

3 Answers 3

2

You will use defined() to test to see if the constant exists and is defined. Then use is_string() with constant() to determine if the constant is a string. I assume you're using return because this condition is part of a function:

if(defined($constantName) and is_string(constant($constantName))) {
    return constant($constantName);
} else {
    // other code
}

Running the following test I could see that 'bar' was returned:

define("FOO", "bar");
$constantName = "FOO";

if(defined($constantName) and is_string(constant($constantName))) {
    echo constant($constantName); // 'bar'
} else {
    // other code
}
Sign up to request clarification or add additional context in comments.

Comments

2

check if a constant is defined by her name:

defined('CONSTANT');

check if any value is a string:

is_string(CONSTANT);

logically, if it's not a constant is just a string.

for check if is a constant you must pass the constant name in string. You also can check if exists a constant with the same value, using get_defined_constants(), but you will not know if is the same constant.

define('MYCONST', "THE VALUE");
function exists_a_constant($value)
{
    $constants = get_defined_constants(true);
    return in_array($value, $constants['user']); // true if finds or false if not
    // return array_search($value, $constants['user']);    //Will return the key (name of the constant)
}

function checkVars($var)
{
    if (exists_a_constant($var)) {
        echo "exists a constant";
    } else {
        echo "not";
    }
}

checkVars(MYCONST);
// exists a constant
checkVars('MYCONST');
// not
checkVars("THE VALUE");
// exists a constant
checkVars("random string");
// not

4 Comments

It returns TRUE even if the constant wasn't defined
i forgot to use only the user defined constants. updated the answer @CrashOverride is working now.
Still not working but I changed array_search for array_key_exists and works perfectly. Added it as an answer, thanks for your help
Strange. Works in here for checking a value. array_key_exists() will search the key. if you are receiving the key, you can check by defined('MYCONSTNAME').
-1

Based on André Walker answer I made a few changes and it works.

get_defined_constants gets an array with all the defined constants then I use array_key_exists to check if the constant you are passing is in the array.

define('FOO','bar');
function exists_a_constant($value){
    $constants = get_defined_constants();
    return array_key_exists($value,$constants); 
}

if(exists_a_constant('FOO')){
    echo 'defined';
}else{
    echo 'not defined';
}

thanks!

1 Comment

I guess your question was misleading/confusing. This has nothing to do with checking if a variable is a string, but rather you are only checking if that constant was defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.