DEV Community

Zxce3
Zxce3

Posted on

2

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
$code = '<?php
$code = %s;
printf($code, 39, $code, 39);
';
printf($code, 39, $code, 39);
Enter fullscreen mode Exit fullscreen mode

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

<?php
$a = '<?php $a = %s; echo sprintf($a, var_export($a, true));';
echo sprintf($a, var_export($a, true));
Enter fullscreen mode Exit fullscreen mode

Uses var_export() to generate a string representation of the code variable. This representation gets inserted back into the original template.

Function-Based Quine

<?php function q() { $v = get_defined_vars(); echo "<?php\nfunction q() {\n \$v = get_defined_vars();\n echo \$v['s'];\n}\n\$s = " . var_export($v['s'], true) . ";\nq();\n"; } $s = "<?php\nfunction q() {\n \$v = get_defined_vars();\n echo \$v['s'];\n}\n\$s = " . var_export($s, true) . ";\nq();\n"; q();
Enter fullscreen mode Exit fullscreen mode

Leverages get_defined_vars() to access its own variables. More complex but demonstrates PHP's introspection capabilities.

File Reading Quine

<?php echo file_get_contents(__FILE__);
Enter fullscreen mode Exit fullscreen mode

Often considered "cheating" in quine competitions. Simply reads its own source code using PHP's __FILE__ magic constant.

Native Functions Quine

<?php
echo html_entity_decode(strip_tags(highlight_file($_SERVER['SCRIPT_FILENAME'], true)));
Enter fullscreen mode Exit fullscreen mode

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);
?>
Enter fullscreen mode Exit fullscreen mode

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)?>")')?>
Enter fullscreen mode Exit fullscreen mode

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.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay