dopmgo4707 2011-01-24 12:46
浏览 5
已采纳

为什么分配顺序的这种变化会改变变量内容?

Explain this please :)

If I run

$custId = getExternalId();
echo $custId . "
"; // Prints foobar_16262499_1
$serial = '';

$custId = explode('_', $custId);
var_dump($custId);
$custId = $custId[1];
$serial = $custId[2];

die("custId: $custId serial: $serial
");

I get

custId: 16262499 serial: 2

This is not correct. Serial should be 1. But if I change the order of assignment to

$custId = getExternalId();
echo $custId . "
"; // Prints foobar_16262499_1
$serial = '';

$custId = explode('_', $custId);
var_dump($custId);
$serial = $custId[2];   // Change order here!!!
$custId = $custId[1];

die("custId: $custId serial: $serial
");

It works and gives me

custId: 16262499 serial: 1

WHY?

In both cases the var_dump of the array produces the same output:

array(3) {
  [0]=>
  string(4) "foobar"
  [1]=>
  string(8) "16262499"
  [2]=>
  string(1) "1"
}

I'm running PHP/5.3.3 ZendServer

SMACKS HEAD... How could I miss the obvious :)...

  • 写回答

2条回答 默认 最新

  • duancoubeng5909 2011-01-24 12:51
    关注

    you override

    $custId 
    

    when you write this line

    $custId = $custId[1];
    

    so after that you get something you do not expect

    $serial = $custId[2];
    

    so do like this

    list($custId,$serial) = array($custId[1],$custId[2]); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别