diff options
-rw-r--r-- | README.markdown | 1 | ||||
-rw-r--r-- | c.html.markdown | 7 | ||||
-rw-r--r-- | clojure.html.markdown | 7 | ||||
-rw-r--r-- | dart.html.markdown | 3 | ||||
-rw-r--r-- | file.erb | 1 | ||||
-rw-r--r-- | fsharp.html.markdown | 1 | ||||
-rw-r--r-- | haskell.html.markdown | 28 | ||||
-rw-r--r-- | lua.html.markdown | 1 | ||||
-rw-r--r-- | php.html.markdown | 640 | ||||
-rw-r--r-- | python.html.markdown | 3 | ||||
-rw-r--r-- | r.html.markdown | 28 |
11 files changed, 409 insertions, 311 deletions
diff --git a/README.markdown b/README.markdown index 3223a2bd..77e09abd 100644 --- a/README.markdown +++ b/README.markdown @@ -17,7 +17,6 @@ properly! The most requested languages are: * Scala -* Python * Javascript ... but there are many more requests to do "every language", so don't let that stop you. diff --git a/c.html.markdown b/c.html.markdown index f2b9047b..69bf099e 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -2,6 +2,7 @@ language: c author: Adam Bard author_url: http://adambard.com/ +filename: learnc.c --- Ah, C. Still the language of modern high-performance computing. @@ -12,6 +13,7 @@ memory management and C will take you as far as you need to go. ```c // Single-line comments start with // + /* Multi-line comments look like this. */ @@ -19,6 +21,7 @@ Multi-line comments look like this. // Import headers with #include #include <stdlib.h> #include <stdio.h> +#include <string.h> // Declare function signatures in advance in a .h file, or at the top of // your .c file. @@ -75,7 +78,7 @@ unsigned long long ux_long_long; // on your machine. sizeof(T) gives you the size of a variable with type T in // bytes so you can express the size of these types in a portable way. // For example, -printf("%d\n", sizeof(int)); // => 4 (on machines with 4-byte words) +printf("%lu\n", sizeof(int)); // => 4 (on machines with 4-byte words) // Arrays must be initialized with a concrete size. char my_char_array[20]; // This array occupies 1 * 20 = 20 bytes @@ -107,7 +110,7 @@ Char #17 is the NUL byte. Chars #18, 19 and 20 have undefined values. */ -printf("%d\n", a_string[16]); => 0 +printf("%d\n", a_string[16]); // => 0 /////////////////////////////////////// // Operators diff --git a/clojure.html.markdown b/clojure.html.markdown index 24250a87..12611fd3 100644 --- a/clojure.html.markdown +++ b/clojure.html.markdown @@ -2,6 +2,7 @@ language: clojure author: Adam Bard author_url: http://adambard.com/ +filename: test.clj --- Clojure is a variant of LISP developed for the Java Virtual Machine. It has @@ -59,10 +60,12 @@ and often automatically. (class false) ; Booleans are java.lang.Boolean (class nil); The "null" value is called nil -; If you want to create a literal list of data, use ' to make a "symbol" +; If you want to create a literal list of data, use ' to stop it from +; being evaluated '(+ 1 2) ; => (+ 1 2) +; (shorthand for (quote (+ 1 2)) -; You can eval symbols. +; You can eval a quoted list (eval '(+ 1 2)) ; => 3 ; Collections & Sequences diff --git a/dart.html.markdown b/dart.html.markdown index d064dc7d..27365746 100644 --- a/dart.html.markdown +++ b/dart.html.markdown @@ -1,7 +1,8 @@ --- -language: Dart +language: dart author: Joao Pedrosa author_url: https://github.com/jpedrosa/ +filename: learndart.dart --- Dart is a newcomer into the realm of programming languages. diff --git a/file.erb b/file.erb new file mode 100644 index 00000000..5f162aa5 --- /dev/null +++ b/file.erb @@ -0,0 +1 @@ +<%= rawcode %> diff --git a/fsharp.html.markdown b/fsharp.html.markdown index 1deaf437..b1860372 100644 --- a/fsharp.html.markdown +++ b/fsharp.html.markdown @@ -2,6 +2,7 @@ language: F# author: Scott Wlaschin author_url: http://fsharpforfunandprofit.com/ +filename: learnfsharp.fs --- F# is a general purpose functional/OO programming language. It's free and open source, and runs on Linux, Mac, Windows and more. diff --git a/haskell.html.markdown b/haskell.html.markdown index 563674c9..a696cb5f 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -2,6 +2,7 @@ language: haskell author: Adit Bhargava author_url: http://adit.io +filename: learnhaskell.hs --- Haskell was designed as a practical, purely functional programming language. It's famous for @@ -259,7 +260,32 @@ Just "hello" Just 1 ---------------------------------------------------- --- 8. The Haskell REPL +-- 8. Haskell IO +---------------------------------------------------- + +-- While IO can't be explained fully without explaining monads +-- it is not hard to explain enough to get going + +-- An IO a value is an IO action: you can chain them with do blocks +action = do + putStrLn "This is a line. Duh" + input <- getLine -- this gets a line and gives it the name "input" + input2 <- getLine + return (input1++"\n"++input2) -- This is the result of the whole action + +-- This didn't actually do anything. When a haskell program is executed +-- an IO action called "main" is read and interprete + +main = do + putStrLn "Our first program. How exciting!" + result <- action -- our defined action is just like the default ones + putStrLn result + putStrLn "This was all, folks!" + + + +---------------------------------------------------- +-- 9. The Haskell REPL ---------------------------------------------------- -- Start the repl by typing `ghci`. diff --git a/lua.html.markdown b/lua.html.markdown index 66ebf6bd..4df57a92 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -2,6 +2,7 @@ language: lua author: Tyler Neylon author_url: http://tylerneylon.com/ +filename: learnlua.lua --- ```lua diff --git a/php.html.markdown b/php.html.markdown index 339499eb..20923548 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -2,19 +2,16 @@ language: php author: Malcolm Fell author_url: http://emarref.net/ +filename: learnphp.php --- This document describes PHP 5+. -## [Basic Syntax](http://www.php.net/manual/en/language.basic-syntax.php) - -All statements must end with a semi-colon; All PHP code must be between <?php and ?> tags. PHP can also be -configured to respect the [short open tags](http://www.php.net/manual/en/ini.core.php#ini.short-open-tag) <? and ?>. - -## [Comments](http://www.php.net/manual/en/language.basic-syntax.comments.php) - ```php -<?php +<?php // PHP code must be enclosed with <?php ? > tags + +// If your php file only contains PHP code, it is best practise +// to omit the php closing tag. // Two forward slashes start a one-line comment. @@ -24,27 +21,36 @@ configured to respect the [short open tags](http://www.php.net/manual/en/ini.cor Surrounding text in slash-asterisk and asterisk-slash makes it a multi-line comment. */ -``` - -## [Types](http://www.php.net/manual/en/language.types.php) -Types are [weakly typed](http://en.wikipedia.org/wiki/Strong_and_weak_typing) and begin with the $ symbol. -A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. +// Use "echo" or "print" to print output +print('Hello '); // Prints "Hello " with no line break -### Scalars +// () are optional for print and echo +echo "World\n"; // Prints "World" with a line break +// (all statements must end with a semicolon) -```php +// Anything outside <?php tags is echoed automatically +?>Hello World Again! <?php + +/************************************ + * Types & Variables + */ + +// Variables begin with the $ symbol. +// A valid variable name starts with a letter or underscore, +// followed by any number of letters, numbers, or underscores. + // Boolean values are case-insensitive -$boolean = true; // or TRUE or True +$boolean = true; // or TRUE or True $boolean = false; // or FALSE or False // Integers -$integer = 1234; // decimal number -$integer = -123; // a negative number -$integer = 0123; // octal number (equivalent to 83 decimal) -$integer = 0x1A; // hexadecimal number (equivalent to 26 decimal) +$int1 = 19; // => 19 +$int2 = -19; // => -19 +$int3 = 019; // => 15 (a leading 0 denotes an octal number) +$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal) // Floats (aka doubles) $float = 1.234; @@ -52,28 +58,30 @@ $float = 1.2e3; $float = 7E-10; // Arithmetic -$sum = $number + $float; -$difference = $number - $float; -$product = $number * $float; -$quotient = $number / $float; +$sum = 1 + 1; // 2 +$difference = 2 - 1; // 1 +$product = 2 * 2; // 4 +$quotient = 2 / 1; // 2 // Shorthand arithmetic -$number += 1; // Add 1 to $number -$number++; // Add 1 to $number after it is used -++$number; // Add 1 to $number before it is used. -$number /= $float // Divide and assign the quotient to $number +$number = 0; +$number += 1; // Increment $number by 1 +echo $number++; // Prints 1 (increments after evaluation) +echo ++$number; // Prints 3 (increments before evalutation) +$number /= $float; // Divide and assign the quotient to $number // Strings should be enclosed in single quotes; $sgl_quotes = '$String'; // => '$String' // Avoid using double quotes except to embed other variables -$dbl_quotes = "This is a $sgl_quotes." // => 'This is a $String' +$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.' -// Escape special characters with backslash -$escaped = "This contains a \t tab character."; +// Special characters are only escaped in double quotes +$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 -$money = "I have $${integer} in the bank." +$money = "I have $${number} in the bank."; // Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners $nowdoc = <<<'END' @@ -81,35 +89,40 @@ Multi line string END; +// Heredocs will do string interpolation $heredoc = <<<END Multi line $sgl_quotes -END; // Nowdoc syntax is available in PHP 5.3.0 +END; -// Manipulation -$concatenated = $sgl_quotes . $dbl_quotes; -``` +// String concatenation is done with . +echo 'This string ' . 'is concatenated'; -### Compound -```php -<?php +/******************************** + * Arrays + */ -// Arrays -$array = array(1, 2, 3); -$array = [1, 2, 3]; // As of PHP 5.4 -$string = ["One", "Two", "Three"]; -$string[0]; // Holds the value "One"; +// All arrays in PHP are associative arrays (hashmaps), // Associative arrays, known as hashmaps in some languages. -$associative = ["One" => 1, "Two" => 2, "Three" => 3]; -$associative["One"]; // Holds the value 1 -``` -## Output +// Works with all PHP versions +$associative = array('One' => 1, 'Two' => 2, 'Three' => 3); -```php -<?php +// PHP 5.4 introduced a new syntax +$associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; + +echo $associative['One']; // prints 1 + +// List literals implicitly assign integer keys +$array = ['One', 'Two', 'Three']; +echo $array[0]; // => "One" + + +/******************************** + * Output + */ echo('Hello World!'); // Prints Hello World! to stdout. @@ -121,133 +134,129 @@ print('Hello World!'); // The same as echo echo 'Hello World!'; print 'Hello World!'; // So is print -echo 100; -echo $variable; -echo function_result(); +$paragraph = 'paragraph'; + +echo 100; // Echo scalar variables directly +echo $paragraph; // or variables // If short open tags are configured, or your PHP version is // 5.4.0 or greater, you can use the short echo syntax -<?= $variable ?> -``` - -## [Operators](http://www.php.net/manual/en/language.operators.php) - -### Assignment - -```php +?> +<p><?= $paragraph ?></p> <?php $x = 1; $y = 2; -$x = $y; // A now contains the same value sa $y -$x = &$y; -// $x now contains a reference to $y. Changing the value of -// $x will change the value of $y also, and vice-versa. -``` +$x = $y; // $x now contains the same value as $y +$z = &$y; +// $z now contains a reference to $y. Changing the value of +// $z will change the value of $y also, and vice-versa. +// $x will remain unchanged as the original value of $y -### Comparison +echo $x; // => 2 +echo $z; // => 2 +$y = 0; +echo $x; // => 2 +echo $z; // => 0 -```php -<?php + +/******************************** + * Logic + */ +$a = 0; +$b = '0'; +$c = '1'; +$d = '1'; + +// assert throws a warning if its argument is not true // These comparisons will always be true, even if the types aren't the same. -$a == $b // TRUE if $a is equal to $b after type juggling. -$a != $b // TRUE if $a is not equal to $b after type juggling. -$a <> $b // TRUE if $a is not equal to $b after type juggling. -$a < $b // TRUE if $a is strictly less than $b. -$a > $b // TRUE if $a is strictly greater than $b. -$a <= $b // TRUE if $a is less than or equal to $b. -$a >= $b // TRUE if $a is greater than or equal to $b. +assert($a == $b); // equality +assert($c != $a); // inequality +assert($c <> $a); // alternative inequality +assert($a < $c); +assert($c > $b); +assert($a <= $b); +assert($c >= $d); // The following will only be true if the values match and are the same type. -$a === $b // TRUE if $a is equal to $b, and they are of the same type. -$a !== $b // TRUE if $a is not equal to $b, or they are not of the same type. -1 == '1' // TRUE -1 === '1' // FALSE -``` +assert($c === $d); +assert($a !== $d); +assert(1 == '1'); +assert(1 !== '1'); -## [Type Juggling](http://www.php.net/manual/en/language.types.type-juggling.php) - -Variables can be converted between types, depending on their usage. - -```php -<?php +// Variables can be converted between types, depending on their usage. $integer = 1; -echo $integer + $integer; // Outputs 2; +echo $integer + $integer; // => 2 $string = '1'; -echo $string + $string; -// Also outputs 2 because the + operator converts the strings to integers +echo $string + $string; // => 2 (strings are coerced to integers) $string = 'one'; -echo $string + $string; +echo $string + $string; // => 0 // Outputs 0 because the + operator cannot cast the string 'one' to a number -``` -Type casting can be used to treat a variable as another type temporarily by using cast operators in parentheses. +// Type casting can be used to treat a variable as another type -```php -$boolean = (boolean) $integer; // $boolean is true +$boolean = (boolean) 1; // => true $zero = 0; -$boolean = (boolean) $zero; // $boolean is false +$boolean = (boolean) $zero; // => false +// There are also dedicated functions for casting most types $integer = 5; $string = strval($integer); -// There are also dedicated functions for casting most types $var = null; // Null value -``` -## [Control Structures](http://www.php.net/manual/en/language.control-structures.php) -### If Statements +/******************************** + * Control Structures + */ -```php -<?php - -if (/* test */) { - // Do something +if (true) { + print 'I get printed'; } -if (/* test */) { - // Do something +if (false) { + print 'I don\'t'; } else { - // Do something else + print 'I get printed'; } -if (/* test */) { - // Do something -} elseif(/* test2 */) { - // Do something else, only if test2 +if (false) { + print 'Does not get printed'; +} elseif(true) { + print 'Does'; } -if (/* test */) { - // Do something -} elseif(/* test2 */) { - // Do something else, only if test2 +$x = 0; +if ($x === '0') { + print 'Does not print'; +} elseif($x == '1') { + print 'Does not print'; } else { - // Do something default + print 'Does print'; } + +// This alternative syntax is useful for templates: ?> -<?php if (/* test */): ?> +<?php if ($x): ?> This is displayed if the test is truthy. <?php else: ?> This is displayed otherwise. <?php endif; ?> -``` - -### Switch statements -```php <?php -switch ($variable) { - case 'one': - // Do something if $variable == 'one' - break; +// Use switch to save some logic. +switch ($x) { + case '0': + print 'Switch does type coercion'; + break; // You must include a break, or you will fall through + // to cases 'two' and 'three' case 'two': case 'three': // Do something if $variable is either 'two' or 'three' @@ -256,199 +265,231 @@ switch ($variable) { // Do something by default } -``` - -### Loops - -```php -<?php - +// While, do...while and for loops are probably familiar $i = 0; while ($i < 5) { echo $i++; -} +}; // Prints "01234" + +echo "\n"; $i = 0; do { echo $i++; -} while ($i < 5); +} while ($i < 5); // Prints "01234" + +echo "\n"; for ($x = 0; $x < 10; $x++) { - echo $x; // Will echo 0 - 9 -} + echo $x; +} // Prints "0123456789" + +echo "\n"; -$wheels = ["bicycle" => 2, "car" => 4]; +$wheels = ['bicycle' => 2, 'car' => 4]; +// Foreach loops can iterate over arrays +foreach ($wheels as $wheel_count) { + echo $wheel_count; +} // Prints "24" + +echo "\n"; + +// You can iterate over the keys as well as the values foreach ($wheels as $vehicle => $wheel_count) { echo "A $vehicle has $wheel_count wheels"; } -// This loop will stop after outputting 2 +echo "\n"; + $i = 0; while ($i < 5) { - if ($i == 3) { - break; // Exit out of the while loop and continue. + if ($i === 3) { + break; // Exit out of the while loop } echo $i++; -} +} // Prints "012" -// This loop will output everything except 3 -$i = 0; -while ($i < 5) { - if ($i == 3) { +for ($i = 0; $i < 5; $i++) { + if ($i === 3) { continue; // Skip this iteration of the loop } - echo $i++; -} -``` + echo $i; +} // Prints "0124" -## Functions -Functions are created with the ```function``` keyword. - -```php -<?php +/******************************** + * Functions + */ -function my_function($my_arg) { - $my_variable = 1; +// Define a function with "function": +function my_function () { + return 'Hello'; } -// $my_variable and $my_arg cannot be accessed outside of the function -``` - -Functions may be invoked by name. +echo my_function(); // => "Hello" -```php -<?php - -my_function_name(); - -$variable = get_something(); // A function may return a value -``` +// A valid function name starts with a letter or underscore, followed by any +// number of letters, numbers, or underscores. -A valid function name starts with a letter or underscore, followed by any -number of letters, numbers, or underscores. There are three ways to declare functions. - -### [User-defined](http://www.php.net/manual/en/functions.user-defined.php) - -```php -<?php - -function my_function_name ($arg_1, $arg_2) { - // $arg_1 and $arg_2 are required +function add ($x, $y = 1) { // $y is optional and defaults to 1 + $result = $x + $y; + return $result; } -// Functions may be nested to limit scope -function outer_function ($arg_1 = null) { // $arg_1 is optional - function inner_function($arg_2 = 'two') { // $arg_2 will default to 'two' - } -} +echo add(4); // => 5 +echo add(4, 2); // => 6 -// inner_function() does not exist and cannot be called until -// outer_function() is called -``` +// $result is not accessible outside the function +// print $result; // Gives a warning. + +// Since PHP 5.3 you can declare anonymous functions; +$inc = function ($x) { + return $x + 1; +}; -This enables [currying](http://en.wikipedia.org/wiki/Currying) in PHP. +echo $inc(2); // => 3 -```php function foo ($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); }; } $bar = bar('A', 'B'); -$bar('C'); -``` +$bar('C'); // Prints "A - B - C" -### [Variable](http://www.php.net/manual/en/functions.variable-functions.php) +// You can call named functions using strings +$function_name = 'add'; +echo $function_name(1, 2); // => 3 +// Useful for programatically determining which function to run. +// Or, use call_user_func(callable $callback [, $parameter [, ... ]]); -```php -<?php - -$function_name = 'my_function_name'; +/******************************** + * Includes + */ -$function_name(); // will execute the my_function_name() function +/* ``` - -### [Anonymous](http://www.php.net/manual/en/functions.anonymous.php) - -Similar to variable functions, functions may be anonymous. - ```php <?php +// Included files must also begin with a PHP open tags. -function my_function($callback) { - $callback('My argument'); -} +include 'my-file.php'; +// The code in my-file.php is now available in the current scope. +// If the file cannot be included (e.g. file not found), a warning is emitted. -my_function(function ($my_argument) { - // do something -}); +include_once 'my-file.php'; +// If the code in my-file.php has been included elsewhere, it will +// not be included again. This prevents multiple class declaration errors -// Closure style -$my_function = function() { - // Do something -}; +require 'my-file.php'; +require_once 'my-file.php'; +// Same as include(), except require() will cause a fatal error if the +// file cannot be included. -$my_function(); -``` +// Contents of my-include.php: +<?php -## [Classes](http://www.php.net/manual/en/language.oop5.php) +return 'Anything you like.'; +// End file -Classes are defined with the ```class``` keyword. +// Includes and requires may also return a value. +$value = include 'my-include.php'; -```php -<?php +// Files are included based on the file path given or, if none is given, +// the include_path configuration directive. If the file isn't found in +// the include_path, include will finally check in the calling script's +// own directory and the current working directory before failing. +/* */ -class MyClass { - const MY_CONST = 'value'; - static $staticVar = 'something'; - public $property = 'value'; // Properties must declare their visibility -} +/******************************** + * Classes + */ -echo MyClass::MY_CONST; // Outputs "value"; +// Classes are defined with the class keyword -final class YouCannotExtendMe { -} -``` +class MyClass +{ + const MY_CONST = 'value'; // A constant -Classes are instantiated with the ```new``` keyword. Functions are referred to as -methods if they belong to a class. + static $staticVar = 'static'; -```php -<?php + // Properties must declare their visibility + public $property = 'public'; + public $instanceProp; + protected $prot = 'protected'; // Accessible from the class and subclasses + private $priv = 'private'; // Accessible within the class only + + // Create a constructor with __construct + public function __construct($instanceProp) { + // Access instance variables with $this + $this->instanceProp = $instanceProp; + } -class MyClass { - function myFunction() { + // Methods are declared as functions inside a class + public function myMethod() + { + print 'MyClass'; } - final function youCannotOverrideMe() { + final function youCannotOverrideMe() + { } - public static function myStaticMethod() { + public static function myStaticMethod() + { + print 'I am static'; } } -$cls = new MyClass(); // The parentheses are optional. +echo MyClass::MY_CONST; // Outputs 'value'; +echo MyClass::$staticVar; // Outputs 'static'; +MyClass::myStaticMethod(); // Outputs 'I am static'; -echo MyClass::$staticVar; // Access to static vars +// Instantiate classes using new +$my_class = new MyClass('An instance property'); +// The parentheses are optional if not passing in an argument. -echo $cls->property; // Access to properties +// Access class members using -> +echo $my_class->property; // => "public" +echo $my_class->instanceProp; // => "An instance property" +$my_class->myMethod(); // => "MyClass" -MyClass::myStaticMethod(); // myStaticMethod cannot be run on $cls -``` -PHP offers some [magic methods](http://www.php.net/manual/en/language.oop5.magic.php) for classes. +// Extend classes using "extends" +class MyOtherClass extends MyClass +{ + function printProtectedProperty() + { + echo $this->prot; + } -```php -<?php + // Override a method + function myMethod() + { + parent::myMethod(); + print ' > MyOtherClass'; + } +} + +$my_other_class = new MyOtherClass('Instance prop'); +$my_other_class->printProtectedProperty(); // => Prints "protected" +$my_other_class->myMethod(); // Prints "MyClass > MyOtherClass" + +final class YouCannotExtendMe +{ +} -class MyClass { +// You can use "magic methods" to create getters and setters +class MyMapClass +{ private $property; public function __get($key) @@ -462,16 +503,13 @@ class MyClass { } } -$x = new MyClass(); +$x = new MyMapClass(); echo $x->property; // Will use the __get() method $x->property = 'Something'; // Will use the __set() method -``` - -Classes can be abstract (using the ```abstract``` keyword), extend other classes (using the ```extends``` keyword) and -implement interfaces (using the ```implements``` keyword). An interface is declared with the ```interface``` keyword. -```php -<?php +// Classes can be abstract (using the abstract keyword) or +// implement interfaces (using the implements keyword). +// An interface is declared with the interface keyword. interface InterfaceOne { @@ -480,90 +518,112 @@ interface InterfaceOne interface InterfaceTwo { - public function doSomething(); + public function doSomethingElse(); } abstract class MyAbstractClass implements InterfaceOne { + public $x = 'doSomething'; } -class MyClass extends MyAbstractClass implements InterfaceTwo +class MyConcreteClass extends MyAbstractClass implements InterfaceTwo { + public function doSomething() + { + echo $x; + } + + public function doSomethingElse() + { + echo 'doSomethingElse'; + } } + // Classes can implement more than one interface class SomeOtherClass implements InterfaceOne, InterfaceTwo { + public function doSomething() + { + echo 'doSomething'; + } + + public function doSomethingElse() + { + echo 'doSomethingElse'; + } } -``` -### [Namespaces](http://www.php.net/manual/en/language.namespaces.rationale.php) -By default, classes exist in the global namespace, and can be explicitly called with a backslash. +/******************************** + * Traits + */ +// Traits are available from PHP 5.4.0 and are declared using "trait" + +trait MyTrait +{ + public function myTraitMethod() + { + print 'I have MyTrait'; + } +} + +class MyTraitfulClass +{ + use MyTrait; +} + +$cls = new MyTraitfulClass(); +$cls->myTraitMethod(); // Prints "I have MyTrait" + + +/******************************** + * Namespaces + */ + +// This section is separate, because a namespace declaration +// must be the first statement in a file. Let's pretend that is not the case + +/* +``` ```php <?php +// By default, classes exist in the global namespace, and can +// be explicitly called with a backslash. + $cls = new \MyClass(); -``` -```php -<?php + +// Set the namespace for a file namespace My\Namespace; class MyClass { } +// (from another file) $cls = new My\Namespace\MyClass; -``` - -Or from within another namespace. - -```php -<?php +//Or from within another namespace. namespace My\Other\Namespace; use My\Namespace\MyClass; $cls = new MyClass(); -``` -Or you can alias the namespace; - -```php -<?php +// Or you can alias the namespace; namespace My\Other\Namespace; use My\Namespace as SomeOtherNamespace; $cls = new SomeOtherNamespace\MyClass(); -``` - -### [Traits](http://www.php.net/manual/en/language.oop5.traits.php) - -Traits are available since PHP 5.4.0 and are declared using the ```trait``` keyword. - -```php -<?php -trait MyTrait { - public function myTraitMethod() - { - // Do something - } -} - -class MyClass -{ - use MyTrait; -} +*/ -$cls = new MyClass(); -$cls->myTraitMethod(); ``` ## More Information @@ -573,3 +633,5 @@ Visit the [official PHP documentation](http://www.php.net/manual/) for reference If you're interested in up-to-date best practices, visit [PHP The Right Way](http://www.phptherightway.com/). If you're coming from a language with good package management, check out [Composer](http://getcomposer.org/). + +For common standards, visit the PHP Framework Interoperability Group's [PSR standards](https://github.com/php-fig/fig-standards). diff --git a/python.html.markdown b/python.html.markdown index 2b67ab83..d1152b82 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -2,6 +2,7 @@ language: python author: Louie Dinh author_url: http://ldinh.ca +filename: learnpython.py --- Python was created by Guido Van Rossum in the early 90's. It is now one of the most popular @@ -15,7 +16,7 @@ to Python 2.x. Look for another tour of Python 3 soon! ```python # Single line comments start with a hash. -""" Multiline strings can we written +""" Multiline strings can be written using three "'s, and are often used as comments """ diff --git a/r.html.markdown b/r.html.markdown index ad2a4559..f68ede0e 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -2,12 +2,12 @@ language: R author: e99n09 author_url: http://github.com/e99n09 - +filename: learnr.r --- R is a statistical computing language. -```r +```python # Comments start with hashtags. @@ -16,9 +16,9 @@ R is a statistical computing language. # Protip: hit COMMAND-ENTER to execute a line -################################################################################### +######################### # The absolute basics -################################################################################### +######################### # NUMERICS @@ -119,9 +119,9 @@ myFunc <- function(x) { # Called like any other R function: myFunc(5) # => [1] 19 -################################################################################### +######################### # Fun with data: vectors, matrices, data frames, and arrays -################################################################################### +######################### # ONE-DIMENSIONAL @@ -243,7 +243,7 @@ array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2)) # LISTS (MULTI-DIMENSIONAL, POSSIBLY RAGGED, OF DIFFERENT TYPES) # Finally, R has lists (of vectors) -list1 <- list(time = 1:40, price = c(rnorm(40,.5*list1$time,4))) # generate random +list1 <- list(time = 1:40, price = c(rnorm(40,.5*list1$time,4))) # random list1 # You can get items in the list like so @@ -251,9 +251,9 @@ list1$time # You can subset list items like vectors list1$price[4] -################################################################################### +######################### # The apply() family of functions -################################################################################### +######################### # Remember mat? mat @@ -281,9 +281,9 @@ install.packages("plyr") require(plyr) ?plyr -################################################################################### +######################### # Loading data -################################################################################### +######################### # "pets.csv" is a file on the internet pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv") @@ -292,14 +292,14 @@ head(pets, 2) # first two rows tail(pets, 1) # last row # To save a data frame or matrix as a .csv file -write.csv(pets, "pets2.csv") # to make a new .csv file in the working directory +write.csv(pets, "pets2.csv") # to make a new .csv file # set working directory with setwd(), look it up with getwd() # Try ?read.csv and ?write.csv for more information -################################################################################### +######################### # Plots -################################################################################### +######################### # Scatterplots! plot(list1$time, list1$price, main = "fake data") |