Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeroenvanrensen's avatar

Invoke Rust from PHP

Hello,

I would like to write a program in Rust that takes a string as its argument, and returns a string.

I want to invoke this program from PHP.

For example something like this (I don't know the correct syntax):

fn main(input: String) -> String {
    return format!("{} Something", input);
}
<?php

include 'program';

echo main('Some input');
0 likes
3 replies
OussamaMater's avatar
Level 37

I have never used rust but I have done something similar with PHP and C.

What you want to do is making your rust program accepts command line arguments, so you can do something like this

./executable string # where executable is your compiled rust program and string is the argument

now you capture that string argument and pass it to what ever function you want, refer to this documentation

Since your rust program is able to accept console argument, you can call the binary using PHP and pass console arguments, in others words you will be able to pass that string to the rust binary via PHP, like so:

$argument = "string"; // this is the console argument that rust binary will take
exec('/path/to/rust/binary ' . $argument . ' 2>&1', $output); # and you will have the output of the execution saved in the $output variable

Hopefully the idea is clear.

3 likes

Please or to participate in this conversation.