PHP-Cheat-Sheet.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Beginner’s Essential PHP CHEAT SHEET Fast, flexible and pragmatic scripting language. Table of Contents PHP Basics 2 Variables and Constants 2 PHP Arrays - Grouped Values 7 PHP Strings 13 PHP Operators 21 Loops in PHP 23 Conditional Statements 23 Working with Forms in PHP 24 Regular Expressions 26 PHP Functions 28 PHP Filters 29 HTTP Functions in PHP 31 Working with MySQL 31 Date and Time 36 PHP Errors 40 WebsiteSetup.org - Beginner’s PHP Cheat Sheet 1 PHP BASICS Including PHP in a File <?php // place PHP code here ?> Writing Comments // Denotes comments that only span on one line # Another way of producing single-line comments /*...*/ Everything between /* and */ is not executed, also works across several lines Outputting Data <?php echo "<h1>PHP Cheat Sheet</h1>"; ?> Writing PHP Functions function NameOfTheFunction() { //place PHP code here } VARIABLES AND CONSTANTS Defining Variables <?php $BlogPostTitle = "PHP Cheat Sheet"; ?> WebsiteSetup.org - Beginner’s PHP Cheat Sheet 2 Types of Data Integers Integers are non-decimal numbers between -2,147,483,648 and 2,147,483,647. They must have at least one digit and no decimal point. Can be in decimal, hexadecimal or octal. Floats This is the name for numbers with a decimal point or in exponential form. Strings This simply means text, we will talk about it in detail further below. Boolean values Meaning true/false statements. Arrays Arrays are variables that store several values. We will talk about them in detail further below. Objects Objects store both data and information on how to process it. Resources These are references to functions and resources outside of PHP. NULL A variable that is NULL doesn’t have any value. Variable Scope function myFunction() { global $a, $b; $b = $a - $b; } WebsiteSetup.org - Beginner’s PHP Cheat Sheet 3 Predefined Variables $GLOBALS Used to access global variables from anywhere inside a PHP script. $_SERVER Contains information about the locations of headers, paths and scripts. $_GET Can collect data that was sent in the URL or submitted in an HTML form. $_POST Used to gather data from an HTML form and to pass variables. $_REQUEST Also collects data after submitting an HTML form Variable-handling Functions boolval Used to retrieve the boolean value of a variable debug_zval_dump Outputs a string representation of an internal zend value empty Checks whether a variable is empty or not floatval Get the float value of a variable (doubleval is another possibility) get_defined_vars Returns an array of all defined variables get_resource_type Returns the resource type gettype Retrieves the variable type WebsiteSetup.org - Beginner’s PHP Cheat Sheet 4 import_request_variables Import GET/POST/Cookie variables into the global scope intval Find the integer value of a variable is_array Checks whether a variable is an array is_bool Finds out if a variable is a boolean of 538 is_callable Verify whether you can call the contents of a variable as a function is_countable Check whether the contents of a variable are countable is_float Find out if the type of a variable is float, alternatives: is_double and is_real is_int Check if the type of a variable is an integer, is_integer and is_long also works is_iterable Verify that a variable’s content is an iterable value is_null Checks whether a variable’s value is NULL is_numeric Find out if a variable is a number or a numeric string is_object Determines whether a variable is an object is_resource Check if a variable is a resource is_scalar Tests if a variable is a scalar WebsiteSetup.org - Beginner’s PHP Cheat Sheet 5 is_string Find out whether the type of a variable is a string isset Determine if a variable has been set and is not NULL print_r Provides human-readable information about a variable serialize Generates a representation of a value that is storable settype Sets a variable’s type strval Retrieves the string value of a variable unserialize Creates a PHP value from a stored representation unset Unsets a variable var_dump Dumps information about a variable var_export Outputs or returns a string representation of a variable that can be parsed Constants define(name, value, true/false) Aside from user-defined constants, there also a number of default PHP constants: __LINE__ Denotes the number of the current line in a file __FILE__ Is the full path and filename of the file WebsiteSetup.org - Beginner’s PHP Cheat Sheet 6 __DIR__ The directory of the file __FUNCTION__ Name of the function __CLASS__ Class name, includes the namespace it was declared in __TRAIT__ The trait name, also includes the namespace __METHOD__ The class method name __NAMESPACE__ Name of the current namespace PHP ARRAYS – GROUPED VALUES Indexed arrays Arrays that have a numeric index Associative arrays Arrays where the keys are named Multidimensional arrays Arrays that contain one or more other arrays Declaring an Array in PHP <?php $cms = array("WordPress", "Joomla", "Drupal"); echo "What is your favorite CMS? Is it " . $cms[0] . ", " . $cms[1] . " or " . $cms[2] . "?"; ?> Array Functions array_change_key_case Changes all keys in an array to uppercase or lowercase WebsiteSetup.org - Beginner’s PHP Cheat Sheet 7 array_chunk Splits an array into chunks array_column Retrieves the values from a single column in an array array_combine Merges the keys from one array and the values from another into a new array array_count_values Counts all values in an array array_diff Compares arrays, returns the difference (values only array_diff_assoc Compares arrays, returns the difference (values and keys) array_diff_key Compares arrays, returns the difference (keys only) array_diff_uassoc Compares arrays (keys and values) through a user callback function array_diff_ukey Compares arrays (keys only) through a user callback function array_fill Fills an array with values array_fill_keys Fills an array with values, specifying keys array_filter Filters the elements of an array via a callback function array_flip Exchanges all keys in an array with their associated values array_intersect Compare arrays and return their matches (values only) WebsiteSetup.org - Beginner’s PHP Cheat Sheet 8 array_intersect_assoc Compare arrays and return their matches (keys and values) array_intersect_key Compare arrays and return their matches (keys only) array_intersect_uassoc Compare arrays via a user-defined callback function (keys and values) array_intersect_ukey Compare arrays via a user-defined callback function (keys only) array_key_exists Checks if a specified key exists in an array, alternative: key_exists array_keys Returns all keys or a subset of keys in an array array_map Applies a callback to the elements of a given array array_merge Merge one or several arrays array_merge_recursive Merge one or more arrays recursively array_multisort Sorts multiple or multi-dimensional arrays array_pad Inserts a specified number of items (with a specified value) into an array array_pop Deletes an element from the end of an array array_product Calculate the product of all values in an array array_push Push one or several elements to the end of the array WebsiteSetup.org - Beginner’s PHP Cheat Sheet 9 array_rand Pick one or more random entries out of an array array_reduce Reduce the array to a single string using a user-defined function array_replace Replaces elements in the first array with values from following arrays array_replace_recursive Recursively replaces elements from later arrays into the first array array_reverse Returns an array in reverse order array_search Searches the array for a given value and returns the first key if successful array_shift Shifts an element from the beginning of an array array_slice Extracts a slice of an array array_splice Removes a portion of the array and replaces it array_sum Calculate the sum of the values in an array array_udiff Compare arrays and return the difference using a user function (values only) array_udiff_assoc Compare arrays and return the difference using a default and a user function (keys and values) array_udiff_uassoc Compare arrays and return the difference using two user functions (values and keys) array_uintersect WebsiteSetup.org - Beginner’s PHP Cheat Sheet 1 0 Compare arrays and return the matches via user function (values only) array_uintersect_assoc Compare arrays and return the matches via a default user function (keys and values) array_uintersect_uassoc Compare arrays and return the matches via two user functions (keys and values) array_unique Removes duplicate values from an array array_unshift Adds one or more elements to the beginning of an array array_values Returns all values of an array array_walk Applies a user function to every element in an array array_walk_recursive Recursively applies a user function to every element of an array arsort Sorts an associative array in descending order according to the value asort Sorts an associative array in ascending order according to the value compact Create an array containing variables and their values count Count all elements in an array, alternatively use sizeof current Returns the current element in an array, an alternative is pos each Return the current key and value pair from an array end WebsiteSetup.org - Beginner’s PHP Cheat Sheet 1 1 Set the internal pointer to the last element of an array extract Import variables from an array into the current symbol table in_array Checks if a value exists in an arraykeyFetches a key from an array krsort Sorts an associative array by key in reverse order ksort Sorts an associative array by key