PHP/25 Essential Functions

This is a list of 25 interesting PHP functions that are useful in a variety of situations. Please do not make changes to the actual set of functions without first discussing them on the talk page.

Function list
Following is the raw list of 25, in alphabetical order:
 * 1) ceil
 * 2) count
 * 3) die
 * 4) echo
 * 5) empty
 * 6) exit
 * 7) file_get_contents
 * 8) file_put_contents
 * 9) getenv
 * 10) header
 * 11) htmlentities
 * 12) include
 * 13) ini_set
 * 14) isset
 * 15) mail
 * 16) md5
 * 17) mkdir
 * 18) phpinfo
 * 19) preg_match
 * 20) print_r
 * 21) rand
 * 22) require
 * 23) str_replace
 * 24) str_len
 * 25) trim

Ceil
float ceil (float value)

Rounds up the supplied float value to the next integer and returns the result.

Usage


Practical applications
Can be used to check if a number divides evenly with another - for example: 

$num1 = real line('enter the num1'); $num2 = real line('enter the num2'); $div = $num2 / $num1; if (ceil($div) == $div) echo "$num2 divides into $num1 evenly."; else echo "$num2 does not divide into $num1 easily ($div).";

Count
int count (mixed var)

Returns an integer value of the number of elements in the supplied variable - generally an array, as anything else will return 1.

Usage


Practical applications
Handling every element of an array - for example:  Bear in mind that this could be done without count using the foreach case structure anyway.

Die
void die ([string status])

Terminates the current script execution. If parameter 'status' is specified, also displays 'status' in an error message. Alias for exit.

Usage


Practical applications
Terminating the script if a database connection cannot be opened. 

Echo
void echo (string outputstring)

Outputs data to the current stream (console, apache request etc.). Is more a language construct than a function, and can therefore be used without the parenthesis.

Usage


Practical applications


Empty
bool empty (mixed var)

Used to check if a variable is empty. Note: It is used to only check variables, thus empty(trim($var)); will result in a parse error.

Usage


Practical applications


Pitfall
0 is considered empty. 

Exit
void exit ( [String status] ) void exit ( int status )

Exactly the same as die.

file_get_contents
String file_get_contents ( String filename [, boolean use_include_path [, resource context [, int offset [, int maxlen]]]] )

Reads a file into a string.

Usage


Practical applications


file_put_contents
int file_put_contents ( string filename, mixed data, [int flags , [resource context]] )

Writes data to a file. The flags are optional and can be FILE_USE_INCLUDE_PATH, FILE_APPEND and/or LOCK_EX. By default, without FILE_APPEND flag, function will overwrite file with given data.

Note: This is a PHP5 function only.

Will return false on failure, or the number of bytes written to the file.

Usage


Practical applications


PHP4 Workaround
There is nothing really special about this function, except it saves your time (it basically calls fopen, fwrite and fclose). Here is something that can be used for making sure your PHP script works with PHP5 and PHP4: 

This is of course a very basic replacement and does not take into account any flags which goes beyond the scope of this document.

getenv
string getenv ( string varname)

Returns the value of an environment variable

Usage
<?php echo getenv('HTTP_USER_AGENT'); //will output what web browser is currently viewing the page ?>

Practical applications
Allows you to execute commands based on server information

<?php if (strpos(getenv('HTTP_USER_AGENT'), "Mozilla") !== FALSE) {    // Execute Mozilla specific code } ?>

header
void header (string string [, bool replace [, int http_response_code]] )

Updates the HTTP headers that are sent to the web browser

Usage
<?php header("Cache-Control: no-cache"); ?>

Practical applications
One use for the header function is to redirect the browser to a different web site

<?php if ($_POST['username'] == "") {    header("Location: http://my.url.com"); exit; } // Redirects the browser to http://my.url.com ?>

Another is to change how the browser will interpret the data that is sent to it

<?php header("Content-Type: text/xml") echo '<?xml version="1.0" encoding="ISO-8859-1" ?>'; // Rest of XML output ?>