Skip to content

Instantly share code, notes, and snippets.

@febritecno
Forked from mulderu/php-cheat-sheet-01.md
Created January 24, 2021 17:34
Show Gist options
  • Save febritecno/e7f17ad41ca8c1bed3ae99f7210426d5 to your computer and use it in GitHub Desktop.
Save febritecno/e7f17ad41ca8c1bed3ae99f7210426d5 to your computer and use it in GitHub Desktop.
php cheat sheet , php look up code, php simple guide
# syntax
array ( "key" => "value", … );
die("message");
do { block } while (condition);
$x = 1; 
while($x <= 5) {
    echo "The number is: $x <br>";
    $x++;
}
echo item;
extract($array);
for (startingval; endingval;incremnt) { block }
for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";
}
foreach( $array as $key => $value) { block }
function funcname(value,value,…) { block }
header("Location: URL");
if (condition) { block }
   elseif (condition) { block }
   else { block }
number_format(number,decimals);
session_start();
session_destroy();
switch var { case value statements break; … }
unset();
while (condition) { block }
define("GREETING","Hello you! How are you today?");
echo constant("GREETING");

# wordpress code examples
require_once( dirname( __FILE__ ) . '/wp-load.php' );
require( dirname( __FILE__ ) . '/wp-load.php' );
if ( ! is_multisite() ) {
	wp_redirect( wp_registration_url() );
	die();
}
$keys     = array_keys( $crons );
$gmt_time = microtime( true );
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
	die();
}
  $args = array(
    'post_type'      => 'post',
    'post_status'    => 'future',
    'category_name'  => 'app_reviews',
    'posts_per_page' => 3
  );
  $coming_soon = new WP_Query( $args );
 

# php and html
$http_post     = ( 'POST' == $_SERVER['REQUEST_METHOD'] );
$interim_login = isset( $_REQUEST['interim-login'] );
<?php
?>
<?php if ( empty( $_GET['key'] ) && empty( $_POST['key'] ) ) { ?>
<?php echo $result['password']; ?>
<p><?php echo $result->get_error_message(); ?></p>

# mysql
mysqli_connect("host","accnt","passwd")
mysqli_select_db($cxn, "dbname",)
mysqli_query($cxn,"query")
mysqli_fetch_assoc($result)
mysqli_num_rows($result)
mysqli_insert_id($cxn)

# mysql example
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();


# PHP Array Functions
array_diff (arr1, arr2 ...)
array_filter (arr, function)
array_flip (arr)
array_intersect (arr1, arr2 ...)
array_merge (arr1, arr2 ...)
array_pop (arr)
array_push (arr, var1, var2 ...)
array_reverse (arr)
array_search (needle, arr)
array_walk (arr, function)
count (count)
in_array (needle, haystack)

#PHP String Functions
crypt (str, salt)
explode (sep, str)
implode (glue, arr)
nl2br (str)
sprintf (frmt, args)
strip_tags (str, allowed_tags)
str_replace (search, replace, str)
strpos (str, needle)
strrev (str)
strstr (str, needle)
strtolower (str)
strtoupper (str)
substr (string, start, len)

#PHP Filesystem Functions
clearstatcache ()
copy (source, dest)
fclose (handle)
fgets (handle, len)
file (file)
filemtime (file)
filesize (file)
file_exists (file)
fopen (file, mode)
fread (handle, len)
fwrite (handle, str)
readfile (file)
PHP Date and Time Functions
checkdate (month, day, year)
date (format, timestamp)
getdate (timestamp)
mktime (hr, min, sec, month, day, yr)
strftime (formatstring, timestamp)
strtotime (str)

# PHP Regular Expres­sions Functions
ereg (pattern, str)
split (pattern, str)
ereg_replace (pattern, replace, str)
preg_grep (pattern, arr)
preg_match (pattern, str)
preg_match_all (pattern, str, arr)
preg_replace (pattern, replace, str)
preg_split (pattern, str)

# check also : https://gist.github.com/itzikbenh/23c1f2a0ee6f9c5731a44d98097a0f6f

'''
#PHP fopen() Modes
r
Read
r+
Read and write, prepend
w
Write, truncate
w+
Read and write, truncate
a
Write, append
a+
Read and write, append

#PHP Date Formatting
Y
4 digit year (2008)
y
2 digit year (08)
F
Long month (January)
M
Short month (Jan)
m
Month ⁴ (01 to 12)
n
Month (1 to 12)
D
Short day name (Mon)
l
Long day name (Monday) (lowercase L)
d
Day ⁴ (01 to 31)
j
Day (1 to 31)
 
h
12 Hour ⁴ (01 to 12)
g
12 Hour (1 to 12)
H
24 Hour ⁴ (00 to 23)
G
24 Hour (0 to 23)
i
Minutes ⁴ (00 to 59)
s
Seconds ⁴ (00 to 59)
 
w
Day of week ¹ (0 to 6)
z
Day of year (0 to 365)
W
Week of year ² (1 to 53)
t
Days in month (28 to 31)
 
a
am or pm
A
AM or PM
B
Swatch Internet Time (000 to 999)
S
Ordinal Suffix (st, nd, rd, th)
 
T
Timezone of machine (GMT)
Z
Timezone offset (seconds)
O
GMT offset (hours) (+0200)
I
Daylight saving (1 or 0)
L
Leap year (1 or 0)
 
U
Seconds since Epoch ³
c
ISO 8601 (PHP 5) (2008-­07-­31T­18:­30:­13+­01:00)
r
RFC 2822 (Thu, 31 Jul 2008 18:30:13 +0100)
¹ 0 is Sunday, 6 is Saturday.
² Week that overlaps two years belongs to year that contains most days of that week. Hence week number for 1st January of a given year can be 53 if week belongs to previous year. date("W­", mktime(0, 0, 0, 12, 8, $year)) always gives correct number of weeks in $year.
³ The Epoch is the 1st January 1970.
⁴ With leading zeroes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment