Tutorial by Examples

Exponentiation makes the second operand the power of the first operand (ab). var a = 2, b = 3, c = Math.pow(a, b); c will now be 8 6 Stage 3 ES2016 (ECMAScript 7) Proposal: let a = 2, b = 3, c = a ** b; c will now be 8 Use Math.pow to find the nth root of a number....
ConstantsDescriptionApproximateMath.EBase of natural logarithm e2.718Math.LN10Natural logarithm of 102.302Math.LN2Natural logarithm of 20.693Math.LOG10EBase 10 logarithm of e0.434Math.LOG2EBase 2 logarithm of e1.442Math.PIPi: the ratio of circle circumference to diameter (π)3.14Math.SQRT1_2Square ro...
All angles below are in radians. An angle r in radians has measure 180 * r / Math.PI in degrees. Sine Math.sin(r); This will return the sine of r, a value between -1 and 1. Math.asin(r); This will return the arcsine (the reverse of the sine) of r. Math.asinh(r) This will return the hype...
Given a list comprehension you can append one or more if conditions to filter values. [<expression> for <element> in <iterable> if <condition>] For each <element> in <iterable>; if <condition> evaluates to True, add <expression> (usually a function...
An array can be initialized empty: // An empty array $foo = array(); // Shorthand notation available since PHP 5.4 $foo = []; An array can be initialized and preset with values: // Creates a simple array with three strings $fruit = array('apples', 'pears', 'oranges'); // Shorthand no...
Anonymous functions can be assigned to variables for use as parameters where a callback is expected: $uppercase = function($data) { return strtoupper($data); }; $mixedCase = ["Hello", "World"]; $uppercased = array_map($uppercase, $mixedCase); print_r($uppercased); ...
The use construct is used to import variables into the anonymous function's scope: $divisor = 2332; $myfunction = function($number) use ($divisor) { return $number / $divisor; }; echo $myfunction(81620); //Outputs 35 Variables can also be imported by reference: $collection = []; $a...
print_r() - Outputting Arrays and Objects for debugging print_r will output a human readable format of an array or object. You may have a variable that is an array or object. Trying to output it with an echo will throw the error: Notice: Array to string conversion. You can instead use the print_r...
The most widely used language construct to print output in PHP is echo: echo "Hello, World!\n"; Alternatively, you can also use print: print "Hello, World!\n"; Both statements perform the same function, with minor differences: echo has a void return, whereas print retu...
This program prints Hello World! to the standard output stream: #include <iostream> int main() { std::cout << "Hello World!" << std::endl; } See it live on Coliru. Analysis Let's examine each part of this code in detail: #include <iostream> is a...
Variables can be incremented or decremented by 1 using the ++ and -- operators, respectively. When the ++ and -- operators follow variables, they are called post-increment and post-decrement respectively. int a = 10; a++; // a now equals 11 a--; // a now equals 10 again When the ++ and -- ope...
let number = 3 switch number { case 1: print("One!") case 2: print("Two!") case 3: print("Three!") default: print("Not One, Two or Three") } switch statements also work with data types other than integers. They work with any data t...
A single case in a switch statement can match on multiple values. let number = 3 switch number { case 1, 2: print("One or Two!") case 3: print("Three!") case 4, 5, 6: print("Four, Five or Six!") default: print("Not One, Two, Three, Four, F...
A single case in a switch statement can match a range of values. let number = 20 switch number { case 0: print("Zero") case 1..<10: print("Between One and Ten") case 10..<20: print("Between Ten and Twenty") case 20..<30: print("Be...
var x = true, y = false; AND This operator will return true if both of the expressions evaluate to true. This boolean operator will employ short-circuiting and will not evaluate y if x evaluates to false. x && y; This will return false, because y is false. OR This operator wil...
It is often necessary to generate a new array based on the values of an existing array. For example, to generate an array of string lengths from an array of strings: 5.1 ['one', 'two', 'three', 'four'].map(function(value, index, arr) { return value.length; }); // → [3, 3, 5, 4] 6 ['one...
Python lists are zero-indexed, and act like arrays in other languages. lst = [1, 2, 3, 4] lst[0] # 1 lst[1] # 2 Attempting to access an index outside the bounds of the list will raise an IndexError. lst[4] # IndexError: list index out of range Negative indices are interpreted as countin...
dictionary = {"Hello": 1234, "World": 5678} print(dictionary["Hello"]) The above code will print 1234. The string "Hello" in this example is called a key. It is used to lookup a value in the dict by placing the key in square brackets. The number 1234 is ...
Arguments are passed to the program in a manner similar to most C-style languages. $argc is an integer containing the number of arguments including the program name, and $argv is an array containing arguments to the program. The first element of $argv is the name of the program. #!/usr/bin/php p...
When run from the CLI, the constants STDIN, STDOUT, and STDERR are predefined. These constants are file handles, and can be considered equivalent to the results of running the following commands: STDIN = fopen("php://stdin", "r"); STDOUT = fopen("php://stdout", "...

Page 28 of 1336