php$var是什么,Php:$ var和&$ var之間有什么區別?

22

The first example creates a copy of the value, whereas the second uses a reference to the original value. So after the first foreach runs, the original array is still untouched. After the second foreach the original array could have been modified since it was handled by reference.

第一個示例創建值的副本,而第二個示例使用對原始值的引用。因此,在第一次foreach運行后,原始陣列仍未觸及。在第二個foreach之后,原始數組可能已被修改,因為它是通過引用處理的。

Some native PHP functions already work this way, such as shuffle() which rearranges the contents of your array. You'll notice that this function doesn't return an array, you just call it:

一些本機PHP函數已經以這種方式工作,例如shuffle(),它重新排列數組的內容。您會注意到此函數不會返回數組,您只需調用它:

$myArray = array('foo', 'bar', 'fizz', 'buzz');

shuffle( $myArray );

// $myArray is now shuffled

And it works its magic since it works with the array by reference rather than creating a copy of it.

它起作用,因為它通過引用與數組一起工作,而不是創建它的副本。

Then there are functions that don't pass anything by reference but rather deal with a copy of the original value, such as ucwords() which returns the new resulting string:

然后有些函數不通過引用傳遞任何東西,而是處理原始值的副本,例如ucwords(),它返回新的結果字符串:

$myString = "hello world";

$myString = ucwords( $myString );

// $myString is now capitalized

請參閱傳遞參考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值