Quine
A quine is a self-replicating computer program. It is a piece of source code that, when executed, produces its own exact source code as output and nothing else, without reading any file, argument, or external input. The concept is named after the philosopher Willard Van Orman Quine, who studied indirect self-reference, and is closely tied to the diagonal lemma and fixed-point theorems in mathematical logic and computability theory, which guarantee that such self-referential constructions are always possible in any sufficiently expressive programming language. Writing a quine is a classic recreational programming exercise and a popular subject of programming-language golf, because the naive approach of simply printing the source code as a string runs into an infinite regress, the string literal representing the code would itself need to contain a representation of itself, which is usually solved with a technique that stores the code as data, then uses string formatting or escaping to reinject that data into itself when printing. This is commonly done with sprintf-style placeholders and character codes to represent quotes safely. Beyond being a curiosity, quines are occasionally cited as a conceptual illustration of how self-modifying or self-propagating code, including malware, can be constructed, which is why some sources flag them as a security-adjacent topic.
<?php
$code = '<?php
$code = %c%s%c;
printf($code, 39, $code, 39);';
printf($code, 39, $code, 39);
See Also
- PHP Quine
- PHP Quines: Self-Replicating Code Explained
- Quine (ath.cx)
- Why Self-Replicating Code (Quines) in PHP Is a Security Red Flag