diff options
-rw-r--r-- | c.html.markdown | 7 | ||||
-rw-r--r-- | clojure.html.markdown | 1 | ||||
-rw-r--r-- | dart.html.markdown | 1 | ||||
-rw-r--r-- | file.erb | 1 | ||||
-rw-r--r-- | fsharp.html.markdown | 1 | ||||
-rw-r--r-- | haskell.html.markdown | 1 | ||||
-rw-r--r-- | lua.html.markdown | 1 | ||||
-rw-r--r-- | php.html.markdown | 216 | ||||
-rw-r--r-- | python.html.markdown | 1 | ||||
-rw-r--r-- | r.html.markdown | 2 |
10 files changed, 156 insertions, 76 deletions
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 c5298aab..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 diff --git a/dart.html.markdown b/dart.html.markdown index c503fb4a..27365746 100644 --- a/dart.html.markdown +++ b/dart.html.markdown @@ -2,6 +2,7 @@ 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 fbaa93f2..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 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 1a8dea2c..20923548 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -2,6 +2,7 @@ language: php author: Malcolm Fell author_url: http://emarref.net/ +filename: learnphp.php --- This document describes PHP 5+. @@ -9,6 +10,9 @@ This document describes PHP 5+. ```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. # So will a hash (aka pound symbol) but // is more common @@ -22,7 +26,7 @@ This document describes PHP 5+. print('Hello '); // Prints "Hello " with no line break // () are optional for print and echo -echo 'World\n'; // Prints "World" with a line break +echo "World\n"; // Prints "World" with a line break // (all statements must end with a semicolon) // Anything outside <?php tags is echoed automatically @@ -39,13 +43,13 @@ echo 'World\n'; // Prints "World" with a line break // 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 -$int1 = 19; // => 19 -$int2 = -19; // => -19 -$int3 = 019; // => 15 (a leading 0 denotes an octal number) +$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) @@ -54,26 +58,26 @@ $float = 1.2e3; $float = 7E-10; // Arithmetic -$sum = 1 + 1; // 2 +$sum = 1 + 1; // 2 $difference = 2 - 1; // 1 -$product = 2 * 2; // 4 -$quotient = 2 / 1; // 2 +$product = 2 * 2; // 4 +$quotient = 2 / 1; // 2 // Shorthand arithmetic $number = 0; -$number += 1; // Add 1 to $number -echo $number++; // Prints 1 (increments after evaluation) -echo ++$number; // Prints 3 (increments before evalutation) +$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.' // Special characters are only escaped in double quotes -$escaped = "This contains a \t tab character."; +$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 @@ -109,7 +113,7 @@ $associative = array('One' => 1, 'Two' => 2, 'Three' => 3); // PHP 5.4 introduced a new syntax $associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; -echo $associative['One']; // prints "1" +echo $associative['One']; // prints 1 // List literals implicitly assign integer keys $array = ['One', 'Two', 'Three']; @@ -132,8 +136,8 @@ print 'Hello World!'; // So is print $paragraph = 'paragraph'; -echo 100; -echo $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 @@ -143,10 +147,11 @@ echo $paragraph; $x = 1; $y = 2; -$x = $y; // A now contains the same value sa $y +$x = $y; // $x now contains the same value as $y $z = &$y; -// $x now contains a reference to $y. Changing the value of -// $x will change the value of $y also, and vice-versa. +// $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 echo $x; // => 2 echo $z; // => 2 @@ -167,8 +172,8 @@ $d = '1'; // These comparisons will always be true, even if the types aren't the same. assert($a == $b); // equality -assert($b != $a); // inequality -assert($a <> $b); // alternative inequality +assert($c != $a); // inequality +assert($c <> $a); // alternative inequality assert($a < $c); assert($c > $b); assert($a <= $b); @@ -215,7 +220,7 @@ if (true) { } if (false) { - print "I don't"; + print 'I don\'t'; } else { print 'I get printed'; } @@ -251,6 +256,7 @@ 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' @@ -275,16 +281,16 @@ do { echo "\n"; for ($x = 0; $x < 10; $x++) { - echo $x; // Will echo 0 - 9 -}// Prints "0123456789" + echo $x; +} // Prints "0123456789" echo "\n"; $wheels = ['bicycle' => 2, 'car' => 4]; // Foreach loops can iterate over arrays -foreach ($wheels as $wheel_count){ - echo "$wheel_count"; +foreach ($wheels as $wheel_count) { + echo $wheel_count; } // Prints "24" echo "\n"; @@ -302,9 +308,9 @@ while ($i < 5) { break; // Exit out of the while loop } echo $i++; -}// Prints "012" +} // Prints "012" -for($i = 0; $i < 5; $i++){ +for ($i = 0; $i < 5; $i++) { if ($i === 3) { continue; // Skip this iteration of the loop } @@ -317,7 +323,7 @@ for($i = 0; $i < 5; $i++){ */ // Define a function with "function": -function my_function() { +function my_function () { return 'Hello'; } @@ -326,7 +332,7 @@ echo my_function(); // => "Hello" // A valid function name starts with a letter or underscore, followed by any // number of letters, numbers, or underscores. -function add($x, $y = 1) { // $y is optional, and defaults to 2 +function add ($x, $y = 1) { // $y is optional and defaults to 1 $result = $x + $y; return $result; } @@ -338,7 +344,7 @@ echo add(4, 2); // => 6 // print $result; // Gives a warning. // Since PHP 5.3 you can declare anonymous functions; -$inc = function($x){ +$inc = function ($x) { return $x + 1; }; @@ -357,78 +363,133 @@ function bar ($x, $y) { } $bar = bar('A', 'B'); -$bar('C'); +$bar('C'); // Prints "A - B - C" // You can call named functions using strings $function_name = 'add'; echo $function_name(1, 2); // => 3 -// But, you should probably use anonymous functions instead. +// Useful for programatically determining which function to run. +// Or, use call_user_func(callable $callback [, $parameter [, ... ]]); + +/******************************** + * Includes + */ + +/* +``` +```php +<?php +// Included files must also begin with a PHP open tags. + +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. + +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 + +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. + +// Contents of my-include.php: +<?php + +return 'Anything you like.'; +// End file + +// Includes and requires may also return a value. +$value = include 'my-include.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. +/* */ /******************************** * Classes */ -//Classes are defined with the class keyword +// Classes are defined with the class keyword + +class MyClass +{ + const MY_CONST = 'value'; // A constant -class MyClass { - const MY_CONST = 'value'; // A constant - static $staticVar = 'static'; - public $property = 'public'; // Properties must declare their visibility - private $privprop = 'private'; // Accessible within the class only - protected $protprop = 'protected'; // Accessible within the class and subclasses + static $staticVar = 'static'; + + // 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){ + public function __construct($instanceProp) { // Access instance variables with $this $this->instanceProp = $instanceProp; } + // Methods are declared as functions inside a class - public function myMethod() { - print "MyClass"; + public function myMethod() + { + print 'MyClass'; } - final function youCannotOverrideMe() { + final function youCannotOverrideMe() + { } - public static function myStaticMethod() { - print "I am static"; + public static function myStaticMethod() + { + print 'I am static'; } } -echo MyClass::MY_CONST; // Outputs "value"; -echo MyClass::$staticVar; // Outputs 'static'; -MyClass::myStaticMethod(); // Outputs "I am static"; +echo MyClass::MY_CONST; // Outputs 'value'; +echo MyClass::$staticVar; // Outputs 'static'; +MyClass::myStaticMethod(); // Outputs 'I am static'; -// Access class members using ->. -$my_class = new MyClass("An instance property"); // The parentheses are optional. -echo $my_class->property; // => "public" +// Instantiate classes using new +$my_class = new MyClass('An instance property'); +// The parentheses are optional if not passing in an argument. + +// Access class members using -> +echo $my_class->property; // => "public" echo $my_class->instanceProp; // => "An instance property" -$my_class->myMethod(); // => "MyClass" +$my_class->myMethod(); // => "MyClass" // Extend classes using "extends" -class MyOtherClass extends MyClass{ - function printProtectedProperty(){ - echo $this->protprop; +class MyOtherClass extends MyClass +{ + function printProtectedProperty() + { + echo $this->prot; } // Override a method - function myMethod() { + function myMethod() + { parent::myMethod(); - print " > MyOtherClass"; + print ' > MyOtherClass'; } } -$my_other_class = new MyOtherClass("Instance prop"); +$my_other_class = new MyOtherClass('Instance prop'); $my_other_class->printProtectedProperty(); // => Prints "protected" -$my_other_class->myMethod(); // Prints "MyClass > MyOtherClass" +$my_other_class->myMethod(); // Prints "MyClass > MyOtherClass" -final class YouCannotExtendMe { +final class YouCannotExtendMe +{ } // You can use "magic methods" to create getters and setters -class MyMapClass { +class MyMapClass +{ private $property; public function __get($key) @@ -462,16 +523,19 @@ interface InterfaceTwo abstract class MyAbstractClass implements InterfaceOne { - public $x = "doSomething"; + public $x = 'doSomething'; } class MyConcreteClass extends MyAbstractClass implements InterfaceTwo { - public function doSomething(){ + public function doSomething() + { echo $x; } - public function doSomethingElse(){ - echo "doSomethingElse"; + + public function doSomethingElse() + { + echo 'doSomethingElse'; } } @@ -479,11 +543,14 @@ class MyConcreteClass extends MyAbstractClass implements InterfaceTwo // Classes can implement more than one interface class SomeOtherClass implements InterfaceOne, InterfaceTwo { - public function doSomething(){ - echo "doSomething"; + public function doSomething() + { + echo 'doSomething'; } - public function doSomethingElse(){ - echo "doSomethingElse"; + + public function doSomethingElse() + { + echo 'doSomethingElse'; } } @@ -492,12 +559,13 @@ class SomeOtherClass implements InterfaceOne, InterfaceTwo * Traits */ -//Traits are available since PHP 5.4.0 and are declared using the trait keyword. +// Traits are available from PHP 5.4.0 and are declared using "trait" -trait MyTrait { +trait MyTrait +{ public function myTraitMethod() { - print "I have MyTrait"; + print 'I have MyTrait'; } } @@ -565,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 eddff031..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 diff --git a/r.html.markdown b/r.html.markdown index 1ace3ed5..f68ede0e 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -2,7 +2,7 @@ language: R author: e99n09 author_url: http://github.com/e99n09 - +filename: learnr.r --- R is a statistical computing language. |