snippet list:
PHP - Basic
switch - integers - last updated: 05:14 Thursday, February 12
php switch example
switch - strings - last updated: 05:14 Thursday, February 12
switch example using strings
write string to file - last updated: 05:14 Thursday, February 12
write string variable to a text file
read file to a string - last updated: 05:14 Thursday, February 12
Read a text file to a string variable using fgets.
foreach loop - last updated: 05:16 Thursday, February 12
simple php example of a for-each loop, useful for iterating over lists and objects arrays.
object declaration - last updated: 05:16 Thursday, February 12
declare an object in php. This example also shows how to pass a value to the object when created.
array declaration - last updated: 20:50 Thursday, February 12
simple php array declaration:
passing variables via url - last updated: 20:50 Thursday, February 12
Pass variables to a page using the question mark, for example mypage.php?value=hello. Access these variables with the _GET command.
string replace - last updated: 02:23 Friday, February 13
find and replace pattern in a string.
send email - last updated: 23:55 Saturday, February 14
Send an email from PHP - one line example.
get current filename as variable - last updated: 00:58 Friday, June 4
Sometimes you need just the filename of the current script, without the path. This snippet does just that using the SCRIPT_NAME and explode function.
redirect browser - last updated: 01:21 Friday, June 4
Using the php header function, a browser can be redirected to another page.
email validation, simple - last updated: 15:39 Friday, June 4
Validate an email address - returns TRUE or FALSE. Checks format only, does not use any networking functions to verify the account actually exists.
ban / block IP addresses - last updated: 20:00 Friday, June 4
This example gets the users IP address and exits the page if it matches a banned number.
get current page URL - last updated: 14:04 Monday, June 14
Often used for form actions, the variable $PHP_SELF has changed with recent versions, and now must be accessed using the _SERVER array of pre-defined variables.
for loop - simple - last updated: 16:20 Monday, June 14
for loop - basic control structure. This example loops until the variable hits 10 and then breaks out.
form action - detect form post - last updated: 16:21 Monday, June 14
There are several ways to process a php form. This example looks for the existance (regardless of value) of the Submit _POST variable.
random number - last updated: 07:03 Tuesday, June 15
Generate a random number using mt_rand. From the php documentation- "If called without the optional min, max arguments mt_rand() returns a pseudo-random value between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use mt_rand (5, 15)."
force cookies, disable php sessid in URL - last updated: 00:16 Thursday, June 17
This commands disables php from rewriting URLs to add a phpsessid, and forces the use of cookie sessions instead. Using cookies is largely thought of as the preferred way to use sessions - both more secure and better for SEO. This can be accomplished by setting the appropriate values in php.ini, or at runtime. This snippet shows a runtime example:
show referring URL - last updated: 04:14 Friday, July 2
Grabs the referring URL - get the HTTP_REFERER and save it to a variable.
check if a file exists - last updated: 16:45 Wednesday, July 7
To determine whether a file exists on the server, PHP has a simple function: file_exists.
include a file with error reporting - last updated: 19:34 Wednesday, July 7
Sometimes you want to be able to give some feedback if a PHP include has failed. Here is a version using the fopen command, which can search the include path.
if else example - last updated: 23:11 Wednesday, July 7
basic example of an if then else control structure.
form handling example - simple - last updated: 00:49 Thursday, July 8
This example shows a very simple form together with the form handling process on a single page.

Smarty
date and time formatting - last updated: 23:42 Saturday, February 14
The Smarty date_format function returns a formatted string showing date, time and/or seconds.

ezSQL

OTHER



php.snippetDB.com php.snippetdb.com is powered by www.dreamhost.com