English (United Kingdom)French (Fr)Italian - Italy
RPG PHP tutorial

  • Array
  • IF
  • Loop
  • Database
    • Read
    • Update
    • Write
    • Setll

 

Array

 

Example: 2 arrays with 100 elements of 1 char length.

 

RPG3

E....FromfileTofile++Name++N/rN/tbLenPDSArrnamLenPDSComments++++++
E                    MYTAB     100  1                             
E                    MYTAB2    100  1                             

 

RPG4

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
D MyTab           s              1    dim(100)                         
D MyTab2          s              1    dim(100)                         

 

PHP

<?php             
$MyTab=array();   
$MyTab2=array();  
?>                

 

In PHP, when you use a variable it always start with $.
You don’t need to specify its type or length, you just used it directly, PHP will manage all dynamically.
For array you don’t need to specify neither the number of elements, PHP will also manage all dynamically.

 


If

 

RPG3

CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++
C           A         IFEQ ‘abc’                                  
C                     MOVE B         C                            
C                     ELSE                                        
C                     MOVE Z         C                            
C                     ENDIF                                       

 

RPG4

CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++
C                   if        A = ‘abc’                                
C                   eval      C = B                                    
C                   endif                                              

 

PHP

<?php              
if ($a ==  “abc”)  
{                  
C = B;             
}                  
else               
{                  
C = Z;             
}                  
?>                 

Or also:

<?php                         
($a == "abc" ? 'C=B' :'C=Z'); 
?>                            

 

In PHP, if you write
if ( $a = $b ) with a single =
this will change the value of the variable $a with the value of the variable $b.

Use always a double == to compare variables (if, do-while, for…)
and use a single = to change variables value. 


 


Loop

 

 

RPG3

CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++
C                     Z-ADD1         I       20                   
C           I         DOUGT100                                    
C                     MOVE MYTAB,I   MYTAB2,I                     
C                     ADD  1         I                            
C                     ENDDO                                       

 

RPG4

CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++
C                   eval      i = 1                                    
C                   dou       i > 100                                  
C                   eval      MyTab2(I) = MyTab(i)                     
C                   eval      i = i + 1                                
C                   enddo                                              

 

PHP

<?php                          
$i = 0;                        
do {                           
    $MyTab2[$i] = $MyTab[$i];  
    $i = $i + 1;               
} while ($i <= 100);           
?>                             

Or also:

<?php                                                 
for ($i = 0; $i < 100; $MyTab2[$i++] = $MyTab[$i++]); 
?>                                                    

 

In PHP, array indexes can be numeric and also alphanumeric. When they are numeric, they start at 0, not 1. 


 


Working with Database file - READ

 

RPG3

FMYFILE  IF  E           K        DISK                           
…                                                                
C                     READ MYFILE                   89           
C           *IN89     DOWEQ'0'                                   
C                     MOVE MYFLD     VAR1    10                  
C                     …                                          
C                     READ MYFILE                   89           
C                     ENDDO                                      

 

RPG4

FMyFile    IF   E           K DISK                                     
…                                                                      
D var1            s             10                                     
…                                                                      
C                   read      MyFile                                 89
C                   dow       *in89 = ‘0’                              
C                   eval      var1 = MyFld                             
C                   …                                                  
C                   read      MyFile                                 89
C                   enddo                                              

 

PHP

<?php                                                                
                                                                     
$connect = '';  //'' means host                                      
$user = '';     //'' means user nobody                               
$pass = '';     //'' no pwd for nobody                               
                                                                     
$ConnectToServer = i5_connect($connect, $user, $pass);               
if (!$ConnectToServer)    // if cannot connect                       
{                                                                    
    die(i5_errormsg()); // display CPFnnnn                           
}                                                                    
                                                                     
                                                                     
$openFile = i5_open("myLib/myFile", I5_OPEN_READ, $ConnectToServer); 
if (!$openFile)           // if cannot open                          
{                                                                    
    die(i5_errormsg()); // display CPFnnnn                           
}                                                                    
                                                                     
                                                                     
while ($row = i5_fetch_array($openFile, I5_READ_NEXT))               
{                                                                    
$var1 = $row['MYFLD']                                                
…                                                                    
}                                                                    
                                                                     
?>                                                                   

 

In PHP, before opening a file you need to connect to a server (which can be another server than the one you are in). Then you have to explicitly open the file.
In PHP, no *indicator (89) are used; instead whole functions return a Boolean value, here the function ($row = i5_fetch_array($openFile, I5_READ_NEXT)) returns 0 or 1 directly (0 if unsuccessful read, 1 if the record exist [yes that’s the opposite of RPG indicator]).


 


Working with Database file - UPDATE

 

RPG3

FMYFILE  UF  E           K        DISK                           
…                                                                
C                     MOVEL’abc’     VAR1    10                  
…                                                                
C                     READ MYFILE                   89           
C           *IN89     DOWEQ'0'                                   
C                     MOVE VAR1      MYFLD                       
C                     …                                          
C                     UPDATMYFILE                                
C                     READ MYFILE                   89           
C                     ENDDO                                      

 

RPG4

FMyFile    UF   E           K DISK                                     
…                                                                      
D var1            s             10                                     
…                                                                      
C                   eval      Var1 = ‘abc’                             
…                                                                      
C                   read      MyFile                                 89
C                   dow       *in89 = ‘0’                              
C                   eval      MyFld = Var1                             
C                   …                                                  
C                   update    MyFile                                   
C                   read      MyFile                                 89
C                   enddo                                              

 

PHP

<?php                                                                     
$connect = '';  //'' means host                                           
$user = '';     //'' means user nobody                                    
$pass = '';     //'' no pwd for nobody                                    
                                                                          
$ConnectToServer = i5_connect($connect, $user, $pass);                    
if (!$ConnectToServer)    // if cannot connect                            
{                                                                         
    die(i5_errormsg()); // display CPFnnnn                                
}                                                                         
                                                                          
                                                                          
$openFile = i5_open("myLib/myFile", I5_OPEN_READWRITE, $ConnectToServer); 
if (!$openFile)           // if cannot open                               
{                                                                         
    die(i5_errormsg()); // display CPFnnnn                                
}                                                                         
                                                                          
$var1 = ‘abc’;                                                            
                                                                          
while ($row = i5_fetch_array($openFile, I5_READ_NEXT))                    
{                                                                         
I5_update_record($openFile, array(“MYFLD” => “abc”));                     
                                                                          
…                                                                         
}                                                                         
                                                                          
?>                                                                        


 


Working with Database file - WRITE

 

RPG3

FMYFILE  OF  E           K        DISK                           
…                                                                
C                     MOVEL’abc’     VAR1    10                  
…                                                                
C                     MOVE VAR1      MYFLD                       
C                     WRITEMYFILE                                

 

RPG4

FMyFile    OF   E           K DISK                
…                                                 
D var1            s             10                
…                                                 
C                   eval      Var1 = ‘abc’        
…                                                 
C                   eval      MyFld = Var1        
C                   …                             
C                   write     MyFile              

 

PHP

<?php                                                                     
$connect = '';  //'' means host                                           
$user = '';     //'' means user nobody                                    
$pass = '';     //'' no pwd for nobody                                    
                                                                          
$ConnectToServer = i5_connect($connect, $user, $pass);                    
if (!$ConnectToServer)    // if cannot connect                            
{                                                                         
    die(i5_errormsg()); // display CPFnnnn                                
}                                                                         
                                                                          
                                                                          
$openFile = i5_open("myLib/myFile", I5_OPEN_READWRITE, $ConnectToServer); 
if (!$openFile)           // if cannot open                               
{                                                                         
    die(i5_errormsg()); // display CPFnnnn                                
}                                                                         
                                                                          
$var1 = ‘abc’;                                                            
                                                                          
                                                                          
I5_new_record($openFile, array(“MYFLD” => “abc”, “MYFLD2” => “xyz”));     
                                                                          
                                                                          
?>                                                                        


 


Working with Database file - SETLL

 

 

RPG3

FMYFILE  IF  E           K        DISK                           
…                                                                
C           K1        KLIST                                      
C                     KFLD           MYFLDK  20                  
C                     KFLD           MYFLDK2 5                   
…                                                                
C                     Z-ADD4         MYFLDK                      
C                     MOVEL’abc  ’   MYFLDK2                     
C           K1        SETLLMYFILE                                
C                     READ MYFILE                   89           

 

RPG4

FMyFile    IF   E           K DISK                                     
…                                                                      
C     K1            KLIST                                              
C                   KFLD                    MyFldK            2 0      
C                   KFLD                    MyFldK2           5        
…                                                                      
C                   eval      MyFldK = 4                               
C                   eval      MyFldK2 = ‘abc’                          
C     K1            setll     MyFile                                   
C                   read      MyFile                                 89

 

PHP

<?php                                                                     
$connect = '';  //'' means host                                           
$user = '';     //'' means user nobody                                    
$pass = '';     //'' no pwd for nobody                                    
                                                                          
$ConnectToServer = i5_connect($connect, $user, $pass);                    
if (!$ConnectToServer)    // if cannot connect                            
{                                                                         
    die(i5_errormsg()); // display CPFnnnn                                
}                                                                         
                                                                          
                                                                          
$openFile = i5_open("myLib/myFile", I5_OPEN_READWRITE, $ConnectToServer); 
if (!$openFile)           // if cannot open                               
{                                                                         
    die(i5_errormsg()); // display CPFnnnn                                
}                                                                         
                                                                          
                                                                          
// SETLL                                                                  
$K1 = array(4,’abc’);                                                     
i5_seek($openFile, "=" , $K1);                                            
                                                                          
                                                                          
$row = i5_fetch_array($openFile, I5_READ_NEXT);                           
                                                                          
?>                                                                        

 

To define the KLIST, PHP uses an array. You will see that arrays are very flexible in PHP (a lot of more than in RPG), they are used every time you need to manage a list of “entities", even of different type or format.  


This e-mail address is being protected from spambots. You need JavaScript enabled to view it


 

Contact e-mail      Valid XHTML and CSS.