Scripting, JavaScript, PHP, comparison
Mapped syntaxes of three programming languages.
Main function
Scriptol |
JavaScript |
PHP |
print "Hello"
|
console.log("Hello");
|
echo "Hello", "\n";
|
print a[x .. y]
|
console.log(a.slice(x,y-x+2));
|
print_r(array_slice($a,$x,$y-($x)+
count($a)*(($y<0)-($x<0))+1));echo "\n";
|
a[.. y] = b[1 .. 3]
|
a.splice.apply(a,[0,y-0+1].
concat(b.slice(1,4)));
|
array_splice($a,0,$y-(0)+count($a)*($y<0)+1,
array_slice($b,1,3));
|
a[2 .. 8] = nil
|
a.splice.apply(a,[2,7].concat([]));
|
array_splice($a,2,7);
|
JavaScript is sometimes simpler than PHP, sometimes vice versa. Scriptol is always simpler than the other two languages.
Data structures
Scriptol |
JavaScript |
PHP |
array a = [ 1, 2, 3 ]
|
var a=[1,2,3];
|
$a=array(1,2,3);
|
<car speed=150 name="Spitfire">
<engine power=100></engine>
<passengers num=4>
"Clara, Dana, Elisa, Farah"
</passengers>
</car>
|
var car={
"speed":150,
"name":"Spitfire",
"engine":{
"power":100
},
"passengers":{
"num":4,
"data":"Clara, Dana, Elisa, Farah"
}
};
|
$car=[
"speed"=>150,
"name"=>"Spitfire",
"engine"=>[
"power"=>100],
"passengers"=>[
"num"=>4,
"data"=>"Clara, Dana, Elisa, Farah"
]
];
|
The XML object is converted to an associative array in JS or PHP. The XML structure has the equivalent of JavaScript as nested objects, which can be saved in a JSON file. Therefore, JavaScript has every opportunity for advanced and dynamic data presentation, but in a rudimentary and not very readable form. Other languages do not even have such opportunities.
JSON is a way to serialize (copy to a file) a JavaScript table.
Control structures
Scriptol |
JavaScript |
PHP |
for int i in 0 .. 9 print i
|
for(i=0;i<=9;i++)
console.log(i);
|
for($i=0;$i<=9;$i++) echo $i, "\n";
|
for int i in 0 .. 9 print i /for
|
for(i=0;i<=9;i++) {
console.log(i);
}
|
for($i=0;$i<=9;$i++) { echo $i, "\n"; }
|
if j in [ 1, 2, 3, 4 ] print "in"
|
if(([1,2,3,4].indexOf(j)!=-1)) { console.log("in"); }
|
if(in_array($j,array(1,2,3,4))) { echo "in", "\n"; }
|
do case x = 1: print 1 case x > 1: print 2 else print 0 /do
|
do {
if(x===1) {
console.log(1);
}
else {
if(x>1) {
console.log(2);
}
else {
console.log(0);
}
}
} while(false);
|
do
{
if($x===1)
{
echo 1, "\n";
}
else
{
if($x>1)
{
echo 2, "\n";
}
else
{
echo 0, "\n";
}
}
}
while(false);
|
text t input "O/N?", t if t.lower() = "o": print "oui" = "n": print "non" else print "quoi?" /if
|
var t="";
process.stdout.write("O/N?");
t=fs.readSync(process.stdin.fd, 100, 0, 'utf8').shift();
process.stdin.pause();
t=t.toLowerCase();
if(t==="o") {
console.log("oui");
}
else {
if(t==="n") {
console.log("non");
}
else {
console.log("quoi?");
}}
|
$t="";
echo "O/N?";
$fp=fopen("php://stdin","r");
$t=rtrim(fgets($fp,65536));
fclose($fp);
$t=strtolower($t);
if($t==="o")
{
echo "oui", "\n";
}
else
{
if($t==="n")
{
echo "non", "\n";
}
else
{
echo "quoi?", "\n";
}
}
|
The for loop is common to all languages, and a variation for in or for each that allows you to scan the contents of the list is added to all dynamic languages. However, JavaScript for each has so little performance that it is better to use the classic loop for.
Pattern matching is implemented in Script with the structure do... field.
Classes
Scriptol |
JavaScript |
PHP |
class Car is Vehicle int speed int getSpeed() speed + 1 return speed /class
|
var Car=(function(_super)
{
scriptol.extends(Car,_super);
Car.prototype.getSpeed=function()
{
this.speed+=1;
return this.speed;
}
function Car() {
_super.apply(this, arguments);
this.speed=0;
}
return Car;
})(Vehicle);
|
class Car extends Vehicle
{
var $speed=0;
function getSpeed()
{
$this->speed+=1;
return $this->speed;
}
}
|
Declaring a class in JavaScript version 6 becomes similar to what it was in Script or PHP, but web compatibility will not come until many years later.