PHP Constant Quiz

I created some little sourcecode and have two simple questions. Quiz is to find answer w/o executing it – let your head be the PHP interpreter. It’s about Namespace, Const and Define:

  1. How many constants are introduced?
  2. How many warning / notices (E_ALL | E_STRICT)?
namespace foo;
const constant = 'value';
defined('constant') || 
define ('constant', 'value');
defined('\constant') || 
define ('\constant', 'value');
defined('\\constant') || 
define ('\\constant', 'value');
defined('\\\constant') || 
define ('\\\constant', 'value');
defined(__NAMESPACE__.'constant') || 
define (__NAMESPACE__.'constant', 'value');
defined(__NAMESPACE__.'\constant') || 
define (__NAMESPACE__.'\constant', 'value');
defined(__NAMESPACE__.'\\constant') || 
define (__NAMESPACE__.'\\constant', 'value');
defined(__NAMESPACE__.'\\\constant') || 
define (__NAMESPACE__.'\\\constant', 'value');

Have fun.

This entry was posted in Pressed and tagged , , , , , , . Bookmark the permalink.

3 Responses to PHP Constant Quiz

  1. Denis says:

    None. A trickier question, IMO, would have been which of these return constants contain “value”.😉

    file1.php:

    define(‘constant’, ‘bar’);

    file2.php;

    namespace foo;
    const constant = ‘value’;
    defined(‘constant’) ||
    define (‘constant’, ‘value’);
    defined(‘\constant’) ||
    define (‘\constant’, ‘value’);
    defined(‘\\constant’) ||
    define (‘\\constant’, ‘value’);
    defined(‘\\\constant’) ||
    define (‘\\\constant’, ‘value’);
    defined(__NAMESPACE__.’constant’) ||
    define (__NAMESPACE__.’constant’, ‘value’);
    defined(__NAMESPACE__.’\constant’) ||
    define (__NAMESPACE__.’\constant’, ‘value’);
    defined(__NAMESPACE__.’\\constant’) ||
    define (__NAMESPACE__.’\\constant’, ‘value’);
    defined(__NAMESPACE__.’\\\constant’) ||
    define (__NAMESPACE__.’\\\constant’, ‘value’);

  2. pce says:

    This one: “fooconstant”
    defined(__NAMESPACE__.’constant’) ||
    define (__NAMESPACE__.’constant’, ‘value’);

    const is a class constant, while define is a “global” userland Constant.
    const in userland would trigger an error.
    constant is a functionname, error.
    and all that defines with a Backslash…mmmhhh? Warnings.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s