日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 5.6 引用传递,升级到5.6.x后如何在php中修复引用传递

發布時間:2025/3/8 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 5.6 引用传递,升级到5.6.x后如何在php中修复引用传递 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我最近將fom php 5.2升級到5.6,并且有一些代碼我無法修復:

//Finds users with the same ip- or email-address

function find_related_users($user_id) {

global $pdo;

//print_R($pdo);

//Let SQL do the magic!

$sth = $pdo->prepare('CALL find_related_users(?)');

$sth->execute(array($user_id));

//print_R($sth);

//Contains references to all users by id, to check if a user has already been processed

$users_by_id = array();

//Contains arrays of references to users by depth

$users_by_depth = array();

while ($row = $sth->fetchObject()) {

//Create array for current depth, if not present

if (!isset($users_by_depth[$row->depth]))

$users_by_depth[$row->depth] = array();

//If the user is new

if (!isset($users_by_id[$row->id])) {

//Create user array

$user = array(

'id' => $row->id,

'name' => $row->name,

'email' => $row->email,

'depth' => $row->depth,

'adverts' => array()

);

//Add all users to depth array

@array_push($users_by_depth[$row->depth], &$user);

//Add references to all users to id array (necessary to check if the id has already been processed)

$users_by_id[$row->id] = &$user;

}

//If user already exists

else

$user = &$users_by_id[$row->id];

//Add advert to user

if ($row->advert_id != null)

array_push($user['adverts'], array(

'id' => $row->advert_id,

'title' => $row->advert_title,

'msgs' => $row->msgs,

'url' => $row->url

));

#print_r($user);

//Unset $user variable !!!

//If this is missing, all references in the array point to the same user

unset($user);

}

//Return users, grouped by depth

return $users_by_depth;

}

如果僅刪除美元符號前的與號,該功能將停止按預期工作.從關于stackoverflow的其他問題中,我發現這是通過引用進行的調用,對于新的php版本,它將停止.但是我找不到解決方案.

感謝您對如何為php 5.6.x更新此代碼的任何幫助

解決方法:

您的代碼可能永遠無法正常工作,因為您正在抑制array_push()調用中的錯誤.請注意,只有array_push()的第一個參數通過引用傳遞,其他值始終按值傳遞.

您應該刪除錯誤抑制器@(切勿在自己的代碼中使用它),在這種情況下,您還可以執行以下操作:

$users_by_depth[$row->depth][] = &$user;

^^ add an element just like `array_push`

現在,您在$users_by_depth中的新值將包含對$user變量的引用.

標簽:variables,reference,php

來源: https://codeday.me/bug/20191120/2041320.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的php 5.6 引用传递,升级到5.6.x后如何在php中修复引用传递的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。