Returns loop in php function

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

function myloop($sql){ $query = mysql_query($sql); return while(mysql_fetch_assoc($query)) } 

The reason I want to create this is to avoid repeating the code. Can anybody help me? Thanks..

+4
source share
7 answers

No, you canโ€™t. You can pass a function to a function, but:

 // 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 a 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. }); 

A function that is called from the called party is often called a callback. call_user_func is the best way to call a callback, as it will also accept methods and static methods, not just functions, so you are more flexible.

+3
source

No, but you can mimic this with Iterator for a stable release of PHP today. PHP 5.5 will also create generators.

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

BTW: PDO and MySqli offer it already out of the box (not a lazy query, but the result is possible), for mysql you need to write such a result object iterator-result.

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

+5
source

No, it is not.

You can return a function to do nothing but start the loop, but you cannot return the loop itself.

+1
source

No.

You can, for example, return an anonymous function that may contain a loop, but you can return values โ€‹โ€‹only from functions, and not from language constructs.

+1
source

You have to turn it inside out!

Instead of returning a loop, you can do it this way using Function Variables :

 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 strabismus, this is similar to the concept of the Template Method OOP design scheme.

+1
source

No, but you could do

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

However - eval is extremely dangerous, it really really discourages it.

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

will 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"); 
+1
source

In PHP 5.5+, it is possible to use 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 earlier PHP, but the code is much cleaner.

0
source

Source: https://habr.com/ru/post/1440711/


All Articles