Return a loop in function php

后端 未结 7 1211
忘了有多久
忘了有多久 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:42

    No, but you can simulate that with an Iterator for stable released PHP as of today. In PHP 5.5 there will be generators that is close, too.

    $lazyQuery = new SqlResultItertor($sql);
    foreach ($lazyQuery as $assoc) {
        $assoc; # the result, one per row
    }
    

    BTW: PDO and MySqli offer this already out of the box (not lazy query, but the result is traversable), for mysql you need to write such an iterator-result object your own.

    For some mysql_* functions related code, see this answer. However the general suggestion as of today is to use PDO or mysqli instead, those offer more out of the box. See How to successfully rewrite old mysql-php code with deprecated mysql_* functions?

    0 讨论(0)
  • 2021-01-13 14:43

    You should turn it inside out!

    Instead of returning the loop, you could do it this way using Variable functions :

    function myloop($sql, $myFunction){ 
        $query = mysql_query($sql);  
        while(mysql_fetch_assoc($query)) {
            $myFunction($result);
        }
    
    } 
    
    
    function doSomethingWithTheResult($result) {
       echo $result; // just to have something here...
    }
    
    //now the usage:
    myloop("SELECT 1", 'doSomethingWithTheResult');
    

    With a squint, this is similar to the concept of the Template method OOP design pattern.

    0 讨论(0)
  • 2021-01-13 14:46

    No, it isn't.

    You can return a function to does nothing but run a loop, but you can't return the loop itself.

    0 讨论(0)
  • 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 "<tr><td>".$key."<td><td>".$value."</td></tr>\n";
     }
    }
    

    So you can call

    loopdate("select * from mytable","myfunc");
    
    0 讨论(0)
  • 2021-01-13 14:50

    In PHP 5.5+ it is possible, using the yield keyword instead of return (this is called a generator):

    function myloop($sql) {
        $query = mysql_query($sql);
        while (($row = mysql_fetch_assoc($query))) {
            yield $row;
        }
    }
    
    foreach (myloop('SELECT * FROM foo') as $row) {
        // do something with $row
    }
    

    This is not much different from what you could do with iterators in older PHP, but the code is much cleaner.

    0 讨论(0)
  • 2021-01-13 14:52

    No, you can't. You can pass a function to a function, though:

    // The myloop function, with another name.
    function query($sql, $rowcallback)
    {
      $query = mysqli_query($sql); // use mysqli, not mysql
      while 
        ( ($row = mysql_fetch_assoc($query)) &&
          call_user_func($rowcallback, $row) );
    }
    
    // The code to process a row.
    function processRow(array $row)
    {
       // Use your row here..
       var_dump($row);
    
       return true; // You can return false to break processing.
    }
    
    //calling:
    query($yourSelf, 'processRow');
    

    Instead of passing the function by name, you can also use anonymous functions, depending on your php version:

    //calling:
    query($yourSelf, 
      function(array $row)
      { 
        var_dump($row); 
        return true; // You can return false to break processing.
      });
    

    The function which is called from the callee is often called a callback. call_user_func is the best way to call a callback, since it will also accept methods and static methods, not just functions, so you're more flexible.

    0 讨论(0)
提交回复
热议问题