Standard PHP Code
//Standard PHP file beginning
session_start(); //needed if you use $_SESSION variables, has to be in the beginning
ini_set('display_errors',0); //1=show all errors, 0=do not show
$VisitorIP = $_SERVER['REMOTE_ADDR']; //read IP adress of visitor
$RandomNumber = mt_rand(1, 9999); //Generate a random number between 1 and 9999
//Variables
//Define variables
$StandardVariable = 'value'; //define a standard variable
$_SESSION["StandardSessionVariable"] = 'value'; //define a session variable
//Change variables
$CalculateableNumber = str_replace(",",".",$GetNumber); //replace string with another string in a string
$Length = strlen($StandardVariable); //Count the length of a variable
$number = round(str_replace(",",".",$number)); //round a number
$substring = substr($string, 0, 10); //use only letter 0-10 of a string
//Arrays
//Define and fill a 1-dimensional array
if (count($_SESSION['array']) == 0) {
$_SESSION['array'] = array('id' => 1, 'name' => 'Holger', 'age' => '30');
};
//Define and fill a multidimensional array
if (count($_SESSION['array']) == 0) {
$_SESSION['array'] = array(
array('id' => 1, 'name' => 'Holger', 'age' => '30'),
array('id' => 2, 'name' => 'Paul', 'age' => '18'),
);
};
//show a 1-dimensional array with while
$i = 0;
while($i < 50)
{
echo $array[$i];
$i++;
};
//show a 1-dimensional array with for (session)
for($i = 0, $ni = count($_SESSION['array']); $i < $ni; $i++){echo $_SESSION['array'][$i];};
//Show a 2 dimensional array
for($i = 0, $ni = count($_SESSION['array']); $i < $ni; $i++){
for($i2 = 0, $ni = count($_SESSION['array']); $i2 < $ni; $i2++){
echo $_SESSION['array'][$i][$i2];
};
};
//Show a 2 dimensional array with foreach
foreach($array as $key => $value){
foreach($value as $key2 => $value2){
echo $value2."
";}
}
//fügt neuen Wert am Anfang des Arrays ein
array_unshift($array, $newValue);
//Database
//Create MySQL connection
$con = mysql_connect("localhost","user","pass"); //connect to database, connection name $conn
if (!$con) {die('Could not connect: ' . mysql_error());} //error if cannot connect
mysql_select_db("DBname", $con) or die(mysql_error()); //error if cannot connect
mysql_query("set names 'utf8'"); //only if UTF-8 Database
//SQL Querie
mysql_close($con); //close the connection $conn
//MySQL Select
$result = mysql_query("SELECT * FROM table WHERE name LIKE '%$name1%' ORDER BY name LIMIT 30 OFFSET 30");
//Count results
// show 1 result
echo mysql_result($result, 0, "name");
// loop results
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
};
//fill 1-dimensional array() with "for"
$array = array();
for($i = 0; $array[$i] = mysql_fetch_assoc($result); $i++);
print_r($array);
//MySQL Insert
mysql_query("INSERT INTO table (name1, name2) VALUES ('$name1', '$name2')");
//MySQL Udate
mysql_query("UPDATE table SET name = '$name2' WHERE name = '$name1' ");
//MySQL Delete
mysql_query("DELETE FROM table WHERE name = '$name1' ");
//MySQL ganze Tabelle Leeren!
mysql_query("TRUNCATE TABLE `table` ");
//SELECT mit LIMIT am Ende, aber trotzdem ASC Sortierung! (Sonderfall)
$result = mysql_query("SELECT * FROM (SELECT * FROM daxminute ORDER BY kurszeit DESC LIMIT 20 ) q ORDER BY kurszeit");
//Date and Time
//Standard date for MySQL database DateTime field
$date = date("Y-m-d H:i:s");
//Count days between now and date
date_default_timezone_set('Europe/Berlin');
$now = new DateTime();
$date = new DateTime('2015-01-01 12:00:00');
$days = $date->diff($now)->format("%d");
echo "Between ".$now." and ".$date." are ".$days." days difference!";
//Timestamp and time between two timestamps
$_SESSION['timestampstart'] = time(); //set start timestamp 1 time
$timestampNow = time(); //read actual timestamp
$timestampDifference = $_SESSION['timestampstart'] - $timestampNow; //calculate difference
//Files
//"Copy" a file from source to destination
$file = 'folder1/file.txt';
$newfile = 'folder2/filecopy.txt';
if (!copy($file, $newfile)) {echo "copy $file error...\n";};
//Check if a file is readable
if (is_readable("folder/".$filename.".jpg")) {echo "
"; };
//logic operators
//if clause
if ($something == "") {echo "empty";} else {echo $something;};
//functions
//include functions from external php file
include 'include/functions.php';
//simple function with 1 input and 1 output
function SIMPLE ($var)
{
$const = 5;
$internalresult = $const * $var;
return $internalresult;
};
$result = SIMPLE(10);
echo $result;
//Forms
//Read Form variables
$GetValue = $_GET['GetValue']; //read a variable from a GET Form or URL
$PostValue = $_POST['PostValue']; //read a variable from a POST Form
if ($_POST['PostValue'] != "") {$PostValue = $_POST['PostValue'];} else {$PostValue = "123";}; //Fill variable only if empty