41. chroot (PHP 4 >= 4.0.5, PHP 5)
chroot — Change the root directory
Description
bool chroot ( string $directory )
Changes the root directory of the current process to directory.
This function is only available if your system supports it and you’re using the CLI, CGI or Embed SAPI. Also, this function requires root privileges.


42. chunk_split (PHP 4, PHP 5)
chunk_split — Split a string into smaller chunks
Description
string chunk_split ( string $body [, int $chunklen [, string $end]] )
Can be used to split a string into smaller chunks which is useful for e.g. converting base64_encode() output to match RFC 2045 semantics. It inserts end every chunklen characters.


43. copy (PHP 4, PHP 5)
copy — Copies file
Description
bool copy ( string $source, string $dest [, resource $context] )
Makes a copy of the file source to dest.
If you wish to move a file, use the rename() function.
$file = ‘example.txt’;
$newfile = ‘example.txt.bak’;

if (!copy($file, $newfile)) {
echo "failed to copy $file…n";
}


44. count (PHP 4, PHP 5)
count — Count elements in an array, or properties in an object
Description
int count ( mixed $var [, int $mode] )
Returns the number of elements in var, which is typically an array, since anything else will have one element.
For objects, if you have SPL installed, you can hook into count() by implementing interface Countable. The interface has exactly one method, count(), which returns the return value for the count() function.
If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.
Note: The optional mode parameter is available as of PHP 4.2.0.
If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. The default value for mode is 0. count() does not detect infinite recursion.
Caution
count() may return 0 for a variable that isn’t set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset() to test if a variable is set.
Please see the Array section of the manual for a detailed explanation of how arrays are implemented and used in PHP.
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3

$b[0] = 7;
$b[5] = 9;
$b[10] = 11;
$result = count($b);
// $result == 3

$result = count(null);
// $result == 0

$result = count(false);
// $result == 1

$food = array(‘fruits’ => array(‘orange’, ‘banana’, ‘apple’),
‘veggie’ => array(‘carrot’, ‘collard’, ‘pea’));

// recursive count
echo count($food, COUNT_RECURSIVE); // output 8

// normal count
echo count($food); // output 2


45. count_chars (PHP 4, PHP 5)
count_chars — Return information about characters used in a string
Description
mixed count_chars ( string $string [, int $mode] )
Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.
$data = "Two Ts and one F.";

foreach (count_chars($data, 1) as $i => $val) {
echo "There were $val instance(s) of "" , chr($i) , "" in the string.n";
}
The above example will output:

There were 4 instance(s) of " " in the string.
There were 1 instance(s) of "." in the string.
There were 1 instance(s) of "F" in the string.
There were 2 instance(s) of "T" in the string.
There were 1 instance(s) of "a" in the string.
There were 1 instance(s) of "d" in the string.
There were 1 instance(s) of "e" in the string.
There were 2 instance(s) of "n" in the string.
There were 2 instance(s) of "o" in the string.
There were 1 instance(s) of "s" in the string.
There were 1 instance(s) of "w" in the string.


46. empty (PHP 4, PHP 5)
empty — Determine whether a variable is empty
Description
bool empty ( mixed $var )
Determine whether a variable is considered to be empty.
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
echo ‘$var is either 0, empty, or not set at all’;
}

// Evaluates as true because $var is set
if (isset($var)) {
echo ‘$var is set even though it is empty’;
}


47. end (PHP 4, PHP 5)
end — Set the internal pointer of an array to its last element
Description
mixed end ( array &$array )
end() advances array’s internal pointer to the last element, and returns its value.

$fruits = array(‘apple’, ‘banana’, ‘cranberry’);
echo end($fruits); // cranberry


48. ereg (PHP 4, PHP 5)
ereg — Regular expression match
Description
int ereg ( string $pattern, string $string [, array &$regs] )
Searches a string for matches to the regular expression given in pattern in a case-sensitive way.


49. ereg_replace (PHP 4, PHP 5)
ereg_replace — Replace regular expression
Description
string ereg_replace ( string $pattern, string $replacement, string $string )
This function scans string for matches to pattern, then replaces the matched text with replacement.
$string = "This is a test";
echo str_replace(" is", " was", $string);
echo ereg_replace("( )is", "1was", $string);
echo ereg_replace("(( )is)", "2was", $string);


50. eregi (PHP 4, PHP 5)
eregi — Case insensitive regular expression match
Description
int eregi ( string $pattern, string $string [, array &$regs] )
This function is identical to ereg() except that it ignores case distinction when matching alphabetic characters
$string = ‘XYZ’;
if (eregi(‘z’, $string)) {
echo "’$string’ contains a ‘z’ or ‘Z’!";
}


51. eregi_replace (PHP 4, PHP 5)
eregi_replace — Replace regular expression case insensitive
Description
string eregi_replace ( string $pattern, string $replacement, string $string )
This function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.
$pattern = ‘(>[^<]*)(‘. quotemeta($_GET[‘search’]) .’)’;
$replacement = ‘12‘;
$body = eregi_replace($pattern, $replacement, $body);


52. explode (PHP 4, PHP 5)
explode — Split a string by string
Description
array explode ( string $delimiter, string $string [, int $limit] )
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *


53. implode (PHP 4, PHP 5)
implode — Join array elements with a string
Description
string implode ( string $glue, array $pieces )
Join array elements with a glue string.
Note: implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.
$array = array(‘lastname’, ’email’, ‘phone’);
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone


54. fclose (PHP 4, PHP 5)
fclose — Closes an open file pointer
Description
bool fclose ( resource $handle )
The file pointed to by handle is closed.
$handle = fopen(‘somefile.txt’, ‘r’);
fclose($handle);


55. fopen (PHP 4, PHP 5)
fopen — Opens file or URL
Description
resource fopen ( string $filename, string $mode [, bool $use_include_path [, resource $context]] )
fopen() binds a named resource, specified by filename, to a stream.
$handle = fopen("c:datainfo.txt", "r");
Table 93. A list of possible modes for fopen() using mode
mode Description
‘r’ Open for reading only; place the file pointer at the beginning of the file.
‘r+’ Open for reading and writing; place the file pointer at the beginning of the file.
‘w’ Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
‘w+’ Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
‘a’ Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
‘a+’ Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
‘x’ Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
‘x+’ Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

Note: Different operating system families have different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system. Unix based systems use n as the line ending character, Windows based systems use rn as the line ending characters and Macintosh based systems use r as the line ending character.
If you use the wrong line ending characters when writing your files, you might find that other applications that open those files will "look funny".
Windows offers a text-mode translation flag (‘t’) which will transparently translate n to rn when working with the file. In contrast, you can also use ‘b’ to force binary mode, which will not translate your data. To use these flags, specify either ‘b’ or ‘t’ as the last character of the mode parameter.
The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the ‘t’ mode if you are working with plain-text files and you use n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the ‘b’ in all other cases.
If you do not specify the ‘b’ flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with rn characters.
Note: For portability, it is strongly recommended that you always use the ‘b’ flag when opening files with fopen().
Note: Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the ‘t’ mode so that it uses the correct line endings and ‘b’ mode instead.



56. fgets (PHP 4, PHP 5)
fgets — Gets line from file pointer
Description
string fgets ( resource $handle [, int $length] )
Gets a line from file pointer.
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}


57. file (PHP 4, PHP 5)
file — Reads entire file into an array
Description
array file ( string $filename [, int $flags [, resource $context]] )
Reads an entire file into an array.
Note: You can use file_get_contents() to return the contents of a file as a string.
// Get a file into an array. In this example we’ll go through HTTP to get
// the HTML source of a URL.
$lines = file(‘http://www.example.com/&#8217;);
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : " . htmlspecialchars($line) . "
n";
}
// Another example, let’s get a web page into a string. See also file_get_contents().
$html = implode(”, file(‘http://www.example.com/&#8217;));


58. file_get_contents (PHP 4 >= 4.3.0, PHP 5)
file_get_contents — Reads entire file into a string
Description
string file_get_contents ( string $filename [, int $flags [, resource $context [, int $offset [, int $maxlen]]]] )
This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.


59. file_exists (PHP 4, PHP 5)
file_exists — Checks whether a file or directory exists
Description
bool file_exists ( string $filename )
Checks whether a file or directory exists.
$filename = ‘/path/to/foo.txt’;
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}


60. filesize (PHP 4, PHP 5)
filesize — Gets file size
Description
int filesize ( string $filename )
Gets the size for the given file.
$filename = ‘somefile.txt’;
echo $filename . ‘: ‘ . filesize($filename) . ‘ bytes’;

Leave a comment