Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

PHPMaster.com:
Understanding Recursion
Jun 06, 2013 @ 14:32:41

On PHPMaster.com today there's a new tutorial posted that talks about something that can be a more difficult concept for budding developers to grasp - recursion.

In one of my previous articles I wrote about iterators and how you can use them. Today I’d like to look at the fraternal twin of iteration: recursion. A recursive function is one that calls itself, either directly or in a cycle of function calls. Recursion can also refer to a method of problem solving that first solves a smaller version of the problem and then uses that result plus some other computation to formulate an answer to the original problem.

He starts with an example - one function showing how to calculate a factorial using just looping and the other by having the function call itself. He talks some about the different types of recursion (direct/indirect) and gives a more practical example, a "find in array" function for one with nested subarrays. He also briefly mentions "head" and "tail" recursion, the difference being if it waits for a return value or not. He also offers some general advice when it comes to using recursion and how you have to watch for optimization issues.

tagged: recursion tutorial introduction head tail direct indirect

Link: http://phpmaster.com/understanding-recursion

Sameer Borate's Blog:
Tail functionality in PHP
Jul 19, 2011 @ 15:16:41

Sameer Borate has posted an alternative to "tail" that you can use to find the last X number of lines in a log file you'd like to follow without having the overhead of parsing the entire file.

Frequently one needs to get the last few lines of some log files, whether php error logs or Apache logs. Most of these file sizes run into megabytes, which makes it difficult and time consuming to remotely open them using ftp. [...] The [example] is a simple but useful 'tail' implementation in PHP. I've encapsulated the tail function in a 'LogRead' class, which can be further enlarged by adding other useful log functions.

His code opens a file pointer to the requested log, grabs the file size and uses the fseek function to move the pointer to the line/location you've requested. Obviously, if the requested lines of data is large, it will still have some overhead, but this is a much better way for keeping track of the latest additions to a log. You can then use the "tail" method on the "LogRead" class to grab just the lines you want.

tagged: tail system log file tutorial

Link:

Paul Gregg's Blog:
"tail -# file" in PHP
Apr 24, 2009 @ 17:02:42

Paul Gregg has written up an example of what he calls "tail -# file for PHP" (starting from the end of file and moving backwards a given number of lines).

Here I will demonstrate a fairly tuned method of seeking to the end of the file and stepping back to read sufficient lines to the end of the file. If insufficient lines are returned, it incrementally looks back further in the file until it either can look no further, or sufficient lines are returned.

His script meets two goals - reading enough lines in for the request and keeping those lines to a minimum. His code grabs the size of the file and opens the file as a resource to fseek to a certain point and read in the lines from the defined start to finish and push them into an array. You can see the source here and a sample execution here.

tagged: tail log file fseek example read

Link:


Trending Topics: