Mac
V2EX  ›  PHP

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

  •  
  •   Mac · Jul 12, 2016 · 2960 views
    This topic created in 3683 days ago, the information mentioned may be changed or developed.

    $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 replies    2016-07-12 13:15:18 +08:00
    master
        1
    master  
       Jul 12, 2016   ❤️ 1
    $a_txt = ($a_value !== 0) ? "XXX{$a_value}" : '';
    dreamcog
        2
    dreamcog  
       Jul 12, 2016
    PHP 比较建议用双引号和{}来做字符串拼接,主要是显而易见。

    $a = 'world';
    $b = "hello {$a}"
    wingoo
        3
    wingoo  
       Jul 12, 2016
    sprintf
    miaotaizi
        4
    miaotaizi  
       Jul 12, 2016
    $arr = array('Hello','World!','I','love','Shanghai!');
    echo implode(" ",$arr);

    这个算吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1034 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:57 · PVG 02:57 · LAX 11:57 · JFK 14:57
    ♥ Do have faith in what you're doing.