zoukankan      html  css  js  c++  java
  • php function 按引用返回一个值

        按引用返回一个值,而不是按值返回,这样就无需为变量建立一个重复的副本

     1 function &array_find_value($needle, &$haystack)
     2 {
     3     foreach ($haystack as $key=>$value){
     4         if ($needle == $value){
     5             //必须返回一个变量的引用,而不能是包含这个变量的字符串
     6             return $haystack[$key];
     7         }
     8     }
     9 }
    10 $artists = ['table','The Doors'];
    11 $band =& array_find_value('The Doors',$artists);
    12 var_dump($band); // string 'The Doors'
    13 //改变数组
    14 $band = 'thhe doors name is panpan';
    15 var_dump($artists);
    16 /*
    17   array (size=2)
    18     0 => string 'table' (length=5)
    19     1 => &string 'thhe doors name is panpan' (length=25)
    20  */
  • 相关阅读:
    ioi1998 Polygon
    [Noip模拟题]Seq
    [noip模拟]分组行动
    入门OJ:photo
    Sgu167 I-country
    入门OJ:简单的网络游戏
    入门OJ:Coin
    ATT&CK实战系列
    Metasploit Framework(二)
    RoarCTF 2019
  • 原文地址:https://www.cnblogs.com/loveyouyou616/p/5541521.html
Copyright © 2011-2022 走看看