Raven Examples - PHP vs Raven
String OperationsPHP
list ($a, $b, $c) = explode(':', 'a:b:c');
Raven
'a:b:c' ':' split into $a, $b, $c
PHP
$text = join("\t", explode(',', $csv_line));
Raven
$csv_line ',' split "\t" join as $text
PHP
$chars = strlen($string);
Raven
$string length as $chars
PHP
$text = str_replace('pattern', 'repl', $data);
Raven
$data 'repl' 'pattern' replace as $text
PHP
$today = date($pattern, time());
Raven
time int $pattern date as $today
Arrays, Lists and HashesNumeric Arrays vs Lists PHP
$test = array(1, 2, 3);
Raven
[ 1 2 3 ] as $test
PHP
$test[] = 17; array_push($test, 17);
Raven
17 $test push
PHP
$num = array_pop($test);
Raven
$test pop as $num
PHP
$num = $test[1];
Raven
$test.1 as $num
PHP
$test[1] = $num;
Raven
$num $test:1
Associative Arrays vs Hashes PHP
$test = array('a' => 1, 'b' => 2, 'c' => 3);
Raven
{ 'a' 1 'b' 2 'c' 3 } as $test
PHP
$var = $test['a']; $test['b'] = 5;
Raven
$test 'a' get as $var 5 $test 'b' set # or equiv $test.'a' as $var 5 $test:'a'
PHP
$items = count($array);
Raven
$array length as $items
PHP
$keys = array_keys($test);
Raven
$test keys as $keys
LoopsLooping Over Structures PHP
foreach ($test as $key => $val) { func_1($key); func_2($val); }
Raven
# equivalent code $test each pair as $key , $val $key func_1 $val func_2 # optimal code $test each pair func_1 func_2
One-liner Loops PHP
foreach ($test as $key => $val) print "$key => $val\n";
Raven
$test each pair "%s => %d\n" print
Conditional Loops PHP
while (some_condition()) some_function(); while (!some_condition()) some_function();
Raven
repeat some_condition while some_function repeat some_condition until some_function
Counted Loops PHP
for ($i = 0; $i < 10; $i++) do_something($i);
Raven
# equivalent code 10 each as $i $i do_something # optimal code 10 each do_something
PHP
for ($i = 0; i < 10; i+=2) do_something($i);
Raven
# equivalent code 0 10 2 range each as $i $i do_something # optimal code 5 each 2 shl do_something
MySQLPHP
$rs = mysql_query('SELECT * FROM mytable'); while ($row = mysql_fetch_assoc($rs)) print_r($row);
Raven
# equivalent code 'SELECT * FROM mytable' $mysql query as rs rs each as $row $row print # optimal code 'SELECT * FROM mytable' $mysql query each print
PHP
$rs = mysql_query('SELECT * FROM mytable'); while (list ($field_1, $field_2, $field_3) = mysql_fetch_array($rs)) do_something();
Raven
# equivalent code 'SELECT * FROM mytable' $mysql query as rs rs each values into $field_1 , $field_2 , $field_3 do_something # optimal code 'SELECT * FROM mytable' $mysql query each values into $field_1 , $field_2 , $field_3 do_something
PHP
$mysql = mysql_connect('localhost', 'user', 'password'); mysql_select_db('test', $mysql);
Raven
'mysql://user:password@localhost/test' open as $mysql
PHP
$rs = mysql_query('INSERT INTO mytable (field_1, field_2, field_3) VALUES (1, 2, 3)'); $id = mysql_insert_id();
Raven
# equivalent code 'INSERT INTO mytable (field_1, field_2, field_3) VALUES (1, 2, 3)' $mysql query as rs rs inserted as $id # optimal code 'INSERT INTO mytable (field_1, field_2, field_3) VALUES (1, 2, 3)' $mysql query inserted as $id
FilesPHP
$data = file_get_contents('myfile.txt');
Raven
'myfile.txt' read as $data
PHP
$lines = explode("\n", trim(file_get_contents('myfile.txt'))); foreach ($lines as $line) do_something($line);
Raven
# equivalent code 'myfile.txt' read trim "\n" split as $lines $lines each as $line $line do_something # optimal code 'myfile.txt' read trim "\n" split each do_something
|