0% found this document useful (0 votes)
99 views

PHP Syntax For Beginners Cheat Sheet: by Via

This document provides a cheat sheet summary of PHP syntax and functions for beginners. It covers connecting to and querying MySQL databases, common PHP functions, control flow structures like if/else and loops, arrays, regular expressions and more. The cheat sheet is intended to be a quick reference guide for PHP syntax and functions.

Uploaded by

SelfMy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

PHP Syntax For Beginners Cheat Sheet: by Via

This document provides a cheat sheet summary of PHP syntax and functions for beginners. It covers connecting to and querying MySQL databases, common PHP functions, control flow structures like if/else and loops, arrays, regular expressions and more. The cheat sheet is intended to be a quick reference guide for PHP syntax and functions.

Uploaded by

SelfMy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP Syntax for beginners Cheat Sheet

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


connection to mysql database
Connect to the database
$dbc = mysqli_connect(HOST, USER,
PW, DB);
Query the database
$result = mysqli_query($dbc, $query);
Close the connection
mysqli_close($dbc);
PHP functions
isset() test for variable
exists
empty() test for empty
variable
mail($to, $subject,
$msg, From: .
$email)
mail function
mysqli_fetch_arra-
y($result)
fetch each row of a
query (in $result)
header() send a header from
the server
is_numeric() test to see if a
value is number
exit() causes script to
stop immediately
trim($string) trims leading and
trailing spaces
mysqli_real_escap-
e_string($string)
escapes special
characters
str_replace(a, b,
$string)
replace a with b in
a string
explode(, ,
$string)
make string into
array
implode(, "
$string)
make array into
string
substr ($string,
start, len)
grabs a substring
preg_match(regex,
$string)
matches regular
expressions
preg_replace(rege-
x, $replace,
$string)
replaces characters
in a string by regex
IF syntax
if (condition) {
... }
elseif (condition) {
... }
else {
... }
Loops
FOR
loop
for (initialize; condition;
update) { ... }
WHILE
loop
while (condition) { ... }
FORE-
ACH
loop
foreach ($array as $value) { ...
}
DO
WHILE
do { ... ;} while (condition)
FOR (loop until a condition is met)
WHILE (loop through query results)
FOREACH (loop through an array)
CONTINUE
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue;
print "$i , ";
}
produces the following output:
0 , 1 , 3 , 4
SWITCH syntax
SWITCH ($s) {
case 1:
...
break;
case 2:
...
break;
default:
...
}
can be used with numbers, or strings
Regular Expressions
^ start of string
\d number from 0 to 9
\s whitespace
. any letter or digit or
whitespace
\w any alphanumeric
[0-9A-Za-z]
$ end of string
( ) group
[ ] character class
{x} {x,}
{x,y}
x of | x or more of | x to y
of
| or
* none or more
? none or one
+ one or more
\ escape
Arrays
Create $myArray = array();
Push into $myArray[] =
"Something";
Push to
associative
$myArray[key] =
"Value";
Create numeric $myArray =
array(value, value2);
Create
associative
$a =
array(key=>val);
Print from
numeric
echo $myArray[0];
Print from
associative
echo $myArray[key];
Associative
arrays
Keys are strings
Numeric arrays Keys are numbers:
0,1,2,3,4
Cheatographer
guslong
cheatography.com/guslong
Cheat Sheet
Published 20th July, 2012.
Updated 13th August, 2012.
Page 1 of 1.
Sponsor
FeedbackFair www.feedbackfair.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy