language version % php --version PHP 7.0.0RC6 (cli) (built: Oct 29 2015 13:49:53) ( NTS ) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies % swift --version Apple Swift version 2.1 (swiftlang-700.1.101.6 clang-700.1.76) Target: x86_64-apple-darwin14.5.0
comment /* * This is a comment. */ // This is also a comment. # This is comment, too. // /* // * /* Nested comment not work. */ // */ /* * This is a comment. */ // This is also a comment. // "#" is not a comment syntax. /* * /* Nested comment works. */ */
access define('GLOBAL_CONST', 'global const'); $global = 'global var'; class ExampleClass { private $privateVar = 'private var'; protected $protectedVar = 'protected var'; public $publicVar = 'public var'; const CLASS_CONST = 'class const'; } // Behavior is different from PHP // These comparison is not correct. let GLOBAL_CONST = "global const" var global = "global var" public class ExampleClass { private var privateVar = "private var" internal var internalVar = "internal var" public var publicVar = "public var" private let privateConst = "private const" internal let internalConst = "inetrnal const" public let publicConst = "public const" } private class PrivateClass {} internal class InternalClass {} public class PublicClass {}
if $a = 123; $b = 456; if ($a > $b) { } elseif ($a < $b) { } else { } var a = 123 var b = 456 // Parens can be ommitted. if a > b { } else if a < b { } else { }
switch $foo = 1; $bar = ''; switch ($foo) { case 1: $bar = 'a'; break; case 2: $bar = 'b'; break; default: $bar = 'c'; } var foo = 1 var bar = "" switch foo { case 1: bar = "a" // no need "break" case 2: bar = "b" default: bar = "c" }
for $array = []; for ($i = 0; $i < 10; $i++) { $array[] = $i; } foreach ($array as $tmp) { echo $tmp; } var array:[Int] = []; for i in 0..<10 { array.append(i) } for tmp in array { print(tmp); }
while $max = 3; $counter1 = 0; $counter2 = 0; while ($counter1 < $max) { echo 'Hi!'; $counter1++; } do { echo 'Ho!'; $counter2++; } while ($counter2 < $max); let max = 3; var counter1 = 0; var counter2 = 0; while counter1 < max { print("Hi!") counter1++ } repeat { print("ho!") counter2++ } while counter2 < max
function // PHP7: scalar type hints, return type hints function foo(string $str): array { $result = []; for ($i = 0; $i < 10; $i++) { $result[] = $str . $i; } return $result; } $list = foo('x'); foreach ($list as $l) { echo $l; }
func foo(str: String) -> Array { var result:[String] = [] for i in 0..<10 { result.append(str + String(i)) } return result } let list = foo("x") for l in list { print(l) }
class interface ExampleInterface { public function foo(): string; } class Base { protected $bar = 'bar'; } class Example extends Base implements ExampleInterface { public function __construct() { echo 'initialize'; } public function foo(): string { return $this->bar; } } $example = new Example(); echo $example->foo(); protocol ExampleProtocol { func foo() -> String } class Base { internal let bar = "bar" } class Example: Base, ExampleProtocol { override init() { print("initialize") } func foo() -> String { return self.bar } } let example = Example() // no need 'new' print(example.foo())
App::run() public function run() { $params = $this->parseGetVars(); $html = ''; $next = $params['next'] ?? ''; switch ($next) { case 'confirm': $html = $this->view->getConfirmHtml($params); break; case 'complete': $html = $this->view->getCompleteHtml($params); break; default: $html = $this->view->getFormHtml($params); } $this->response($html); } func run() { let params = self.parseGetVars() var html = "" let next = params["next"] ?? "" switch next { case "confirm": html = self.view.getConfirmHtml(params) case "complete": html = self.view.getCompleteHtml(params) default: html = self.view.getFormHtml(params) } self.response(html); }