PHP Tutorials
101. Locking files in PHP
By: Andi, Stig and Derick : 2008-11-23
Description: While writing to files that are possibly being read by other scripts at the same time, you will run into problems at some point because a write might not totally be completed while another script is reading the same file. The reading script will only see a partial file at that moment. Preventing this problem is not hard to do, and the method for this is called locking.
102. Renaming and Removing Files in PHP
By: Andi, Stig and Derick : 2008-11-23
Description: PHP provides the unlink() function for deleting a file, which "unlinks" the file from a directory. On a UNIX-like system the file will only be deleted if no programs have this file in use. This means that with the following script, the bytes associated with the file will only be released to the operating system after the fclose() is executed:
103. tmpfile() in PHP
By: Andi, Stig and Derick : 2008-11-23
Description: In case you want to create a temporary file, the best way to do it is with the tmpfile() function. This function creates a temporary file with a unique random name in the current directory and opens this file for writing. This temporary file will be closed automatically when you close the file with fclose() or when the script ends:
104. preg_match(), function preg_match_all(), preg_grep() in PHP
By: Andi, Stig and Derick : 2008-11-23
Description: preg_match() is the function that matches one pattern with the subject string and returns either true or false depending whether the subject matched the pattern. It also can return an array containing the contents of the different sub-pattern matches. X Turns on extra features in the PCRE engine. At the moment, the only feature it turns on is that the engine will throw an error in case an unknown escape sequence was detected. Normally, this would just have been treated as a literal. (Notice that we still have to escape the one \ for PHP itself.)
105. preg_replace() and preg_replace_callback() in PHP
By: Andi, Stig and Derick : 2008-11-23
Description: PHP's regular expression functions can also replace text based on pattern matching. The replacement functions can replace a substring that matches a subpattern with different text. In the replacement, you can refer to the pattern matches using back references. Here is an example that explains the replacement functions. In this example, we use preg_replace() to replace a pseudo-link, such as [link url="www.php.net"]PHP[/link], with a real HTML link:
106. preg_split() and explode() in PHP
By: Andi, Stig and Derick : 2008-11-23
Description: preg_split() can be used to split a string into substrings by using a regular expression match for the delimiters. PHP provides an explode() function that also splits strings, but explode() can only use a simple string as the delimiter. explode() is much faster than using a regular expression, so you might be better off using explode() when possible. A simple example of preg_splits()'s usage might be to split a string into the words it contains.
107. Execution Lifetime of a PHP script
By: Andi, Stig and Derick : 2008-11-23
Description: When embedded in a web server, PHP scripts usually do their job quickly and exit. This paradigm does not fit when using CLI; your scripts may run forever, or at least until the next power failure. For example, if you write a daemon (UNIX lingo for a server process running in the background), the script will typically hang around forever, waiting for some kind of input to process, a timer signal, or something similar.
108. Input Validation in PHP
By: Andi, Stig and Derick : 2008-11-23
Description: One essential technique to protect your web site from users is input validation, which is an impressive term that doesn't mean much at all. The term simply means that you need to check all input that comes from the user, whether the data comes from cookies, GET, or POST data.
109. __autoload() METHOD in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: When writing object-oriented code, it is often customary to put each class in its own source file. The advantage of this is that it's much easier to find where a class is placed, and it also minimizes the amount of code that needs to be included because you only include exactly the classes you need. The downside is that you often have to include tons and tons of source files, which can be a pain, often leading to including too many files and a code-maintenance headache.
110. Here-docs (<<<) in PHP
By: Emiley J. : 2008-11-22
Description: Here-docs enable you to embed large pieces of text in your scripts, which may include lots of double quotes and single quotes, without having to constantly escape them.