Mac
V2EX  ›  PHP

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

  •  
  •   Mac · Jul 12, 2016 · 3078 views
    This topic created in 3768 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
    master
    Jul 12, 2016❤️1
    1
    $a_txt = ($a_value !== 0) ? "XXX{$a_value}" : '';
    dreamcog
    dreamcog
    Jul 12, 2016
    2
    PHP 比较建议用双引号和{}来做字符串拼接,主要是显而易见。

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

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