All posts by Pramod T P

headers_list()

It returns a list of response headers sent

Example

<?php
 setcookie("time",time());
 header('Content-type: text/plain');
 echo "<pre>";
 print_r(headers_list());
 ?>

Output

<pre>Array
 (
 [0] => X-Powered-By: PHP/5.3.3-1ubuntu9.10
 [1] => Set-Cookie: time=1337938449
 [2] => Content-type: text/plain
 )

mysql_unbuffered_query()

It executes a query on a MySQL database and buffering the result

A. Return the first data faster
B. Does not return all data faster
C. Use less memory

Example

<?php
 mysql_pconnect("localhost", "root", "phpcode") or die(mysql_error());
 $db_selected = mysql_select_db("database_name");
 echo mysql_result(mysql_unbuffered_query( "SELECT * FROM users"),1);
 mysql_close();
 ?>

xpath()

It runs an XPath query on XML data

  • XPath is a major element in the XSLT standard.
  • XPath can be used to navigate through elements and attributes in an XML document.
  • XPath stands for XML Path Language
  • XPath uses “path like” syntax to identify and navigate nodes in an XML document
  • XPath contains over 200 built-in functions
  • XPath is a major element in the XSLT standard
  • XPath is a W3C recommendation

Example

<?php
$xml = simplexml_load_file(“test.xml”);
$xml->registerXPathNamespace(“msg”,”http://www.phpcodez.com”);
$result = $xml->xpath(“msg:web”);
?>

XML

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>

substr_compare()

It compares two strings from a specified start position

This function returns:
0 – if the two strings are equal
<0 – if string1 (from startpos) is less than string2
>0 – if string1 (from startpos) is greater than string2
If length is equal or greater than length of string1, this function returns FALSE

Example

<?php
 echo substr_compare("phpcodexc","phpcode",0);
 ?>

Output

2

soundex()

It calculates the  soundex key of a string

  • A soundex key is a four character long alphanumeric string that represent English pronunciation of a word.
  • The soundex() function can be used for spelling applications.
  • The soundex() function creates the same key for similar sounding words.

Example

<?php
 echo soundex("phpcode");
 ?>

Output

P123

quotemeta()

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 (^)
  • dollar sign ($)
  • parenthesis (())

Example

<?php
 echo quotemeta("php(code)'");
 ?>

Output

php(code)’

metaphone()

It calculate the metaphone key of a string

  • A metaphone key represents how a string sounds if said by an English speaking person.
  • The metaphone() function can be used for spelling applications.
  • The metaphone() function creates the same key for similar sounding words.
  • The generated metaphone keys vary in length.

Example

<?php
 echo metaphone("phpcode");
 ?>

Output

FPKT

htmlspecialchars()

It converts predefined characters to HTML entities.  The predefined characters are:

  • & (ampersand) becomes &amp;
  • ” (double quote) becomes &quot;
  • ‘ (single quote) becomes &#039;
  • < (less than) becomes &lt;
  • > (greater than) becomes &gt;

Example

<?php
 echo htmlspecialchars("<a href='http://phpcodez.comt'>phpcode</a>");
 ?>

Output

<a href=’http://phpcodez.comt’>phpcode</a>

pathinfo

It returns information about a file path

Example

<?php
echo "<pre>";
print_r(pathinfo("test.ini"));
?>

Output

Array
(
[dirname] => .
[basename] => test.ini
[extension] => ini
[filename] => test
)

 

 

move_uploaded_file

It moves an uploaded file to a new location. It is used to validate whether the contents of $_FILES[‘name’][‘tmp_name’] have really been uploaded via HTTP, and also save the contents into another folder.

Example

<?php
 move_uploaded_file($tmp_name, $_FILES["file"]["name"]) or die("Failed");
?>