1

I'm new to php.
I'm having a weird scenario where I cannot declare the following inside a function:

const CONST_Example = 1/192;

I'v tried also:

const CONST_Example  = 1;    

Do not work either. I want to be able to save constant float number which is a result of arithmetic expression such as 1/190, etc.

4
  • 1
    U should declare it as a class constant and not inside a class method. As naming convention, constants are full camelcase named Commented Jul 31, 2014 at 13:30
  • Why is this weird? The documentation says that constants can only be declared at the class level, not in functions. Commented Jul 31, 2014 at 13:31
  • 1
    Try using define if you want to set a named constant at runtime equal to the result of some operation: php.net/manual/en/function.define.php Commented Jul 31, 2014 at 13:33
  • 1
    See php.net/manual/en/language.oop5.constants.php for the documentation on class constants. Commented Jul 31, 2014 at 13:34

3 Answers 3

1

It is indeed impossible to create that value as a class constant in PHP. You can't declare it like this:

class Foo {

    const CONST_Example = 1/192;

}

because PHP is not able to evaluate expressions while it parses class declarations.
You can also not add the constant to the class after the fact at runtime.

Your options are:

  1. Declare it as regular runtime defined constant:

    define('CONST_Example', 1/192);
    
  2. Use the next best float representation as a literal:

    class Foo {
    
        const CONST_Example = 0.005208333333333333;
    
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

Note that PHP 5.7 will allow basic evaluation of constants, but you'll have to wait a little while for that
Note constant expressions made it into PHP 5.6. However, this still won't help with the question of declaring a constant inside a function.
0

The first one won't work, but the second one should. Here's an example how you can have your class working with constants

<?php
class Foo {
    const CONST_EXAMPLE = 1;
}

The first one won't work, because of the following reason:

"The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call."

Source: http://php.net/manual/en/language.oop5.constants.php

3 Comments

He said in his question where he's declaring the constant: in a function. As you showed, they can only be declared at class level.
Ok what about defining const to 1/125 for example compiler doesn't like the operator "\"
That won't work. As I said it musn't be a result of a mathematical operatin, which this would be. :)
0

As opposed to defining constants using define(), constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops, if statements or try/catch blocks.

https://www.php.net/manual/en/language.constants.syntax.php

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

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.