Creating a PHP, Go, and Python Quine Relay
Source: Dev.to
Introduction
I have created a Quine relay involving PHP, Go, and Python, and I’ll explain how it works step by step.
PHP Quine (QuineRelay.php)
<?=eval($_='print "package main;import\42fmt\42;func main(){fmt.Print(\140print(r\47\47\47<?=eval(\44_=\47$_\47);\47\47\47)\140)}";');
Octal Notation
In PHP double‑quoted strings, sequences like \42 are interpreted as octal ASCII codes:
| Octal | ASCII | Character |
|---|---|---|
| 42 | 34 | " (double quote) |
| 47 | 39 | ' (single quote) |
| 140 | 96 | ` (backtick) |
Using octal notation avoids the need to escape these characters when they appear inside Go or Python strings.
How the PHP Code Works
- The variable
$_holds a string (enclosed in single quotes) that contains the Go source code, with octal escapes. eval($_=…)first evaluates the outer PHP string, converting octal escapes to their characters.- Inside the double‑quoted string,
$_is expanded, inserting the same PHP source code into the generated Go program—this self‑reference is the key to the quine.
Running the PHP script outputs the following Go code.
Generated Go Code
package main;import:"fmt";func main(){fmt.Print(`print(r'''<?=eval($_='print "package main;import\42fmt\42;func main(){fmt.Print(\140print(r\47\47\47<?=eval(\44_=\47$_\47);\47\47\47)\140)}";');''')`)}
- The Go program is written as a one‑liner using semicolons (the Go compiler inserts them automatically).
- Backticks are used for the string literal, allowing both single and double quotes inside without escaping.
Executing this Go program prints the Python code below.
Generated Python Code
print(r'''<?=eval($_='print "package main;import\42fmt\42;func main(){fmt.Print(\140print(r\47\47\47<?=eval(\44_=\47$_\47);\47\47\47)\140)}";');''')
- The triple‑quoted raw string (
r'''…''') lets us keep the octal escapes intact and avoid further conversion. - Running the Python script reproduces the original PHP source.
Back to PHP
Running the Python code yields the original PHP quine:
<?=eval($_='print "package main;import\42fmt\42;func main(){fmt.Print(\140print(r\47\47\47<?=eval(\44_=\47$_\47);\47\47\47)\140)}";');
Summary
A three‑language Quine relay may seem daunting, but by leveraging octal notation and careful string quoting, the implementation becomes straightforward. I hope this article helps you explore and write more quines! 🙂