kmvan
V2EX  ›  PHP

不启用的选项值,设定为 -1 好还是 0 好?

  •  
  •   kmvan · Feb 13, 2015 · 3882 views
    This topic created in 4296 days ago, the information mentioned may be changed or developed.

    例如默认值为“不启用”,那么这个 value 设定为 -1 还是 0 比较好?

    <?php
    $default_enable = -1 // or 0?
    $checked = $default_enable === -1 ? ' checked ' : null; // or === 0?
    ?>
    <input type="checkbox" value="<?php echo $default_enable;?>"  <?php echo $checked;?> />
    
    9 replies  •  2015-02-14 06:20:34 +08:00
    abelyao
    abelyao
    Feb 13, 2015
    1
    删了 < 0,禁用 = 0,可用 > 0
    目前几个项目都啥这样表示,其实怎样都好,全项目统一就好
    suliuyes
    suliuyes
    Feb 13, 2015
    2
    跟楼上一样。一般我是,0和1表示开关,0是关闭禁用,1是启用,-1表示删除之类的不可用状态。
    lujiajing1126
    lujiajing1126
    Feb 13, 2015
    3
    默认值一般是0
    可以参考FLAGS系统
    icedx
    icedx
    Feb 13, 2015
    4
    一般都是0
    yksoft1
    yksoft1
    Feb 13, 2015
    5
    习惯使用C/C++的,禁用肯定会定义于0
    miaoever
    miaoever
    Feb 13, 2015
    6
    用 0 可以用 unsigned int 存储, -1 就只能是 signed int 了。
    lincanbin
    lincanbin
    Feb 14, 2015
    7
    0

    如果你用0的话,你可以这么写:
    $checked = $default_enable ? ' checked ' : null;

    不过你为什么不用bool呢?
    lincanbin
    lincanbin
    Feb 14, 2015
    8
    PHP官方指定了true=1, false=0
    echo intval(false);//0
    echo intval(true);//1

    所以你可以这样:
    <?php
    $default_enable = 0;
    $checked = $default_enable ? ' checked ' : null;
    ?>
    <input type="checkbox" value="<?php echo $default_enable;?>" <?php echo $checked;?> />

    也可以这样:
    <?php
    $default_enable = false;
    $checked = $default_enable ? ' checked ' : null;
    ?>
    <input type="checkbox" value="<?php echo intval($default_enable); ?>" <?php echo $checked; ?> />
    ryd994
    ryd994
    Feb 14, 2015
    9
    @miaoever 不差这点吧……
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2317 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 14:09 · PVG 22:09 · LAX 06:09 · JFK 09:09
    ♥ Do have faith in what you're doing.