熊猫购彩

<meter id="pgsoe"></meter>

<code id="pgsoe"><u id="pgsoe"></u></code>

<acronym id="pgsoe"><video id="pgsoe"></video></acronym>
    1. <label id="pgsoe"></label>
  • <acronym id="pgsoe"><legend id="pgsoe"><blockquote id="pgsoe"></blockquote></legend></acronym>
      <label id="pgsoe"></label>
    <acronym id="pgsoe"></acronym>
  • <meter id="pgsoe"><u id="pgsoe"><option id="pgsoe"></option></u></meter>
    <output id="pgsoe"></output>
  • <var id="pgsoe"><ol id="pgsoe"></ol></var>
    DPC2020 - Workshop day

    Constants

    Table of Contents

    A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

    The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$

    It is possible to define() constants with reserved or even invalid names, whose value can (only) be retrieved with constant(). However, doing so is not recommended.

    Tip

    See also the Userland Naming Guide.

    Example #1 Valid and invalid constant names

    <?php

    // Valid constant names
    define("FOO",     "something");
    define("FOO2",    "something else");
    define("FOO_BAR""something more");

    // Invalid constant names
    define("2FOO",    "something");

    // This is valid, but should be avoided:
    // PHP may one day provide a magical constant
    // that will break your script
    define("__FOO__""something"); 

    ?>

    Note: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 128 through 255 (0x80-0xff).

    Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope. For more information on scope, read the manual section on variable scope.

    add a note add a note

    User Contributed Notes 12 notes

    up
    207
    wbcarts at juno dot com
    8 years ago
    11/14/2016 - note updated by sobak
    -----

    CONSTANTS and PHP Class Definitions

    Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away.

    <?php

    define
    ('MIN_VALUE', '0.0');   // RIGHT - Works OUTSIDE of a class definition.
    define('MAX_VALUE', '1.0');   // RIGHT - Works OUTSIDE of a class definition.

    //const MIN_VALUE = 0.0;         RIGHT - Works both INSIDE and OUTSIDE of a class definition.
    //const MAX_VALUE = 1.0;         RIGHT - Works both INSIDE and OUTSIDE of a class definition.

    class Constants
    {
     
    //define('MIN_VALUE', '0.0');  WRONG - Works OUTSIDE of a class definition.
      //define('MAX_VALUE', '1.0');  WRONG - Works OUTSIDE of a class definition.

     
    const MIN_VALUE = 0.0;      // RIGHT - Works INSIDE of a class definition.
     
    const MAX_VALUE = 1.0;      // RIGHT - Works INSIDE of a class definition.

     
    public static function getMinValue()
      {
        return
    self::MIN_VALUE;
      }

      public static function
    getMaxValue()
      {
        return
    self::MAX_VALUE;
      }
    }

    ?>

    #Example 1:
    You can access these constants DIRECTLY like so:
    * type the class name exactly.
    * type two (2) colons.
    * type the const name exactly.

    #Example 2:
    Because our class definition provides two (2) static functions, you can also access them like so:
    * type the class name exactly.
    * type two (2) colons.
    * type the function name exactly (with the parentheses).

    <?php

    #Example 1:
    $min = Constants::MIN_VALUE;
    $max = Constants::MAX_VALUE;

    #Example 2:
    $min = Constants::getMinValue();
    $max = Constants::getMaxValue();

    ?>

    Once class constants are declared AND initialized, they cannot be set to different values -- that is why there are no setMinValue() and setMaxValue() functions in the class definition -- which means they are READ-ONLY and STATIC (shared by all instances of the class).
    up
    19
    Raheel Khan
    5 years ago
    class constant are by default public in nature but they cannot be assigned visibility factor and in turn gives syntax error

    <?php

    class constants {

        const
    MAX_VALUE = 10;
            public const
    MIN_VALUE =1;

    }

    // This will work
    echo constants::MAX_VALUE;

    // This will return syntax error
    echo constants::MIN_VALUE;
    ?>
    up
    10
    gried at NOSPAM dot nsys dot by
    4 years ago
    Lets expand comment of 'storm' about usage of undefined constants. His claim that 'An undefined constant evaluates as true...' is wrong and right at same time. As said further in documentation ' If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string...'. So yeah, undefined global constant when accessed directly will be resolved as string equal to name of sought constant (as thought PHP supposes that programmer had forgot apostrophes and autofixes it) and non-zero non-empty string converts to True.

    There are two ways to prevent this:
    1. always use function constant('CONST_NAME') to get constant value (BTW it also works for class constants - constant('CLASS_NAME::CONST_NAME') );
    2. use only class constants (that are defined inside of class using keyword const) because they are not converted to string when not found but throw exception instead (Fatal error: Undefined class constant).
    up
    18
    ewspencer at industrex dot com
    16 years ago
    I find using the concatenation operator helps disambiguate value assignments with constants. For example, setting constants in a global configuration file:

    <?php
    define
    ('LOCATOR',   "/locator");
    define('CLASSES',   LOCATOR."/code/classes");
    define('FUNCTIONS', LOCATOR."/code/functions");
    define('USERDIR',   LOCATOR."/user");
    ?>

    Later, I can use the same convention when invoking a constant's value for static constructs such as require() calls:

    <?php
    require_once(FUNCTIONS."/database.fnc");
    require_once(
    FUNCTIONS."/randchar.fnc");
    ?>

    as well as dynamic constructs, typical of value assignment to variables:

    <?php
    $userid 
    = randchar(8,'anc','u');
    $usermap = USERDIR."/".$userid.".png";
    ?>

    The above convention works for me, and helps produce self-documenting code.

    -- Erich
    up
    10
    hafenator2000 at yahoo dot com
    15 years ago
    PHP Modules also define constants.  Make sure to avoid constant name collisions.  There are two ways to do this that I can think of.
    First: in your code make sure that the constant name is not already used.  ex. <?php if (! defined("CONSTANT_NAME")) { Define("CONSTANT_NAME","Some Value"); } ?>  This can get messy when you start thinking about collision handling, and the implications of this.
    Second: Use some off prepend to all your constant names without exception  ex. <?php Define("SITE_CONSTANT_NAME","Some Value"); ?>

    Perhaps the developers or documentation maintainers could recommend a good prepend and ask module writers to avoid that prepend in modules.
    up
    10
    katana at katana-inc dot com
    18 years ago
    Warning, constants used within the heredoc syntax (http://www.bjzci.com/manual/en/language.types.string.php) are not interpreted!

    Editor's Note: This is true. PHP has no way of recognizing the constant from any other string of characters within the heredoc block.
    up
    3
    Andreas R.
    13 years ago
    If you are looking for predefined constants like
    * PHP_OS (to show the operating system, PHP was compiled for; php_uname('s') might be more suitable),
    * DIRECTORY_SEPARATOR ("\\" on Win, '/' Linux,...)
    * PATH_SEPARATOR (';' on Win, ':' on Linux,...)
    they are buried in 'Predefined Constants' under 'List of Reserved Words' in the appendix:
    http://www.bjzci.com/manual/en/reserved.constants.php
    while the latter two are also mentioned in 'Directory Functions'
    http://www.bjzci.com/manual/en/ref.dir.php
    up
    4
    storm
    15 years ago
    An undefined constant evaluates as true when not used correctly. Say for example you had something like this:

    settings.php
    <?php
    // Debug mode
    define('DEBUG',false);
    ?>

    test.php
    <?php
    include('settings.php');

    if (
    DEBUG) {
      
    // echo some sensitive data.
    }
    ?>

    If for some reason settings.php doesn't get included and the DEBUG constant is not set, PHP will STILL print the sensitive data. The solution is to evaluate it. Like so:

    settings.php
    <?php
    // Debug mode
    define('DEBUG',0);
    ?>

    test.php
    <?php
    include('settings.php');

    if (
    DEBUG == 1) {
      
    // echo some sensitive data.
    }
    ?>

    Now it works correctly.
    up
    -3
    jcastromail at yahoo dot es
    2 years ago
    Performance of constants.  PHP 7.1.10 32 bits (Opcache active, windows 10 i7-64bits) but apparently the trends is the same with the 5.x

    using a constant declared by DEFINE('CNS',value) : 0.63575601577759s
    using a constant declared by const CNS=value : 0.61372208595276s
    using a variable declared by $v=value : 0.51184010505676s

    In average, the use of DEFINE and CONST is around the same with some sightly  better performance of CONST instead of DEFINE. However, using a variable is around 10-50% better than to use a constant.  So, for a performance intensive task, constant is not the best option.

    $p1=microtime(true);
    $x=0;
    for($i=0;$i<50000000;$i++) {
        $x+=CNS;
    }
    $p2=microtime(true);
    up
    -21
    php at webflips dot net
    6 years ago
    It is perfectly valid to use a built-in PHP keyword as a constant name - as long as you the constant() function to retrieve it later:

    <?php
    define
    ('echo', 'My constant value');

    echo
    constant('echo'); // outputs 'My constant value'
    ?>
    up
    -1
    Sumon Mahmud (Abu Taleb)
    3 months ago
    define('MYKEY', 'The value is from outside of class');
    class Abc{
       
        const MAX = 10;    

        public function __construct(){
            define('TEST', 'hello world! '); // using define function in class constructor
        }
        public function getOutput(){
            echo TEST . MYKEY . PHP_EOL; // accessing constants from outside of class
        }
    }

    $obj = new Abc();  // define function will call
    $obj->getOutput(); // hello world! The value is from outside of class

    echo Abc::MAX . PHP_EOL; // 10 - accessing constant using scope resolution

    echo TEST; // hello world!  Because the constants is defined while constructor call
    up
    -39
    xs2ilyas at gmail dot com
    3 years ago
    When we start a constant name with space, it doesn't produce any error.

    <?php
      define
    (" YEAR"2000)
      echo
    "success";
    ?>

    output: success

    But when we call this constant, it produce error.

    <?php
      define
    (" YEAR"2000)
      echo
    YEAR;
    ?>

    output: E_NOTICE :  type 8 -- Use of undefined constant YEAR - assumed 'YEAR' -- at line 4 YEAR
    To Top
    乐昌| 虎林| 辽中| 蕉岭| 潢川| 沛县| 桓仁| 铜陵| 察哈尔右翼中旗| 吉兰太| 太原北郊| 嘉黎| 连云港| 阿勒泰| 新河| 邹平| 古浪| 镇巴| 井研| 青阳| 都安| 长岛| 怀化| 沂源| 平顶山| 郫县| 科尔沁左翼后旗| 巴南| 泽普| 鄂州| 托托河| 额济纳旗| 成安| 乾安| 阆中| 长兴| 镇康| 郓城| 黟县| 临城| 阜宁| 额济纳旗| 新建| 平台| 东乡| 鹰潭| 陆川| 金平| 万州龙宝| 乌审旗| 宁安| 瓦房店| 皋兰| 密山| 永春| 南阳| 潞城| 白沙| 宁国| 鹤山| 五道梁| 江夏| 安庆| 塘头| 南岳| 德庆| 宝坻| 纳雍| 句容| 十三间房气象站| 济南| 会泽| 浚县| 长岭| 延津| 千里岩| 赤壁| 宣化| 伽师| 酒泉| 亳州| 分宜| 武夷山| 万载| 怀化| 太康| 临潼| 南宁| 尚志| 舒城| 长治| 祁东| 霍城| 鄱阳| 天津| 丹阳| 莲塘| 美姑| 丹凤| 贵德| 英吉沙| 屯昌| 金州| 宜宾县| 鹤壁| 武山| 合作| 镶黄旗| 黔阳| 太原| 凉山| 莒南| 尤溪| 梧州| 环县| 潜江| 万山| 川沙| 金州| 门头沟| 洪江| 甘洛| 枣阳| 武城| 长白| 磐安| 西吉| 永新| 柏乡| 晋江| 鄂尔多斯| 黑山| 驻马店| 龙泉驿| 桂平| 凤城| 浑源| 茶卡| 阿合奇| 北辰| 宁洱| 永定| 康山| 延津| 法库| 荔波| 民勤| 乐昌| 莱芜| 铜鼓| 尚义| 盐城| 丰城| 满都拉| 都安| 东宁| 桓仁| 天津| 会理| 加格达奇| 海西| 曲沃| 肇庆| 门源| 金山| 万山| 榆社| 南昌| 翁牛特旗| 英德| 涿州| 无锡| 灯塔| 敖汉旗| 兴仁| 道县| 泸县| 荣县| 宁海| 伊宁县| 雷山| 湖州| 乌兰乌苏| 魏县| 江安| 临朐| 理塘| 襄城| 会理| 呼中| 渭南| 西吉| 莲塘| 理县| 千阳| 临汾| 太原古交区| 阿拉善右旗| 永平| 永丰| 定日| 景县| 德钦| 潞西| 麦积| 永仁| 吉水| 孙吴| 武山| 会泽| 洋县| 北道区| 武川| 蔡家湖| 孤家子| 六合| 新沂| 乌兰| 库尔勒| 禄劝| 德安| 枣庄| 屏边| 剑河| 铜川| 崇左| 鄯善| 吉安县| 武汉| 农安| 华山| 茌平| 阿瓦提| 隆林| 清水河| 耒阳| 五峰| 仙居| 红柳河| 泰兴| 玛沁| 武川| 万宁| 新宁| 雅布赖| 甘南| 宁国| 镇雄| 二连浩特| 临澧| 琼中| 桃园| 金川| 凤冈| 双牌| 咸宁| 泾阳| 讷河| 都匀| 肃南| 广南| 尼勒克| 平原| 稻城| 农安| 灌云| 荣县| 陆川| 井陉| 石河子| 广安| 镇沅| 广安| 新晃| 龙泉| 资溪| 枣庄| 清原| 婺源| 藁城| 达川| 祁连| 云澳| 富平| 上虞| 丹东| 澄城| 潢川| 平江| 遮浪| 孟连| 南澎岛| 彭阳| 修武| 耒阳| 华蓥山| 靖西| 米易| 天台| 鄂伦春旗| 陈家镇| 威信| 汤阴| 新界| 怀远| 岚县| 孟州| 乐安| 师宗| 新民| 鄂托克旗| 庆阳| 云梦| 阜宁| 泰兴| 杂多| 杭锦后旗| 克什克腾旗| 安多| 托克托| 城口| 歙县| 乌斯太| 遮浪| 平山| 淮北| 晋宁| 怀安| 比如| 崇阳| 江油| 美姑| 隆昌| 仁化| 武平| 吴忠| 莒县| 葫芦岛| 苏尼特左旗| 怀仁| 德江| 莎车| 正镶白旗| 始兴| 满城| 芜湖县| 沽源| 括苍山| 孤家子| 错那| 庆元| 五峰| 得荣| 丹棱| 全南| 金湖| 宁陕| 东港| 井研| 西吉| 静海| 临武| 晋城| 华安| 巴雅尔吐胡硕| 乌什| 麻阳| 永平| 羊山| 大通| 许昌| 奉化| 江永| 阜阳| 六安| 宜昌县| 歙县| 临夏| 魏县| 屏边| 麦积| 莱阳| 达州| 赵县| 上杭| 松江| 泽当| 芜湖县| 索伦| 鄄城| 班玛| 太湖| 诸暨| 京山| 龙陵| 南丰| 临淄| 武清| 正安| 定边