Quoting Constants in PHP: "This Is a My_Constant"

quoting constants in php: this is a MY_CONSTANT

Sorry, that's not the way constants in PHP work. You can put variables in double quotes and heredocs but not constants.

Constants inside quotes are not printed?

Because "constants inside quotes are not printed". The correct form is:

echo "This is a constant: " . CONSTANT;

The dot is the concatenation operator.

Include constant in string without concatenating

No.

With Strings, there is no way for PHP to tell string data apart from constant identifiers. This goes for any of the string formats in PHP, including heredoc.

constant() is an alternative way to get hold of a constant, but a function call can't be put into a string without concatenation either.

Manual on constants in PHP

How to use a class constant inside a double quoted string?

You can't.

However, you can:

$foo = myClass::myConstant;
echo "something $foo something else";

echo "something " . myClass::myConstant . " something else";

printf('something %s something else', myClass::myConstant);

unable to get defined constant from app.php in cakephp3

You should use CakePHP's own class for reading and writing to the app.php configuration.

If you want to read a value from the config, you simply need to call Configure::read($key = null).

Used to read configuration data from the application. Defaults to
CakePHP’s important debug value. If a key is supplied, the data is
returned. Using our examples from write() above, we can read that data
back:

Configure::read('Company.name');    // Yields: 'Pizza, Inc.'
Configure::read('Company.slogan'); // Yields: 'Pizza for your body
// and soul'

Configure::read('Company');

// Yields:
['name' => 'Pizza, Inc.', 'slogan' => 'Pizza for your body and soul'];

For writing to the config file you would need to call Configure::write($key, $value).

Use write() to store data in the application’s configuration:

Configure::write('Company.name','Pizza, Inc.');
Configure::write('Company.slogan','Pizza for your body and soul');

More Information

  • Configuration
  • Reading Configuration Data
  • Writing Configuration data

Edit

To fix your error: When calling an constant in PHP, you do not enclose it with ' quotes, that's what's causing the error. See this answer.

Edit

All of the changes made to the config are stored at runtime. To store changes please look into Storing Runtime Configuration and Creating or Modifying Configuration Files.

PHP Defined Constant and mktime() Do Not Agree

I'm fairly certain this is a timezone issue. If you do the two statements right next to each other it works:

define('SC_TIME_END_OF_64', mktime(23, 59, 59, 12, 31, 9999));
echo SC_TIME_END_OF_64 . ' ' . mktime(23, 59, 59, 12, 31, 9999);
//253402329599 253402329599

http://sandbox.onlinephpfunctions.com/code/73e441ef17890152380b6c875157596e0287a1b6

But if you change the timezone at some point in between you get different results:

define('SC_TIME_END_OF_64', mktime(23, 59, 59, 12, 31, 9999));
date_default_timezone_set('Europe/Amsterdam');
echo SC_TIME_END_OF_64 . ' ' . mktime(23, 59, 59, 12, 31, 9999);
//253402329599 253402297199

http://sandbox.onlinephpfunctions.com/code/795bede12ac82b69b360bf8cae9bcd9eebc2ebf4

Either standardize on a timezone (ideally UTC), or use gmmktime() instead.



Related Topics



Leave a reply



Submit