dthjnc306679 2013-10-10 13:09
浏览 21
已采纳

PHP内爆函数说明

I am relatively new to PHP. The textbook im working from covers PHP5.2 and mentions nothing of implode. The information im getting from PHP manual is a bit unclear to me. Im looking for a short clear explanation and example on implode

  • What exactly is its purpose?
  • Why would you use it?
  • Is it secure?

I know implode returns a string of the elements of its array but how is the following 2 exmples different, and why would you use one over the other:

example 1

$query = 'SELECT `name`, `position` 
        FROM `player_info` 
        WHERE `player_id` IN (' . implode(',', $player_ids) . ')';

example 2

 $result2 = mysql_query("SELECT `fixture_id`, `opponents` 
                         FROM `fixtures` ") or die (mysql_error());

Thank you

  • 写回答

5条回答 默认 最新

  • dpquu9206 2013-10-10 13:15
    关注

    Implode can be particularly useful for lists. Say you have an array of ingredients, you might echo implode(" ",$ingredients) to show them on their own lines.

    In your first example, you need to be very careful. It will only work properly if $player_ids is an array of numbers. implode() by itself is perfectly secure, however improper use can leave gaping holes in your security. A more "correct" version of that code might look like this:

    $query = "SELECT `name`, `position` FROM `player_info`
      WHERE `player_id` IN ('".implode("','",array_map("mysql_real_escape_string",$player_ids))."')";
    

    The above code also handles the case where $player_ids is empty. In your code, it will result in a MySQL syntax error (IN () is not valid), whereas in this code it will simply look for IN (''), which will match no rows.

    Another use for implode might be when packing numbers. Say you have an array of digits, you might implode("",$digits) to pack them into a single string (and str_split($string) to unpack them).

    In short, there are many potential uses for this function, and generally it will be obvious when the function applies. Try not to think too hard about it ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)