Tag Archives: function

__autoload

PHP introduced the __autoload() function in version 5 which is called whenever the code tries to use a class that has not yet been defined. You simply put some code into __autoload() to include the appropriate class file and don’t have to bother about manually including those files.

This global function is called whenever you try to create an object of a class that hasn’t been defined. It takes just one parameter, which is the name of the class you have not defined. If you define an object as being from a class that PHP does not recognise, PHP will run this function, then try to re-create the object – you have a second chance to have the right class.

func_get_args

Returns an array in which each element is a copy of the corresponding member of the current user-defined function’s argument list.

<?php
 function testFunction() {
 echo "Number of arguments : " .func_num_args();
 }
testFunction(1, 2, 3);
?>

It outputs Number of arguments : 3

ob_start

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.

<?php
 function callback($buffer) {
 return (str_replace("PHP", "PHP Codez", $buffer));
 }
 ob_start("callback");
?>
 <p>Welcome to PHP</p>
<?php
 ob_end_flush();
?>

The output will be Welcome to PHP Codez

quote_meta

The quotemeta() function adds backslashes in front of some predefined characters in a string.

This function can be used to escape characters with special meanings, such as ( ), [ ], and * in SQL.

This function is binary-safe.

The predefined characters are:

  • period (.)
  • backslash (\)
  • plus sign (+)
  • asterisk (*)
  • question mark (?)
  • brackets ([])
  • caret (^)c
  • dollar sign ($)
  • parenthesis (())

Example

<?php
 $str = "PHP Codez. (Welcome)";
 echo quotemeta($str);
?>

It outputs PHP Codez\. \(Welcome\)

parse_url

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

  • scheme – e.g. http
  • host
  • port
  • user
  • pass
  • path
  • query – after the question mark ?
  • fragment – after the hashmark #

This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.

<?php
 $url = 'phpcodez.com/index.php?user=1';
 echo "<pre>";print_r(parse_url($url)); 
?>
Output
Array
(
    [path] => phpcodez.com/index.php
    [query] => user=1
)

preg_match_all

The preg_match_all() function matches all occurrences of pattern in string.

It will place these matches in the array pattern_array in the order you specify using the optional input parameter order. There are two possible types of order −

PREG_PATTERN_ORDER − is the default if the optional order parameter is not included. PREG_PATTERN_ORDER specifies the order in the way that you might think most logical; $pattern_array[0] is an array of all complete pattern matches, $pattern_array[1] is an array of all strings matching the first parenthesized regexp, and so on.

PREG_SET_ORDER − will order the array a bit differently than the default setting. $pattern_array[0] will contain elements matched by the first parenthesized regexp, $pattern_array[1] will contain elements matched by the second parenthesized regexp, and so on.

The function returns  the number of matching.

<?php
 $text = <<<EOT
 The big bang bonged under the bung.
 EOT;
 echo preg_match_all('@b.n?g@', $text, $matches);
 print_r($matches);
?>

property_exists

This function checks if the given property exists in the specified class (and if it is accessible from the current scope).

It returns TRUE if the property exists, FALSE if it doesn’t exist or NULL in case of an error.

<?php
 $a = array('a'=>'a', 'b'=>'c');
 echo property_exists((object) $a, 'a')?'true':'false';
 ?>

true-true will be the output