summaryrefslogtreecommitdiffhomepage
path: root/php.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'php.html.markdown')
-rw-r--r--php.html.markdown121
1 files changed, 98 insertions, 23 deletions
diff --git a/php.html.markdown b/php.html.markdown
index 39ec5aef..02249cd3 100644
--- a/php.html.markdown
+++ b/php.html.markdown
@@ -12,7 +12,7 @@ This document describes PHP 5+.
<?php // PHP code must be enclosed with <?php tags
// If your php file only contains PHP code, it is best practice
-// to omit the php closing tag.
+// to omit the php closing tag to prevent accidental output.
// Two forward slashes start a one-line comment.
@@ -53,6 +53,8 @@ $int1 = 12; // => 12
$int2 = -12; // => -12
$int3 = 012; // => 10 (a leading 0 denotes an octal number)
$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal)
+// Binary integer literals are available since PHP 5.4.0.
+$int5 = 0b11111111; // 255 (a leading 0b denotes a binary number)
// Floats (aka doubles)
$float = 1.234;
@@ -103,6 +105,9 @@ END;
// String concatenation is done with .
echo 'This string ' . 'is concatenated';
+// Strings can be passed in as parameters to echo
+echo 'Multiple', 'Parameters', 'Valid'; // Returns 'MultipleParametersValid'
+
/********************************
* Constants
@@ -113,10 +118,12 @@ echo 'This string ' . 'is concatenated';
// a valid constant name starts with a letter or underscore,
// followed by any number of letters, numbers, or underscores.
-define("FOO", "something");
+define("FOO", "something");
+
+// access to a constant is possible by calling the choosen name without a $
+echo FOO; // Returns 'something'
+echo 'This outputs ' . FOO; // Returns 'This ouputs something'
-// access to a constant is possible by direct using the choosen name
-echo 'This outputs '.FOO;
/********************************
@@ -141,6 +148,8 @@ echo $array[0]; // => "One"
// Add an element to the end of an array
$array[] = 'Four';
+// or
+array_push($array, 'Five');
// Remove element from array
unset($array[3]);
@@ -155,9 +164,9 @@ echo('Hello World!');
print('Hello World!'); // The same as echo
-// echo is actually a language construct, so you can drop the parentheses.
+// echo and print are language constructs too, so you can drop the parentheses
echo 'Hello World!';
-print 'Hello World!'; // So is print
+print 'Hello World!';
$paragraph = 'paragraph';
@@ -215,7 +224,11 @@ assert($a !== $d);
assert(1 === '1');
assert(1 !== '1');
-// spaceship operator since PHP 7
+// 'Spaceship' operator (since PHP 7)
+// Returns 0 if values on either side are equal
+// Returns 1 if value on the left is greater
+// Returns -1 if the value on the right is greater
+
$a = 100;
$b = 1000;
@@ -379,7 +392,7 @@ for ($i = 0; $i < 5; $i++) {
// Define a function with "function":
function my_function () {
- return 'Hello';
+ return 'Hello';
}
echo my_function(); // => "Hello"
@@ -388,8 +401,8 @@ echo my_function(); // => "Hello"
// number of letters, numbers, or underscores.
function add ($x, $y = 1) { // $y is optional and defaults to 1
- $result = $x + $y;
- return $result;
+ $result = $x + $y;
+ return $result;
}
echo add(4); // => 5
@@ -400,21 +413,21 @@ echo add(4, 2); // => 6
// Since PHP 5.3 you can declare anonymous functions;
$inc = function ($x) {
- return $x + 1;
+ return $x + 1;
};
echo $inc(2); // => 3
function foo ($x, $y, $z) {
- echo "$x - $y - $z";
+ echo "$x - $y - $z";
}
// Functions can return functions
function bar ($x, $y) {
- // Use 'use' to bring in outside variables
- return function ($z) use ($x, $y) {
- foo($x, $y, $z);
- };
+ // Use 'use' to bring in outside variables
+ return function ($z) use ($x, $y) {
+ foo($x, $y, $z);
+ };
}
$bar = bar('A', 'B');
@@ -426,6 +439,31 @@ echo $function_name(1, 2); // => 3
// Useful for programatically determining which function to run.
// Or, use call_user_func(callable $callback [, $parameter [, ... ]]);
+
+// You can get the all the parameters passed to a function
+function parameters() {
+ $numargs = func_num_args();
+ if ($numargs > 0) {
+ echo func_get_arg(0) . ' | ';
+ }
+ $args_array = func_get_args();
+ foreach ($args_array as $key => $arg) {
+ echo $key . ' - ' . $arg . ' | ';
+ }
+}
+
+parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World |
+
+// Since PHP 5.6 you can get a variable number of arguments
+function variable($word, ...$list) {
+ echo $word . " || ";
+ foreach ($list as $item) {
+ echo $item . ' | ';
+ }
+}
+
+variable("Separate", "Hello", "World") // Separate || Hello | World |
+
/********************************
* Includes
*/
@@ -693,6 +731,43 @@ use My\Namespace as SomeOtherNamespace;
$cls = new SomeOtherNamespace\MyClass();
+
+/**********************
+* Late Static Binding
+*
+*/
+
+class ParentClass {
+ public static function who() {
+ echo "I'm a " . __CLASS__ . "\n";
+ }
+ public static function test() {
+ // self references the class the method is defined within
+ self::who();
+ // static references the class the method was invoked on
+ static::who();
+ }
+}
+
+ParentClass::test();
+/*
+I'm a ParentClass
+I'm a ParentClass
+*/
+
+class ChildClass extends ParentClass {
+ public static function who() {
+ echo "But I'm " . __CLASS__ . "\n";
+ }
+}
+
+ChildClass::test();
+/*
+I'm a ParentClass
+But I'm ChildClass
+*/
+
+
/**********************
* Error Handling
*
@@ -702,15 +777,15 @@ $cls = new SomeOtherNamespace\MyClass();
try {
// Do something
-} catch ( Exception $e) {
+} catch (Exception $e) {
// Handle exception
}
// When using try catch blocks in a namespaced enviroment use the following
-try {
+try {
// Do something
-} catch (\Exception $e) {
+} catch (\Exception $e) {
// Handle exception
}
@@ -719,13 +794,13 @@ try {
class MyException extends Exception {}
try {
-
- $condition = true;
-
+
+ $condition = true;
+
if ($condition) {
throw new MyException('Something just happend');
}
-
+
} catch (MyException $e) {
// Handle my exception
}