PHP Code Snippets

Provides key PHP (PHP Hypertext Preprocesor) statements and code snippets. Includes PHP-MySQL select, insert, update and delete statements; email, functions, class, while loops, etc.

Version 2.0.0.1. Released: 2008-09-02.

References: PHP.

<?php  ?>
<?  ?>
include '';
require '';
include_once '';
require_once '';
echo '';
if ($a > $b) { // Do this echo 'and this'; } // end if a is greater than b
if ($a > $b) { // Do this echo 'and this'; } else { // Do other } // end if a is greater than b
if ($a > $b) { // Do this echo 'and this'; } elseif ($a < $b) { // Do that echo 'and this'; } else { // The same } // end if a is greater than b
while ($c > 25) { // Do all that } // end while
switch ($var) { case 'this': echo 'Do this'; break; case 'that': echo 'Do that'; break; default: echo 'Default stuff'; } // end switch var
for ($i=0; $i<25; $i++) { // Iterate through the range } // end for i[0-25]
foreach ($array as $value) { echo $value; } // end foreach array
foreach ($array as $key => $value) { echo "$key: $value"; } // end foreach array
break;
continue;
function me ($var1, $var2, $varn) { // process stuff $answer = $var1 + $var2 + $varn; return $answer; } // end (function) me
class meClass { var $meProperty; var $look; function meClass () { // Do inital stuff $this->look = 6; } // end (constructor)meClass function meMethod ($var1, $var2) { $this->$meProperty = $var1; // process object $answer = $this->meProperty + $var2 * $this->look; return $answer; } // end (method) meMethod } // end (class) meClass
$meClass->meMethod(1, 4);
$meClass->meProperty;
// Must not be any output header('location: somewhere-else.php');
$HTTP_GET_VARS['']
$HTTP_POST_VARS['']
$HTTP_COOKIE_VARS['']
$source = $HTTP_POST_FILES['file1']['tmp_name']; $source_name = $HTTP_POST_FILES['file1']['name'];
$HTTP_SESSION_VARS['']
$GLOBALS['']
$HTTP_SERVER_VARS['']
$HTTP_ENV_VARS['']
$_GET['']
$_POST['']
$_COOKIE['']
$source = $_FILES['file1']['tmp_name']; $source_name = $_FILES['file1']['name'];
$_SESSION['']
$GLOBALS['']
$_SERVER['']
$_ENV['']
$_REQUEST['']
$myConnect = mysql_connect('localhost', 'username', 'password'); $myDB = mysql_select_db('myDatabaseName', $myConnect);
$sql = mysql_query("select rec1, rec2 from tableName", $myConnect);
$sql = mysql_query("insert into tableName (rec1, rec2) values ('value1', 1), ('value2', 2)", $myConnect);
$sql = mysql_query("update tableName set rec1='value', rec2=1 where id=1", $myConnect);
$varRealEscapedString = mysql_real_escape_string($varString, $myConnect);
$sql = mysql_query("delete from tableName where id=1", $myConnect);
mysql_close($myConnect);
$myConnect = mysqli_connect('localhost', 'username', 'password', 'myDatabaseName');
$sql = mysqli_query($myConnect, "select rec1, rec2 from tableName");
$sql = mysqli_query($myConnect, "insert into tableName (rec1, rec2) values ('value1', 1), ('value2', 2)");
$sql = mysqli_query($myConnect, "update tableName set rec1='value', rec2=1 where id=1");
$varRealEscapedString = mysqli_real_escape_string($myConnect, $varString);
$sql = mysqli_query($myConnect, "delete from tableName where id=1");
mysqli_close($myConnect);
$mysqli = new mysqli('localhost', 'username', 'password', 'myDatabaseName');
$mysqli->query("select rec1, rec2 from tableName");
$mysqli->query("insert into tableName (rec1, rec2) values ('value1', 1), ('value2', 2)");
$mysqli->query("update tableName set rec1='value', rec2=1 where id=1");
$varRealEscapedString = $mysqli->real_escape_string($varString);
$mysqli->query("delete from tableName where id=1");
$mysqli->close();
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'."\n"; ?>
<?php echo '<?xml version="1.1" encoding="UTF-8"?>'."\n"; ?>
<? echo '<?pitarget commands ?>'."\n"; ?>
<? echo '<?xml-stylesheet type="" href="" media="" alternate="yes/no"?>'."\n"; ?>
if (preg_match("/aaa/", $subject)) { } // end if subject has 'aaa'
$updatedSubject = preg_replace("/aaa/", "bbb", $subject);
// P3P header to allow MS Internet Explorer 6+ to use cookies from domain names with more than one dot. if ((substr($_SERVER['HTTP_USER_AGENT'], 25, 4)==='MSIE') && (substr($_SERVER['HTTP_USER_AGENT'], 30, 1)>=6)) { header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"'); } setcookie('cookieName', 'cookieValue', strtotime('1 hour'), '/', '', 0);
$email = mail('me@localhost.com', 'A Subject', 'This is an email message.', "From: here@elsewhere.com\r\nReply-To: nowhere@a.com\r\nCc: you@here.co.uk\r\nBcc: me@here.co.uk\r\n");

Web-based Code Snippets are copywritten 2008 to Legend Scrolls and Peter Davison.
Web-based Code Snippets are licensed under the Creative Commons Attribution 3.0 Unported.