Php: what's the difference between $var and &$var?

前端 未结 5 1829
心在旅途
心在旅途 2021-02-18 22:36

What is the difference between

foreach ($my_array as $my_value) {
}

And:

foreach ($my_array as &$my_value) {
}
5条回答
  •  猫巷女王i
    2021-02-18 22:52

    When you prefix a variable with an ampersand, you’re creating a “reference.” PHP references are like shortcuts or symlinks on your computer. You can create a pointer variable that is just another name for the same data.

    I dont see a big difference in using these, except that you DONT COPY a variable saving memory. When you are passing variables you can just pass the reference and the reference points to the original object.

提交回复
热议问题