addslashes()top
adds backslash to single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).
string addslashes ( string str );
array()top
$array = array('Hi', 'Hello', 'day' => 'Tuesday'); print_r($array); echo $array[0]; echo $array['day']; array_reverse($array); count($array);//3
call_user_func()top
function guitar($type){ echo "I want a $type guitar!!!"; } call_user_func('guitar', "Jackson");
Common PHP Variablestop
$_SERVER["PHP_SELF"] /public/eltreno/php/index.php $_SERVER["DOCUMENT_ROOT"] /var/www/rootdir $_SERVER["SCRIPT_URI"] $_SERVER["SCRIPT_URL"] $_SERVER["REMOTE_ADDR'] 127.0.0.1 $_SERVER["REQUEST_HOST"] $_SERVER["REQUEST_METHOD"] GET $_SERVER["QUERY_STRING"] query=php $_SERVER["REQUEST_URI"] /public/eltreno/php/ $_SERVER["PATH_INFO"] $_SERVER["argv"] $_SERVER["HTTP_REFERER"] http://somedowmain.com/?query=1 $_SERVER["HTTP_USER_AGENT"]
cookies()top
stored on client
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]]); setcookie ("TestCookie", "something", time() - 3600, "/~rasmus/", ".example.com", 1, 1); $_COOKIE['cookie'];
eval()top
Processes as php code
$string1 = 'Here I am!!'; $string2 = 'echo $string1;'; eval($string2); //will echo Here I am!!
for()top
for($i=0; $i<10; $i++){ echo $i; }
foreach()top
foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement $arr = array("one", "two", "three"); reset ($arr); while (list(, $value) = each ($arr)) { echo "Value: $value<br />\n"; } foreach ($arr as $value) { echo "Value: $value<br />\n"; }
function()top
function functionName($defaultValue='something', $var){ echo $defaultValue . ' ' . $var; //or return $defaultValue . ' was sent'; }
htmlentities()top
convert special chars to html entities
string htmlentities ( string string [, int quote_style [, string charset]] ); converts > to >
include() / require()top
_once only allows file to be included once
include($filename); include_once('filename.php'); require($filename); require_once('filename.php');
Increase Max file size NGINXtop
php.ini vim /etc/php5/fpm/php.ini > upload_max_filesize = 100M > post_max_size = 100M Change in Nginx config http { #... client_max_body_size 100m; #... }
operatorstop
! ++ -- @ suppress errors * / % + - . concatenation < <= > >= == === != !== & | && || and or ?:
preg_match()top
regular expression (email validation)
preg_match( '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' , $_POST['email']);
rand()top
Random number
echo rand(int loswest_num, int highest_num); //lowest and highest numbers are inclusive in result
regular expressionstop
regular expressions
foo The string "foo" ^foo "foo" at the start of a string foo$ "foo" at the end of a string ^foo$ "foo" when it is alone on a string [abc] a, b, or c [a-z] Any lowercase letter [^A-Z] Any character that is not a uppercase letter (gif|jpg) Matches either "gif" or "jpeg" [a-z]+ One or more lowercase letters [0-9\.\-] Any number, dot, or minus sign ^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _ ([wx])([yz]) wy, wz, xy, or xz [^A-Za-z0-9] Any symbol (not a number or a letter) ([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers
sessions()top
stored on server
session_start(); session_unset(); session_destroy(); session_cache_expire(380); session_id(); $_SESSION['myvar'];
strpos()top
return position in string
int strpos ( string haystack, string needle [, int offset]); int strpos ("This dog bites", "bi" ,0); // returns 9
strstr()top
Find first occurrence of a string (for case-insensitive use stristr)
$email = '[email protected]';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
str_replace()top
str_replace (for case-insensitive use str_ireplace)
mixed str_replace (searchStr, replaceStr, string); //replace line breaks from text boxes with <br /> str_replace ("\x0a", '<br />', $string);
timertop
timer functionality
//#################### START TIMER ################## $time_start = microtime(true); for ($i=0;$i<100;$i++){ //#################### START TIMER ################## //do some code //#################### END TIMER ################## } $time_end = microtime(true); $time = $time_end - $time_start; echo "Did nothing in $time seconds\n"; //#################### END TIMER ##################
while()top
$i=0; while($i < 10){ echo $i; $i++; } $i=0; while($i < 10){ echo $i; $i++; if ($i>5){ continue; //skip rest of loop and go to next iteration also continue 2; continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. } else { echo "Still under 5<br />"; } } $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5<br />\n"; break 1; /* Exit only the switch. */ case 10: echo "At 10; quitting<br />\n"; break 2; /* Exit the switch and the while. */ default: break; } }