Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Syntax For Beginners Cheat Sheet: by Via

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

PHP Syntax for beginners Cheat Sheet

by guslong via cheatography.com/1345/cs/451/

connection to mysql database IF syntax Regular Expres​sions

Connect to the database if (condi​tion) { ^ start of string

$dbc = mysqli​_co​nne​ct(​HOST, USER, PW, ... } \d number from 0 to 9


elseif (condi​tion) {
DB);
\s whitespace
... }
Query the database else { . any letter or digit or whitespace
$result = mysqli​_qu​ery​($dbc, $query); ... } \w any alphan​umeric [0-9A-​Za-z]

Close the connection $ end of string


Loops
mysqli​_cl​ose​($dbc); () group
FOR loop for (initi​alize; condition; update)
[] character class
{ ... }
PHP functions
{x} {x,} {x,y} x of | x or more of | x to y of
WHILE loop while (condi​tion) { ... }
isset() test for variable | or
FOREACH foreach ($array as $value) { ...
exists
loop } * none or more
empty() test for empty
DO WHILE do { ... ;} while (condi​tion) ? none or one
variable
+ one or more
mail($to, $subject, mail function FOR (loop until a condition is met)
WHILE (loop through query results) \ escape
$msg, 'From: ' . $email)
FOREACH (loop through an array)
mysqli​_fe​tch​_ar​ray​($r​es fetch each row of a
Arrays
ult) query (in $result)
CONTINUE
header() send a header from Create $myArray = array();

the server for ($i = 0; $i < 5; ++$i) { Push into $myArray[] = "​Som​eth​ing​";
if ($i == 2)
is_num​eric() test to see if a value Push to $myArr​ay[​'key'] = "​Val​ue";
continue;
is number associ​ative
print "$i , ";
exit() causes script to stop } Create numeric $myArray = array(​'va​lue',
immedi​ately 'value2');
produces the following output:
trim($​string) trims leading and Create $a = array(​'ke​y'=​>'v​al');
0,1,3,4
trailing spaces associ​ative

mysqli​_re​al_​esc​ape​_st​ri escapes special SWITCH syntax Print from echo $myArr​ay[0];


n​g($​string) characters numeric
SWITCH ($s) {
str_re​pla​ce('a', 'b', replace a with b in a Print from echo $myArr​ay[​'key'];
case 1:
$string) string associ​ative
...
explode(', ' , $string) make string into break; Associ​ative Keys are strings
array case 2: arrays

implode(', " $string) make array into ... Numeric arrays Keys are numbers:
string break; 0,1,2,3,4
default:
substr ($string, start, grabs a substring
...
len)
}
preg_m​atc​h('​regex', matches regular
$string) expres​sions can be used with numbers, or strings

preg_r​epl​ace​('r​egex', replaces characters


$replace, $string) in a string by regex

By guslong Published 20th July, 2012. Sponsored by CrosswordCheats.com


cheatography.com/guslong/ Last updated 2nd June, 2014. Learn to solve cryptic crosswords!
Page 1 of 1. http://crosswordcheats.com

You might also like