PHP itself isn’t that much different from COBOL

If you’re a developer, you know most languages aren’t that much different from each other, it’s just learning how to implement the current function or feature you’re looking for that’s the hard part.

I was reading this article on COBOL still accounting for 80% of business code in the world, and thought about how languages aren’t really that different from each other. For example, I’ll document some common and routine snippets of code from COBOL and PHP. It isn’t the language that’s hard to learn, it’s simple syntax, but the hard part is finding out how to get your favorite language to do what you want.

To simplify things, I’m using PHP’s alternative syntax, which means that, similar to COBOL, you don’t use { and } to enclose some statements, you use statements such as ENDIF and ENDFOR, which is how I learned it.

These should work if you’re using PHP5 and PHP4. PHP’s Object Oriented structure isn’t described here. The COBOL examples are strictly IBM z/OS COBOL, old school, no OOP required, or desired.

One thing to understand about PHP, as this is different from some other languages, is it will do a fairly good job of determining if your variable is a string or numeric, so you don’t see a lot of “wrong type” errors in PHP.

Maybe another day, or another life time, I’ll write about the similarities of CICS and PHP/Apache. They both are stateless protocols, in which in CICS you need a COMMUNICATIONS AREA to or a DATA MAP to hold variables you want to keep, and with PHP, you’d probably use a session to do the same thing. A CICS MAP isn’t a lot different from defining an HTML FORM to a user.

I haven’t included the WORKING STORAGE of the definitions of these COBOL variables, to save space, but they shouldn’t matter very much for these examples. You can define variables in PHP “on the fly”, but you may get syntax notification warnings in PHP if you try to use a variable before its definition.

If you’re a COBOL developer interested in PHP, the best place to learn it is on http://php.net, search or browse through some functions to see some of the things you work with today that could work in PHP. For instance, search for the “Date” function, and browse the pages on arrays to get a feel for how those topics are presented. Each function has user contributed notes too for code tidbits or gotchas

Then, download some PHP code, and see what it’s doing. Try to avoid Object Oriented PHP code unless you’re familiar with those types of structures.

These examples below are pretty simple, if you need more complex ones, let me know. No questions about how to convert any Cobol ’74 Report-Writer or internal sorts to PHP though!

(btw, in the CNET article, it describes PHP being a “new” language, and Java and .NET, as well as COBOL, as older languages. PHP itself has been around longer than .NET, but what do you expect, it’s CNET).

Conditional Statements

COBOL

IF EMP-NO = '228A'
    COMPUTE EMP-RAISE = EMP-SALARY * .05
ELSE
    COMPUTE EMP-RAISE = 0
END-IF

PHP

if ($emp_no == '228A'):
    $emp_raise = $emp_salary * .05;
else:
    $emp_raise = 0;
endif;

Case/Switch/Evaluate

COBOL

EVALUATE ERR_CODE
    WHEN 00
        DISPLAY 'ROW FOUND'
    WHEN +100
        DISPLAY 'ROW NOT FOUND'
    WHEN OTHER
        DISPLAY 'WHAT HAPPENED?'
END-EVALUATE

PHP

switch ($err_code) {
    case 0:
        echo "Row Found";
        break;
    case 100:
        echo "Row not found";
        break;
    default:
        echo "What happened?";
}

Loops

COBOL

COUNTER = 1
PERFORM UNTIL COUNTER > 5000
    DISPLAY 'COUNTER = ' COUNTER
    COUNTER = COUNTER + 1
END-PERFORM

PHP

for($counter = 1; $counter <= 5000; $counter++):
    echo "counter = ".$counter;
endfor;

Code Reuse / Copying

COBOL

COPY ILXAN001.

PHP

include ("ilxan0001");

(or)

require ("ilxan001");

Find/Replace

COBOL

INSPECT BAD-DATA REPLACING ALL 'XXX' BY 'YYY' TALLYING THE-COUNT
MOVE BAD-DATA TO GOOD-DATA
DISPLAY 'GOOD-DATA = ' GOOD-DATA ', NUMBER OF REPLACEMENTS - '
    THE-COUNT

PHP

$good_data = str_replace('XXX','YYY',$bad_data, $the_count);
echo "Good Data = ".$good_data.", Number of replacements - ".$the_count;

File access (sequential)

COBOL

(Assumes one record in the file)
OPEN INPUT IN-FILE
OPEN OUTPUT OUT-FILE
READ IN-FILE INTO IN-REC
WRITE OUT-REC FROM IN-REC
CLOSE IN-FILE OUT-FILE


(Multiple records)


OPEN INPUT IN-FILE
PERFORM UNTIL END-OF-INPUT = 'Y'
    READ IN-FILE INTO THE-RECORD
        AT END MOVE 'Y' TO END-OF-INPUT
    DISPLAY 'THE RECORD = ' THE-RECORD
END-PERFORM
CLOSE IN-FILE

PHP

(Assumes whole input file should be read and written)

$in_rec = file_get_contents('input.txt');
$out_file = fopen('output.txt', 'w');
fwrite($out_file, $in_rec);
fclose($out_file);


(Reading individual records)


$in-file = fopen('input.txt', 'r');
$contents = '';
while (!feof($in-file)) {
    $the_record = fread($in-file, 8192); // max size of record
    echo "The Record = ".$the_record
}
fclose($in-file);

Currrent Date

COBOL

ACCEPT SYSTEM-LONG-DATE FROM DATE YYYYMMDD.
DISPLAY 'DATE = ' SYSTEM-LONG-DATE

PHP

$system_long_date = Date("Ymd");
echo "Date = ".$system_long_date;

Boolean Switches

COBOL

SET DEBUG-ON TO TRUE
IF DEBUG-ON
    DISPLAY 'THIS IS A DEBUG STATEMENT'
END-IF

PHP

$debug = TRUE;
if ($debug):
    echo "This is a debug statement";
endif;

Arrays

COBOL

ARRAY-MAX = 10
PERFORM UNTIL SUB > ARRAY-MAX
    MOVE 'XXX' TO WS-TABLE-ROW(SUB)
    ADD 1 TO SUB
END-PERFORM

NOTE: Array index starts at 1. There are 10 elements in this array.

PHP

$array_max = 10;
$table_rows = array($array_max);
foreach($table_rows as $a_row){
    $a_row = 'XXX';
}

NOTE: Array index starts at 0. There are 11 elements in this array.

Subroutines

COBOL

    MOVE 'HI THERE' TO THING-TO-DISPLAY
    PERFORM AAAA-DISPLAY-SOMETHING


AAAA-BUILD-SOMETHING.
    DISPLAY THING-TO-DISPLAY

PHP

function DisplaySomething($thing_to_display) {
    echo $thing_to_display;
}

DisplaySomething("Hi There");

Concatenation

COBOL

STRING EMP-ID
       ', '
       EMP_NO
           DELIMITED BY SIZE INTO EMP-DETAILS
END-STRING

DISPLAY 'DETAILS = ' EMP-DETAILS

Unstring / Explode example


MOVE '123,456,789' TO THE-DATA
MOVE ',' TO DELIM
UNSTRING THE-DATA DELIMITED BY DELIM
    INTO DATA1, DATA2, DATA3
END-UNSTRING
DISPLAY 'DATA 1' DATA1
DISPLAY 'DATA 2' DATA2
DISPLAY 'DATA 3' DATA3

PHP

$emp_details = $emp-id.", ".$emp-no;
echo "Details = ".$emp_details

Unstring / Explode example

$the-data = '123,456,789';
$the-array = explode(",",$the_data);
echo "Data 1".$the-array[0];
echo "Data 1".$the-array [0];
echo "Data 1".$the-array [0];

Comments

COBOL

(Remember, COLUMN 8!!)

* THESE ARE SOME COMMENTS IN COBOL
* THEY ARE PRETTY

PHP

/**
* These are some comments in PHP
* They are pretty
*/

(or)

// These are some comments in PHP
// They are pretty

9 Replies to “PHP itself isn’t that much different from COBOL”

  1. I’m forced to use PHP, since the world has unfortunately moved in that direction. By comparison, PHP appears primitive. Is there any reliable converter that can convert COBOL to PHP?
    I’m used to writing structured COBOL code with the PERFORM targets lying out of sight, so the program flows almost declaratively (PERFORM UNTIL). It appears that PHP can’t do this, as everything is in-line. And true subroutines as in CALL ….. USING don’t seem to exist. However, a superior converter could solve all of these problems.

  2. Technically, a “CALL … USING” can be performed by using a “REQUIRE ‘xxxxx.PHP'” or “INCLUDE ‘xxxx.PHP'” which simply copies in the source of the PHP file into the executing code. PHP doesn’t have a compiler, so it is all interpretive and parsed.

    The PERFORM UNTIL’s I’ve worked with can be replaced with WHILE routines, but I believe I’m probably missing your point.

    I haven’t seen anything anything that COBOL can do that PHP can’t, unless it is specific to some peripheral software, maybe CICS, VSAM or DB2.

    If you post an example of the code you think doesn’t convert well to PHP, I’ll try to give a shot at it.

  3. Hello!

    Interesting discussion about COBOL and PHP.

    I can imagine how Tony feel about PHP and COBOL.

    I´m since a long time an application devoloper on the IBMs System i commercial plattform with focus on business critical applications in RPG and have COBOL experience too.

    At the moment I make my experience with PHP.

    Sure, languages seem to be similar. But in real life it´s not all about syntax and structure.

    PHP is a great chance. I could be the COBOL of the web world.

    But for business critical application there are some important leaks, that etablished plattforms and language deliver since more than 30 years.

    Things are missing:

    A real compiler (zend?).

    Specify a variable somewhere in the code is great. But don´t step in mission critical operations on a non defined variable. The whole shop (production, delivery, supply chain) could stop. I´m never sure if the deployed application is really neat. In an enterprise scale this is hard to manage.

    Missing persistence. On large plattforms main storage is organized by OS and not by applications. Nobody talks about a swap file-limit or storage managment.
    You can keep data connections and programs open as much you like.
    This simplify your work and stability. You don´t need to care about the business to coordinate reenter a program or to do the “keep variables in mind and restore them”-job with a special storage part like session variables. And this is very important.
    Because: All is session.

    Missing seperation business layer and presentation layer: Large programs in php are hard to maintain. On business critical application the focus is on the application code. This is on of the most important job that is missing in php. Sure there are lots of concepts, but there is not a real solution. Regarding of the skill level you can imagine how difficult it is to manage different programer skill levels and “dialects” in an enterprise application??

    The good the thing is: You can start quick an application with php and php has a lot of potential.

    Greetings from germany

    alex

  4. Is this straight batch COBOL to PHP? I don’t know any specific tools, but I have experience converting some processes.

  5. As a COBOL programmer being attracted to PHP. I found this very good. I deliver applications on web sites in COBOL using the CGI method and the basic concepts are similar to PHP.

    Here is snippet I really like of PHP code. To do this in Cobol requires many more lines of code.

    It names and then processes a text file of HTML code. It replaces certain variables and then displays the page to the user. It is so simple. This is also the basis for a Content Management System (CMS).

Leave a Reply