V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Mac
V2EX  ›  PHP

拼接字符串和值,有更简洁的写法么?

  •  
  •   Mac · 2016-07-12 02:44:02 +08:00 · 2438 次点击
    这是一个创建于 2859 天前的主题,其中的信息可能已经有所发展或是发生改变。

    $a="XXX"; $a_value=$运算值; $a_txt=$a.$a_vlue; if ($a_value=0){ $a_txt=""; }

    $b="XXX";
    $b_value=$运算值;
    $b_txt=$a.$a_vlue;
    if ($b_value=0){
    $b_txt="";
    }
    ..........
    $abc=$a_txt.$b_txt.$c_txt...
    
    4 条回复    2016-07-12 13:15:18 +08:00
    master
        1
    master  
       2016-07-12 03:28:05 +08:00   ❤️ 1
    $a_txt = ($a_value !== 0) ? "XXX{$a_value}" : '';
    dreamcog
        2
    dreamcog  
       2016-07-12 04:17:50 +08:00
    PHP 比较建议用双引号和{}来做字符串拼接,主要是显而易见。

    $a = 'world';
    $b = "hello {$a}"
    wingoo
        3
    wingoo  
       2016-07-12 09:37:29 +08:00
    sprintf
    miaotaizi
        4
    miaotaizi  
       2016-07-12 13:15:18 +08:00
    $arr = array('Hello','World!','I','love','Shanghai!');
    echo implode(" ",$arr);

    这个算吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2484 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 16:09 · PVG 00:09 · LAX 09:09 · JFK 12:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.