Skip to content
Advertisement

PHP Interoperability – Print statements not showing in command line

I am having issues with PHP not returning print statements from Fortran. The idea is that PHP executes a command to compile a Fortran file then runs it.

You are asked to input a number via print statement, problem is there is none showing up to CMD but the program is working as intended – when I enter an “incorrect” number it waits for me to enter a “correct” number then completes execution.

How do I get the command line to print the statements nested in the Fortran file?

The PHP Script

$<?php
// This is a script to test interoperability between Fortran and PHP
$ExecutionStringOne = "gfortran C:Users****DesktopProgramsFortran_ProgramsFibSequence.f95 -o C:Users****DesktopProgramsFortran_ProgramsFibSequenceTest";
$ExecutionStringTwo = "C:Users*****DesktopProgramsFortran_ProgramsFibSequenceTest.exe";
exec ($ExecutionStringOne);
exec ($ExecutionStringTwo);
?>

Fortran Code:

! This program will calculate the first 7 numbers in the Fibonacci sequence with user input as well as error checking
    Program FibSequence
        implicit none
        ! Defining our global variables 
        Integer :: acceptValue, n_terms          ! Accept Value: 0, 1 - This tracks whether input is correct (0 False, 1 True) , N_terms: Number of terms
        real    :: timeForSubRoutineStart, timeForSubRoutineEnd, totalTime ! Tracks execution time of subroutine
        write(*,*) "Please input an amount of terms to calculate sequentially to - This number can be between 1 and 7"
        read *, n_terms
        if (n_terms > 0 .AND. n_terms <= 7) then
            acceptValue = 1                      ! This is true therefore run program (This isnt really needed but for consistency)
            Call calculateSequence(n_terms)
        else
            acceptValue = 0                      ! This is false therefore run the read again and keep doing so until the output is correct
            do while ( acceptValue == 0 )
                write(*,*) "Invalid number please enter one within the given range - Range ( 1 - 7 )"
                read *, n_terms
                if (n_terms > 0 .AND. n_terms <= 7) then 
                    acceptValue = 1                     !Yay correct input
                    Call calculateSequence(n_terms)
                else
                    write(*,*) "Invalid number, try again" !Boo invalid input
                end if 
            end do
        end if
    End Program
    
    Subroutine calculateSequence(NumberOfTerms)
        Integer :: pt1 = 0, pt2 = 1
        Integer :: pt3
        Call CPU_TIME(timeForSubRoutineStart)
        i = NumberOfTerms
        
        do while (i > 0)
            pt3 = pt1
            pt1 = pt1 + pt2
            pt2 = pt3
            i = i-1
            write(*,*) "Point A Equals", pt1, "Point B Equals", pt2, "Calculated Next Number", pt3
            write(*,*) "Current Term = ", i
            write(*,*) "This program calculated this many points ", NumberOfTerms
            write(*,*) "End" 
        end do
        Call CPU_TIME(timeForSubRoutineEnd)
        totalTime = timeEnd - timeBegin
        write(*,*) "This program took ", totalTime, " seconds to execute"
    end Subroutine calculateSequence

enter image description here

EDIT – Added a new image regarding the issue @roygvib Suggested ECHO which is returning some print statements now

enter image description here

Advertisement

Answer

With a users help @roygvib I was able to find a solution to the problem.

PHP several different ways for executing files and commands – One as was used in the problem set is exec() which will execute the program and not return any output.

On the other hand there exists System() which will launch the given commands and executable and return any output that exists within the lifetime of the program execution.

TLDR: Using System instead of Exec resolved the issues I was having.

system ($ExecutionStringOne);
system ($ExecutionStringTwo);

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement