summaryrefslogtreecommitdiffhomepage
path: root/php.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'php.html.markdown')
-rw-r--r--php.html.markdown51
1 files changed, 39 insertions, 12 deletions
diff --git a/php.html.markdown b/php.html.markdown
index ce178a15..f247ba77 100644
--- a/php.html.markdown
+++ b/php.html.markdown
@@ -88,6 +88,8 @@ $escaped = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';
// Enclose a variable in curly braces if needed
+$apples = "I have {$number} apples to eat.";
+$oranges = "I have ${number} oranges to eat.";
$money = "I have $${number} in the bank.";
// Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners
@@ -142,6 +144,9 @@ $associative = ['One' => 1, 'Two' => 2, 'Three' => 3];
echo $associative['One']; // prints 1
+// Add an element to an associative array
+$associative['Four'] = 4;
+
// List literals implicitly assign integer keys
$array = ['One', 'Two', 'Three'];
echo $array[0]; // => "One"
@@ -278,7 +283,7 @@ if (false) {
if (false) {
print 'Does not get printed';
-} elseif(true) {
+} elseif (true) {
print 'Does';
}
@@ -300,7 +305,7 @@ echo $b ?? 'b is not set'; // prints 'Does print'
$x = 0;
if ($x === '0') {
print 'Does not print';
-} elseif($x == '1') {
+} elseif ($x == '1') {
print 'Does not print';
} else {
print 'Does print';
@@ -337,7 +342,7 @@ switch ($x) {
$i = 0;
while ($i < 5) {
echo $i++;
-}; // Prints "01234"
+} // Prints "01234"
echo "\n";
@@ -462,7 +467,7 @@ function variable($word, ...$list) {
}
}
-variable("Separate", "Hello", "World") // Separate || Hello | World |
+variable("Separate", "Hello", "World"); // Separate || Hello | World |
/********************************
* Includes
@@ -525,7 +530,8 @@ class MyClass
private $priv = 'private'; // Accessible within the class only
// Create a constructor with __construct
- public function __construct($instanceProp) {
+ public function __construct($instanceProp)
+ {
// Access instance variables with $this
$this->instanceProp = $instanceProp;
}
@@ -536,11 +542,26 @@ class MyClass
print 'MyClass';
}
- //final keyword would make a function unoverridable
+ // final keyword would make a function unoverridable
final function youCannotOverrideMe()
{
}
+ // Magic Methods
+
+ // what to do if Object is treated as a String
+ public function __toString()
+ {
+ return $property;
+ }
+
+ // opposite to __construct()
+ // called when object is no longer referenced
+ public function __destruct()
+ {
+ print "Destroying";
+ }
+
/*
* Declaring class properties or methods as static makes them accessible without
* needing an instantiation of the class. A property declared as static can not
@@ -737,11 +758,15 @@ $cls = new SomeOtherNamespace\MyClass();
*
*/
-class ParentClass {
- public static function who() {
+class ParentClass
+{
+ public static function who()
+ {
echo "I'm a " . __CLASS__ . "\n";
}
- public static function test() {
+
+ public static function test()
+ {
// self references the class the method is defined within
self::who();
// static references the class the method was invoked on
@@ -755,8 +780,10 @@ I'm a ParentClass
I'm a ParentClass
*/
-class ChildClass extends ParentClass {
- public static function who() {
+class ChildClass extends ParentClass
+{
+ public static function who()
+ {
echo "But I'm " . __CLASS__ . "\n";
}
}
@@ -797,7 +824,7 @@ echo "Current method is " . __METHOD__;
echo "Current namespace is " . __NAMESPACE__;
// Get the name of the current trait. Only returns a value when used inside a trait or object declaration.
-echo "Current namespace is " . __TRAIT__;
+echo "Current trait is " . __TRAIT__;
/**********************
* Error Handling