Guide showing you PHP and RPG differences
$x = "Hello world!"; //String
$y = 5985; //Int
$z = 10.365; //Float
$a = true; //Boolean
Dcl-S x Char(12) Inz('Hello world!');
Dcl-S y Int(5) Inz(5985);
Dcl-S z Float(8) Inz(10.365);
Dcl-S a Ind Inz(*On);
Options for integer types:
Dcl-S y Int(3|5|10|20)
(signed)
3
: -128 to 1275
: -32768 to 3276710
: -2147483648 to 214748364720
: -9223372036854775808 to 9223372036854775807Dcl-S y Unz(3|5|10|20)
(unsigned)
3
: 0 to 2555
: 0 to 6553510
: 0 to 429496729520
: 0 to 18446744073709551615Options for decimal types:
Dcl-S z Float(4|8)
Dcl-S z Zoned(length:digits)
(zoned decimal)`Dcl-S z Packed(length:digits)
(packed decimal)`Dcl-S z Bindec(length:digits)
(binary decimal)`$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
Dcl-S txt Char(12) Inz('Hello world!');
Dcl-S x Int(3) Inz(5);
Dcl-S y Packed(5:1) Inz(10.5);
DSPLY txt;
DSPLY %Char(x);
DSPLY %Char(y);
echo 5 + 5;
echo 5 - 5;
echo 5 / 5;
echo 5 * 5;
Dcl-S Result Int(5);
Result = 5 + 5;
dsply %char(Result);
Result = 5 - 5;
dsply %char(Result);
Result = 5 / 5;
dsply %char(Result);
Result = 5 * 5;
dsply %char(Result);
$x = 10;
$x += 10;
$x -= 10;
$x /= 10;
$x *= 10;
Dcl-S Result Int(5);
Result = 10;
Result += 10;
Result -= 10;
Result /= 10;
Result *= 10;
$varA = 5;
%varB = 10;
$bool = ($varA == 4varB);
$bool = ($varA != $varB);
$bool = ($varA > $varB);
$bool = ($varA < $varB);
$bool = ($varA >= $varB);
$bool = ($varA <= $varB);
Dcl-S bool Ind;
Dcl-S varA Int(3) Inz(5);
Dcl-S varb Int(3) Inz(10);
bool = (varA == varB);
bool = (varA <> varB);
bool = (varA > varB);
bool = (varA < varB);
bool = (varA >= varB);
bool = (varA <= varB);
If ($bool == true && $varA == 5) {
echo('Hello world.');
]
If ($bool == false || $varA == 5) {
echo('Hello world.');
]
If ($bool != false) {
echo('Hello world.');
]
If (bool = *On AND varA = 5);
dsply ('Hello world.');
Endif;
If (bool = *Off OR varA = 5);
dsply ('Hello world.');
Endif;
If NOT (bool = *Off);
dsply ('Hello world.');
Endif;
$MyArray = [];
$MyArray[0] = 'Hello';
$MyArray[1] = 'World';
$MyArray[2] = 'How';
$MyArray[3] = 'Are';
$MyArray[4] = 'You?';
echo $MyArray[2];
Dcl-S MyArray Char(5) Dim(10);
MyArray(1) = 'Hello';
MyArray(2) = 'World';
MyArray(3) = 'How';
MyArray(4) = 'Are';
MyArray(5) = 'You?';
dsply (MyArray(3));
$num = 0;
while ($num < 5) {
echo 'number: ' . $num;
$num++;
}
Dcl-S num Int(3) Inz(0);
Dow (num < 5);
dsply ('number: ' + %Char(num));
num += 1;
Enddo;
for ($x = 0; $x <= 10; $x++) {
echo "The number is: " . $x;
}
Dcl-S x Int(3);
For x = 0 to 10;
dsply ('The number is: ' + %Char(x));
Endfor;
for ($x = 10; $x >= 0; $x--) {
echo "The number is: " . $x;
}
Dcl-S x Int(3);
For x = 10 downto 0;
dsply ('The number is: ' + %Char(x));
Endfor;
for
is also writable as For x = 0 to 10 by 2
.for ($x = 0; $x <= 10; $x += 2) {
echo "The number is: " . $x;
}
For x = 0 by 2 to 10;
dsply ('The number is: ' + %Char(x));
Endfor;
value
keyword.*N
in RPG represents 'nothing' - you can optionally put the procedure name (doDivide
) in its place.echo doDivide(10, 2);
return;
function doDivide($numA, $numB) {
return $numA / $numB;
}
dsply (%Char(doDivide(10, 2)));
*InLR = *On;
Return;
Dcl-Proc doDivide;
Dcl-Pi *N Int(3);
numA Int(3) Value;
numB Int(3) Value;
End-Pi;
Return numA / numB;
Edn-Proc;
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
Dcl-S x Int(3) Inz(5); //Global scope
myTest();
dsply ('Variable x outside procedure is: ' + %Char(x));
*Inlr = *On;
Return;
Dcl-Proc myTest;
// using x inside this function will be ok because it's global
dsply ('Variable x inside procedure is: ' + %Char(x));
End-Proc;
x
outside the function/procedure will stop the compiler/script because x doesn't exist in the global scope.function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
myTest();
dsply ('Variable x outside procedure is: ' + %Char(x));
*Inlr = *On;
Return;
Dcl-Proc myTest;
Dcl-S x Int(3) Inz(5);
dsply ('Variable x inside procedure is: ' + %Char(x));
End-Proc;
echo strlen("Hello world!");
%len
, it will return the length of the variable - this includes the blanks.//We use %trim to remove the blanks from the character field
// Dcl-S someAlphaVar Char(100) Inz('Hello there world!');
// %len(%trim(someAlphaVar));
//%Char must be used because DSPLY won't except anything other than characters
dsply %char(%len('Hello world!'));
strpos
and %scan
return the starting index of the string.echo strpos("Hello world!", "world");
dsply %char(%scan('world':'Hello world!'));
echo str_replace("world", "Barry", "Hello world!");
dsply %scanrpl('world':'Barry':'Hello world!');