PHP获取变量的变量名的一段代码的bug

百度一下关于php获取变量名的办法都是下面这个函数。但是用了发现有点bug

/**
* 获取变量名
*
* @param $string
* @return $string
*
* $test = "helo";
* $test2 = "helo";
* getVarName($test2);
*/
function getVarName(&$src){
//存储当前变量值
$save = $src;
//存储所有变量值
$allvar = $GLOBALS;
//在函数中不要直拉遍历$GLOBALS,会出现堆栈问题
foreach($allvar as $k=>$v){
//变量值相同,可能不是相同变量,因多个变量的值可能相同
if ($src == $v){
//改变当前变量$src的值
$src = ‘change’;
//如果$GLOBALS[$k]也跟着改变,那就是同一个变量。
if ($src == $GLOBALS[$k]){
//echo "$$k name is $k
";
//还原变量值
$src = $save;
return $k;
}
}
}
}
copy下来后发现这个怎么测试结果有时候对有时候不对,想了很久,终于弄明白,虽然很简单,但是自己还是记录下,希望遇到同样情况的同学注意。

比如:现在我测试

$test2 = "hello";
$countNum=0;
echo getVarName($test2);
//按道理应该输出为”test2“,但是输出的是"countNum",
因为函数中的
if ($src == $v) 这里存在问题 比如$src="hello", $GLOBALS里面有个变量$countNUm=0;

这时在循环的时候判断 if ($src == $v) ,即"hello"==0,比较的结果为true,类型转换的时候“hello“转为整形为0,

然后就退出循环了,得到错误的结果。

一个解决办法就是 if ($src == $v) 改为 if($src===$v),即恒等。

如果我理解错了欢迎您指正,一起进步。

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » PHP获取变量的变量名的一段代码的bug

170 评论

  1. buy doxycycline online cheap order lasix sale lasix buy online

  2. order catapres 0.1mg online cheap minocycline cheap spiriva 9mcg pill

  3. cost furosemide 100mg lasix for sale hydroxychloroquine 400mg for sale

  4. buy flomax 0.4mg online cheap flomax tablet aldactone 100mg sale

  5. order keflex 250mg generic clindamycin brand order erythromycin 500mg

  6. where can i buy a research paper generic stromectol ivermectin generic name

  7. order zithromax 250mg online cheap buy gabapentin 800mg order generic neurontin 800mg

  8. purchase sildenafil sale budesonide cheap budesonide over the counter

  9. montelukast without prescription buy singulair pill viagra 25mg for sale

  10. play casino online free roulette online gambling money sports gambling

  11. asacol 800mg oral order astelin irbesartan oral

  12. buy doxycycline 100mg online cheap oral medrol cost buy medrol 16 mg online

  13. buy metformin 500mg generic buy calan pills nolvadex pills

  14. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across.

  15. order methotrexate 5mg without prescription methotrexate cost reglan online buy

  16. buy generic tamsulosin aldactone pills aldactone 100mg canada

  17. buy carbamazepine 400mg pill ciplox 500 mg brand lincocin without prescription

  18. buy naproxen 250mg for sale buy omnicef pill buy lansoprazole generic

  19. buy generic olumiant 2mg buy cheap olumiant buy atorvastatin without prescription

  20. adalat 30mg drug nifedipine sale order allegra 120mg online cheap

  21. buy metoprolol without a prescription metoprolol us methylprednisolone oral

  22. generic reglan cost nexium cost esomeprazole

  23. purchase dutasteride without prescription ranitidine sale purchase meloxicam without prescription

  24. spironolactone usa simvastatin price valacyclovir 500mg without prescription

  25. lamisil brand buy suprax trimox 500mg canada

  26. over the counter ed pills brand name cialis order cialis 5mg for sale

  27. arcoxia canada astelin generic buy azelastine 10ml for sale

  28. albendazole buy online albenza order order provera sale

  29. order generic nitrofurantoin 100mg furadantin 100 mg drug order nortriptyline 25 mg without prescription

  30. order glucotrol 10mg piracetam oral buy betnovate creams

  31. prozac cheap revia ca letrozole medication

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡