Return a loop in function php

后端 未结 7 1213
忘了有多久
忘了有多久 2021-01-13 14:04

Is it possible to return a loop? not the result but the loop it self. I want to create a function in php. For example like this.

function myloop($sql){
$quer         


        
7条回答
  •  离开以前
    2021-01-13 14:50

    No, but you could do

    function loopdate($sql,$code)
    {
     $query=mysql_query($sql)
     while (mysql_fetch_assoc($query))
     {
      eval($code);
     }
    }
    

    However - eval is rediculously dangerous its really really discouraged.

    function loopdate($sql,$function)
    {
     $query=mysql_query($sql)
     while ($data=mysql_fetch_assoc($query))
     {
      $function($data);
     }
    }
    

    would be better.

    myfunc($data)
    {
     foreach ($data as $key->$value)
     {
      print "".$key."".$value."\n";
     }
    }
    

    So you can call

    loopdate("select * from mytable","myfunc");
    

提交回复
热议问题