Blogger Information
Blog 53
fans 3
comment 0
visits 55280
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
20200119-GET传值中的空格与(int)-***线上九期班
邯郸易住宋至刚
Original
608 people have browsed it

GET传值与Request接收

1、前端传值代码

  1. <script>
  2. function admin_menu_child(mid) {
  3. window.location.href = '?mid = ' + mid;
  4. }
  5. </script>

2、后台接收代码

  1. $mid = (int)$request->mid;
  2. echo '<pre>';
  3. print_r($request->all());
  4. print_r($mid);
  5. return;

3、结果

4、分析

明明$request->all()有值,而$request->mid无值,说明$request->mid有问题,$request->没问题,那就只有mid有问题。
一遍又一遍的排查,忽然发现$request->all()拿到的值是mid_,而不是mid,奇怪,这是什么问题,原来是window.location.href = ‘?mid = ‘ + mid;当中的’?mid = ‘在捣鬼,把等号之前的空格拿掉,再看结果,正常了。

5、(int)的使用

$mid = (int)$request->mid;当中的(int),如果没有,那么当mid无值时, $mid的类型是undefined,如果使用,就会报错。如果使用(int)强制转换成整数,当遇到$mid的类型是undefined会转换在“0”,使用时就不会报错了。

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments