How to check if a defined constant exists in PHP?
First, these are not variables, but constants.
And you can check their existence by using the defined()
function :
bool defined ( string $name )
Checks whether the given constant exists and is defined.
Check if a constant is already defined
CONST = 2 unless defined? CONST
See here for more about awesome defined?
operator.
P.S. And in the future I guess you'll want var ||= 1
instead of var = var||1
.
Right way to check if a constant is already defined in a Ruby Class
The keyword defined?
is documented here.
It is better to ask if it is a constant, and use const_defined?
if it is important that it is a constant. If you only care that it is defined, then use the keyword defined?
constant already defined in php
Replace this:
define('constant', 'value');
with this:
if (!defined('constant')) define('constant', 'value');
How do I check if a constant is defined from a different class?
Probably works better with a class name instead of an object variable :
if(defined(get_class($foo).'::BAR')) // Do something
Check if a class constant exists
Yes, just use the class name in front of the constant name:
defined('SomeNamespace\SomeClass::CHECKED_CONSTANT');
http://www.php.net/manual/en/function.defined.php#106287
How to check if a constant is defined by its symbol in Ruby?
Use const_defined?
instead: http://ruby-doc.org/core/classes/Module.html#M000487
Object.const_defined?(const.to_s)
Related Topics
Create Custom HTML Helpers in Ruby on Rails
Complicated Graphviz Tree Structure
Handling Exceptions Raised in a Ruby Thread
How to Create a Hash in Ruby That Compares Strings, Ignoring Case
Why Is Gets Throwing an Error When Arguments Are Passed to My Ruby Script
Getting Uninitialized Constant Error When Trying to Run Tests
How to Improve Jruby Load Time
Undefined Namespace Prefix in Nokogiri and Xpath
How to Dump an Object's Fields to the Console
Trouble Comparing Time with Rspec
Check If a Constant Is Already Defined
What Exactly "Config.Assets.Debug" Setting Does
Rails Date Format in Form Field
How to Deal with Memory Leaks in Rmagick in Ruby
Howto Rank Items by Balance in Ruby on Rails