PHP Quines: Self-Replicating Code Explained
PHP offers several ways to create quines - programs that output their own source code. Here's a technical dive into these self-replicating curiosities. What Are Quines? Quines are programs that produce their exact source code as output. While rarely practical, they present interesting programming challenges and demonstrate code introspection capabilities. Simple Print Quine

PHP offers several ways to create quines - programs that output their own source code. Here's a technical dive into these self-replicating curiosities.
What Are Quines?
Quines are programs that produce their exact source code as output. While rarely practical, they present interesting programming challenges and demonstrate code introspection capabilities.
Simple Print Quine
$code = ';
printf($code, 39, $code, 39);
This basic quine stores its own source in $code
with a placeholder. The printf()
function replaces the placeholder with the code itself wrapped in quotes.
var_export() Quine
$a = ';
echo sprintf($a, var_export($a, true));
Uses var_export()
to generate a string representation of the code variable. This representation gets inserted back into the original template.
Function-Based Quine
function q() { $v = get_defined_vars(); echo "\nfunction q() {\n \$v = get_defined_vars();\n echo \$v['s'];\n}\n\$s = " . var_export($v['s'], true) . ";\nq();\n"; } $s = "\nfunction q() {\n \$v = get_defined_vars();\n echo \$v['s'];\n}\n\$s = " . var_export($s, true) . ";\nq();\n"; q();
Leverages get_defined_vars()
to access its own variables. More complex but demonstrates PHP's introspection capabilities.
File Reading Quine
echo file_get_contents(__FILE__);
Often considered "cheating" in quine competitions. Simply reads its own source code using PHP's __FILE__
magic constant.
Native Functions Quine
echo html_entity_decode(strip_tags(highlight_file($_SERVER['SCRIPT_FILENAME'], true)));
A creative approach by Tim Bond using PHP's syntax highlighting and then cleaning the output.
ASCII Manipulation Quine
$a='chr(60).chr(63).chr(10).chr(36).chr(97).chr(61).chr(39).$a.chr(39).chr(59).chr(10)."echo $a;".chr(10).chr(63).chr(62)';
echo chr(60).chr(63).chr(10).chr(36).chr(97).chr(61).chr(39).$a.chr(39).chr(59).chr(10)."echo $a;".chr(10).chr(63).chr(62);
?>
Uses ASCII character codes to represent and reconstruct its own source. Harder to read but demonstrates another technique.
Arrow Function Quine
=(fn($s)=>"=$s(\x27$s\x27)?>")('(fn($s)=>"=$s(\x27$s\x27)?>")')?>
A modern PHP quine by Benoit Viguier using arrow functions. Compact and elegant with no semicolons.
Technical Application
Quines have limited practical use but demonstrate language features like:
- String manipulation
- Variable interpolation
- Self-reference capabilities
- Code as data principles
They're useful for understanding language internals and compiler theory. They represent fixed points in computation - a fascinating theoretical concept.
Further Exploration
Try modifying these examples to create "quine variants" like:
- Radiation-hardened quines that can recover from corruption
- Polyglot quines that work in multiple languages
- Multiquines that transform into other programs on subsequent runs
These self-replicating programs offer a unique window into the fundamentals of computation.