普通网友 2014-02-02 13:15
浏览 41

PHP基本不了解一些代码?

I am struggling understanding how this code works. when we call the function with a value 5, the value of c, becomes 5. Now 5 + 25 = 30 . What i don't understand is how the value of the $_GET['c'] equals to 25, when it should be 5.

my answer would therefore be 5 + 25 - 5 - 10 = 15.

<?php
   function process($c, $d = 25){
     global $e;
     $retval = $c + $d - $_GET['c'] - $e;
     return $retval;
   }

   $e = 10;
   echo process(5);
?>
  • 写回答

1条回答 默认 最新

  • drdyf42880 2014-02-02 13:21
    关注

    Its variables should be getting another value, maybe you use "global" you might be picking up the value from another location.

    Try show variables:

    <?php
       function process($c, $d = 25){
         global $e;
    
         echo '$e: ', $e , '<br>';
         echo '$c: ', $c , '<br>';
         echo '$d: ', $d , '<br>';
         echo '$_GET['c']: ', $_GET['c'] , '<br>';
    
         $retval = $c + $d - $_GET['c'] - $e;
    
         echo $c ,' + ' , $d , ' - ' , $_GET['c'] , ' - ' , $e , ' = ' , $retval , '<hr>';
    
         return $retval;
       }
    
       $e = 10;
       echo process(5);
    ?>
    

    So you will probably find the variable problem.

    In some cases you can use a static function in a class (php object oriented)

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题