Created
June 15, 2014 15:00
-
-
Save ilovejs/448cc4ae217768243355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------------------------ | |
String: | |
$mystr = "Jello, world?"; | |
$mystr{0} = "H"; | |
$mystr{12} = "!"; | |
print $mystr; | |
------------------------------ | |
Octal: | |
$octalnum = 06333; | |
print $octalnum; | |
------------------------------ | |
Hexadecimal: | |
$hexnum = 0x44; | |
print $hexnum; | |
------------------------------ | |
Avoid comparing floating-point numbers if possible: | |
$c = 1.1e15; => 1.1E+015 | |
$d = (0.1+0.7) * 10; => 7.9999999999999991 | |
------------------------------ | |
Automatic Type Conversion: | |
$mystring = "12"; | |
$myinteger = 20; | |
print $mystring + $myinteger; | |
That script will output 32 | |
------------------------------ | |
$bool = true; | |
print "Bool is set to $bool\n"; | |
$bool = false; | |
print "Bool is set to $bool\n"; | |
That will output the following: | |
Bool is set to 1 | |
Bool is set to | |
------------------------------ | |
$bool = true; | |
print "Bool is set to $bool\n"; | |
$bool = false; | |
print "Bool is set to "; | |
print (int)$bool; | |
------------------------------ | |
$mystring = "wombat"; | |
$myinteger = (integer)$mystring | |
------------------------------ | |
Checking Whether a Variable Is Set: isset() | |
Variable Variables: | |
$bar = 10; | |
$foo = "bar" | |
$$foo; | |
------------------------------ | |
$_GET | |
Contains all variables sent via a HTTP GET request. For example, a URL of myfile. | |
php?name=Paul would load myfile.php and give you $_GET["name"] with the value “Paul”. | |
Users of older PHP versions will have used $HTTP_GET_VARS array, which, although deprecated, is | |
still available for use. | |
$_POST | |
Contains all variables sent via a HTTP POST request. This is similar to the old $HTTP_POST_VARS | |
array, which, although deprecated, is still available for use. | |
$_FILES Contains all variables sent via a HTTP POST file upload. This is similar to the old $HTTP_POST_ | |
FILES array, which is also deprecated. | |
$_COOKIE Contains all variables sent via HTTP cookies. This is similar to the old $HTTP_COOKIE_VARS array, | |
which is deprecated like the rest. See Chapter 10 for more information on cookies. | |
$_REQUEST ContainsallvariablessentviaHTTPGET,HTTPPOST,andHTTPcookies.Thisisbasicallytheequivalent | |
of combining $_GET, $_POST, and $_COOKIE, and is less dangerous than using $GLOBALS. | |
However, as it does contain all variables from untrusted sources (that is, your visitors), it is best | |
avoided. There’s no equivalent to $_REQUEST in versions of PHP before v4.1. | |
$_SESSION Contains all variables stored in a user’s session (server-side data store). This is similar to the old | |
$HTTP_SESSION_VARS array, which is deprecated. See Chapter 10 for more information on | |
sessions. | |
$_SERVER Contains all variables set by the web server you are using, or other sources that directly relate to the | |
execution of your script (see examples in the next section). This is similar to the old $HTTP_ | |
SERVER_VARS array, which is deprecated. | |
$_ENV Contains all environment variables set by your system or shell for the script (see examples in the next | |
section). This is similar to the old $HTTP_ENV_VARS array, which is deprecated. | |
$GLOBALS An array containing all global variables in your script, including other superglobals. $GLOBALS has | |
been available since PHP 3, and its operation has not changed. | |
--------------------------- | |
Useful preset variables in the $_SERVER superglobal: | |
HTTP_REFERER If the user clicked a link to get the current page, this will contain the URL of the previous page, or it will be empty if the user entered the URL directly. | |
HTTP_USER_AGENT The name reported by the visitor’s web browser. | |
PATH_INFO Any data passed in the URL after the script name. | |
PHP_SELF The name of the current script. | |
REQUEST_METHOD Either GET or POST. | |
QUERY_STRING Includes everything after the question mark in a GET request. Not available on the command line. | |
-------------------------- | |
<?php | |
if (isset($_SERVER['HTTP_REFERER'])) { | |
print "The page you were on previously was {$_SERVER['HTTP_ | |
REFERER']}<br />"; | |
} else { | |
print "You didn't click any links to get here<br />"; | |
} | |
?> | |
-------------------- | |
Reference: | |
$a = 10; | |
$b =& $a; | |
print $a; | |
print $b; | |
++$a; | |
print $a; | |
print $b; | |
++$b; | |
print $a; | |
print $b; | |
-------------------- | |
define("SecondsPerDay", 86400); | |
print SecondsPerDay; | |
Note that it is not $SecondsPerDay or SECONDSPERDAY | |
define("SecondsPerDay", 86400, true); // case-insensitive: | |
//print constant | |
print SecondsPerDay; | |
print SECONDSperDAY; | |
Usage: | |
define("SecondsPerDay", 86400, true); | |
if (defined("Secondsperday")) { | |
// etc | |
} | |
The defined( ) function is basically the constant equivalent of isset( ) | |
-------------------- | |
define("SecondsPerDay", 86400, true); | |
$somevar = "Secondsperday"; | |
print constant($somevar); | |
-------------------- | |
__FILE__ | |
Thenameofthescriptthat’srunning.Notethatthisreportsthefilethatcontainsthecurrentline | |
of code, so this will report the name of an include file if applicable. | |
__LINE__ The line number PHP is executing. Like __FILE__, this holds the line number of the current | |
line of code, which may be in an include file if applicable. | |
__FUNCTION__ The name of the function PHP is currently inside | |
__CLASS__ The name of the class of the object being used | |
__METHOD__ The name of the class function PHP is currently inside | |
-------------------- | |
Math: | |
$area = M_PI * pow($radius, 2); | |
M_PI 3.14159265358979323846 pi | |
M_PI_2 1.57079632679489661923 pi/2 | |
M_PI_4 0.78539816339744830962 pi/4 | |
M_1_PI 0.31830988618379067154 1/pi | |
M_2_PI 0.63661977236758134308 2/pi | |
M_SQRTPI 1.77245385090551602729 sqrt(M_PI) | |
M_2_SQRTPI 1.12837916709551257390 2/sqrt(M_PI) | |
M_SQRT2 1.41421356237309504880 sqrt(2) | |
M_SQRT3 1.73205080756887729352 sqrt(3) | |
M_SQRT1_2 0.70710678118654752440 1/sqrt(2) | |
------------------- | |
Array: | |
iteratively outputs all elements inside the array | |
$myarray = array("Apples", "Oranges", "Pears"); | |
$size = count($myarray); | |
print_r($myarray); | |
//output back as its return value, and not print anything out. | |
$myarray = array("Apples", "Oranges", "Pears"); | |
$size = count($myarray); | |
$output = print_r($myarray, true); | |
print $output; | |
------------------ | |
var_export( ) | |
it prints out variable information in a style that can be used as PHP code. | |
var_dump( ) | |
a) prints out sizes of variables, | |
b) does not print out nonpublic data in objects | |
c) does not have the option to pass a second parameter to return its output. | |
array(3) { | |
[0]=> | |
string(6) "Apples" | |
[1]=> | |
string(7) "Oranges" | |
[2]=> | |
string(5) "Pears" | |
} | |
Associative Arrays: | |
$myarray = array("a"=>"Apples", "b"=>"Oranges", "c"=>"Pears"); | |
var_dump($myarray); | |
array(3) { | |
["a"]=> | |
string(6) "Apples" | |
["b"]=> | |
string(7) "Oranges" | |
["c"]=> | |
string(5) "Pears" | |
} | |
//what if floating point index: | |
$myarr = array("1.5"=>"foo", "1.6"=>"bar"); | |
var_dump($array); | |
array(2) { | |
["1.5"]=> | |
string(3) "foo" | |
["1.6"]=> | |
string(3) "bar" | |
} | |
------------------ | |
non-index array insert: | |
$array[ ] = "Foo"; | |
$array[ ] = "Bar"; | |
$array[ ] = "Baz"; | |
var_dump($array); | |
$array["a"] = "Foo"; | |
$array["b"] = "Bar"; | |
======================= | |
function load_member_data($ID, &$member) { | |
// this would connect to a database and load the data, | |
// but for space reasons this is done by hand! | |
$member["Name"] = "Bob"; | |
return true; | |
} | |
$ID = 22901221079; | |
$result = load_member_data($ID, $member); | |
// pass $member in for data storage, but get a return value too | |
if ($result) { | |
print "Member {$member["Name"]} loaded successfully.\n"; | |
} else { | |
print "Failed to load member #$ID.\n"; | |
} | |
======================= | |
array diff: | |
$toppings1 = array("Pepperoni", "Cheese", "Anchovies", "Tomatoes"); | |
$toppings2 = array("Ham", "Cheese", "Peppers"); | |
$diff_toppings = array_diff($toppings1, $toppings2); | |
array_filter: | |
function endswithy($value) { | |
return (substr($value, -1) = = 'y'); | |
} | |
$people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Joe"); | |
$withy = array_filter($people, "endswithy"); | |
var_dump($withy); | |
// contains "Johnny", "Timmy", "Bobby", and "Tammy" | |
array_flip: | |
$capitalcities['England'] = 'London'; | |
$capitalcities['Scotland'] = 'Edinburgh'; | |
$capitalcities['Wales'] = 'Cardiff'; | |
$flippedcities = array_flip($capitalcities); | |
var_dump($flippedcities); | |
The output is this: | |
array(3) { | |
["London"]=> | |
string(7) "England" | |
["Edinburgh"]=> | |
string(8) "Scotland" | |
["Cardiff"]=> | |
string(5) "Wales" | |
} | |
array_intersect: | |
$toppings1 = array("Pepperoni", "Cheese", "Anchovies", "Tomatoes"); | |
$toppings2 = array("Ham", "Cheese", "Peppers"); | |
$int_toppings = array_intersect($toppings1, $toppings2); | |
$arr1 = array("Paul"=>25, "Ildiko"=>38, "Nick"=>27); | |
$arr2 = array("Ildiko"=>27, "Paul"=>38); | |
print "\nIntersect:\n"; | |
var_dump(array_intersect($arr1, $arr2)) | |
array_keys: | |
$users[923] = 'TelRev'; | |
$users[100] = 'Skellington'; | |
$users[1202] = 'CapnBlack'; | |
$userids = array_keys($users); | |
$users[923] = 'TelRev'; | |
$users[100] = 'Skellington'; | |
$users[1202] = 'CapnBlack'; | |
$userids = array_keys($users, "TelRev"); | |
// userids contains only 923 | |
======================= | |
$toppings1 = array("Pepperoni", "Cheese", "Anchovies", "Tomatoes"); | |
$toppings2 = array("Ham", "Cheese", "Peppers"); | |
$both_toppings = array_merge($toppings1, $toppings2); | |
var_dump($both_toppings); | |
// prints: array(7) { [0]=> string(9) "Pepperoni" [1]=> | |
// string(6) "Cheese" [2]=> string(9) "Anchovies" [3]=> | |
// string(8) "Tomatoes" [4]=> string(3) "Ham" [5]=> | |
// string(6) "Cheese" [6]=> string(7) "Peppers" } | |
$arr1 = array("Paul"=>25, "Ildiko"=>38, "Nick"=>27); | |
$arr2 = array("Ildiko"=>27, "Paul"=>38); | |
print "Merge:\n"; | |
var_dump(array_merge($arr1, $arr2)); | |
// Values 27 and 38 clash, so their keys from $arr2 are used. | |
// So, output is Paul (38), Ildiko (27), and Nick (27). | |
You can merge several arrays simultaneously by providing more parameters to the | |
function. For example: | |
$sports_teams = array_merge($soccer, $baseball, $basketball, $hockey); | |
======================= | |
array_push( ) | |
array_rand( ) | |
array_shift( ) | |
array_unique( ) | |
array_unshift( ) | |
array_values( ) | |
arsort( ) | |
asort( ) | |
explode( ) | |
extract( ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment