Help us understand the problem. What is going on with this article?

PHPの自作関数で元の値を書き換えるには&で参照渡しする

参照渡し(元の値が書き換わる)

hoge.php
function hoge (&$argument) {
  for ($i = 0; $i <= count($argument); $i++) {
    if ($argument[$i] == 0){
      $argument = $argument + 1;
    };
  };
};

こういう配列の中身の数値が、
0のものには1を足すっていう関数があって、

hoge($array);

実行すれば、実際に配列の中身が変わります。

値渡し

hoge.php
function hoge ($argument) {
  for ($i = 0; $i <= count($argument); $i++) {
    if ($argument[$i] == 0){
      $argument = $argument + 1;
    };
  };
};

仮引数の&がないと値渡しになるので、

hoge($array);

実行しても元の値は書き換わりません。

最後に

メインで書いているのがRubyで、
破壊的な操作〜とかで検索しても出なかったのでメモ。
まあ普通に考えればわかることではありますが。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした