Skip to content

Instantly share code, notes, and snippets.

@wakhub
Created April 28, 2012 16:32
Show Gist options
  • Save wakhub/2520108 to your computer and use it in GitHub Desktop.
Save wakhub/2520108 to your computer and use it in GitHub Desktop.
PHP and Perl and Python/Ruby polyglot
#<?php eval('echo "PHP Code\n";'); __halt_compiler(); ?>
print ((("b" + "0" == 0) and eval('"Perl Code\n"')) or (0 and "Ruby Code\n" or "Python Code"));
__DATA__ = 1
"""""
__END__
===== This is comment of all. =====
outputs:
$ perl polyglot.pl.php.py.rb
Perl Code
$ php polyglot.pl.php.py.rb
#PHP Code
$ python polyglot.pl.php.py.rb
Python Code
$ ruby polyglot.pl.php.py.rb
Ruby Code
$
"""
@SaswatPadhi
Copy link

Yes, not perfect .. But good effort anyways! :-)

@joshyrobot
Copy link

If the eval(...) in the PHP section doesn't do anything, it can be compressed and the hash can be removed.
#<?php echo "\x08PHP Code\n";__halt_compiler();?>

@alexd2580
Copy link

you can run php with the output_buffering parameter enabled php --define output_buffering=On $file.
then in the first line fo your polyglot you can use ob_end_clean() to drop everything that was printed before and __halt_compiler() to not even read the rest, thus yielding a "real" PHP on stdout, without magic/invis characters. e.g.:

#define X[<?php ob_end_clean();echo"PHP\n";__halt_compiler();#print(("ALGOL68",newline))COMMENT]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment