0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

No8_ググったこと投稿(5分)_PHP系 参照渡し

Posted at

目次

1.PHP 参照渡し

きっかけ

leetcode(下記)で出てきて、参照渡しは思いついたのですが具体的に何か覚えてなかったので、調べました!

    function reverseString(&$s) {
        // code...
    }

1. PHP 参照渡し

  • 変数のメモリ番地(メモリ上の位置)を渡す方法で、変数のメモリ番地を渡しているため、関数内で引数の値を変更すると、呼び出し元の変数も変更されてしまいます。
    • メリット
      • 値をコピーしない分だけ高速に動作する可能性有り
    • デメリット
      • 多用するとプログラムの流れを追いかけ難くする原因になります。
    • 値渡し
      • 変数の値をコピーして渡す方法で、変数の値をコピーして渡すため、関数内で引数の値を変更しても、呼び出し元の変数に影響を及ぼすことはありません。
参照渡し 例)
function add (&$number) {    // 「&」を引数につける
	return ++$number;
}

$a = 1;
$b = add($a);

echo $a; //2
echo $b; //2

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?