PHP Constant inside IF [duplicate] - php

This question already has answers here:
PHP | define() vs. const
(9 answers)
Closed 9 years ago.
having problems with constants in PHP wondering if someone can explain:
this works
const _ROOT = 'd:/aphp/www';
echo "r="._ROOT;
as does this:
if (true)
define('_ROOT','d:/aphp/www');
echo "r="._ROOT;
but this gives the error: Parse error: syntax error, unexpected T_CONST
if (true)
const _ROOT = 'd:/aphp/www';
echo "r="._ROOT;
I am using php 5.3.2

That is because ..
The const keyword must be declared at the top-level scope
From the PHP Docs
Note: 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 or if statements.
Source

Related

Get a list of constants outside of PHP class [duplicate]

This question already has answers here:
Find or list all constants used in a PHP file
(2 answers)
Closed 3 years ago.
I'm trying to get a list of all the constants that are defined outside of a PHP class but only if they start with a specific prefix (see this tutorial) and saw the ReflectionClass::getConstants functionality but this requires a class.
How do I achieve the same functionality if the constants are defined outside of a class?
You can use the built-in function get_defined_constants. It will list all the constants even the ones coming from Core PHP and loaded extensions. You can pass true as the parameter to categorize them. To get just userland constants (both from const and from define()) use get_defined_constants(true)['user']
<?php
define('DEFINED_CONST', 'foo');
const myConst = 1;
print_r(get_defined_constants(true)['user']);
prints:
Array
(
[DEFINED_CONST] => foo
[myConst] => 1
)

PHP "EMPTY" constant name [duplicate]

This question already has answers here:
php - Why can't you define a constant named EMPTY
(4 answers)
Closed 4 years ago.
Could you please tell me why this code throws a parse error if the name of the constant is EMPTY, but if I change it to EMPTY2 or SUBSTR it does work.
define('EMPTY', '');
if (empty(EMPTY)) {
echo 'hello world';
}
Because, as stated in this Quora answer, PHP function names are case-insensitive, so EMPTY collides with the built-in function empty().
PHP manual mentions this in a small note right after Example #3 in the subsection about User-defined functions:
Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.

Define class constant by another class constant in php [duplicate]

This question already has answers here:
Can I use string concatenation to define a class CONST in PHP?
(5 answers)
Closed 9 years ago.
How do I define a class constant based on another constant in the same class?
class A{
const BASE_URL = 'http://example.org'
const API_URL = BASE_URL . '/api'; // < error
}
You can only initialize class constants with constant values. You pass
a statement which has to be evaluated at runtime, while the class
constants are defined at compile time which comes earlier.
So. this is not possible.
Check this incorrect bug report.

Trying to assign a constant a value in a php class results in unexpected T_variable [duplicate]

This question already has answers here:
Assigning const value to const error in php
(2 answers)
Closed 8 years ago.
What's wrong in my code here?
<?php
class someClass
{
const HOSTNAME = $_SERVER["SERVER_NAME"]; // p.s: this is line # 5
and basically when an object of this class is instantiated, I get this:
syntax error, unexpected T_VARIABLE in /var/www/html/..../someClass.php on line 5
What am I doing wrong here?
From the manual: (emphasis mine)
The value must be a constant expression, not (for example) a variable,
a property, a result of a mathematical operation, or a function call.

Can I access a PHP Class Constant using a variable for the constant name? [duplicate]

This question already has answers here:
Accessing a class constant using a simple variable which contains the name of the constant
(5 answers)
Closed 10 months ago.
When accessing a class constant I see that I can use a variable for the class name, e.g. $classname::CONST_VALUE.
What if I want to use a variable for the constant name, e.g. self::$constant. This does not seem to work. Is there a workaround?
$variable = $classname.'::'.$constant;
constant($variable);
See the docs: http://php.net/constant

Categories