2

I have a script which would need to understand whether it runs under HHVM or the standard Zend engine.

Using zend_version() outputs different version tags - 2.5 for PHP5 and 2.4.99 for HHVM master as of current, and I don't feel safe using this method at all due to potential overlaps.

Using phpversion() just gives me the supported PHP version, again I don't see this as safe due to potential overlaps.

What would be the best way to safely determine exactly which runtime the script is executing on?

csvan
  • 8,782
  • 12
  • 48
  • 91
  • Safest way would be not to check this at all. Ask yourself why you need this check and find another way, for instance by checking if some functionality you want to use is available (function_exists, package_exists). – GolezTrol Jul 15 '14 at 14:55
  • I know, but in this case it is necessary since Zend and HHVM implement the same functionality in mutually exclusive ways, causing code running under HHVM to break under php5. For details on the issue, see this ticket: https://github.com/facebook/hhvm/issues/1391 – csvan Jul 15 '14 at 14:58
  • 1
    Closely related: http://stackoverflow.com/questions/20777815/how-to-programmatically-check-if-running-on-hhvm – Quasdunk Jul 15 '14 at 14:58
  • ...and downvotes, with no motivation whatsoever. Thank you for smearing the community. – csvan Jul 15 '14 at 14:59
  • Check what `php_sapi_name()` returns. – Marek Jul 15 '14 at 15:03
  • 1
    *Please do not downvote silently. I don't see anything wrong with this question. Please at least explain why you downvoted.* – Quasdunk Jul 15 '14 at 15:03
  • @Marek - it just gives 'ncli' for both when running from terminal. – csvan Jul 15 '14 at 15:08
  • @Quasdunk - that question indeed solves the specific problem differentiating between HHVM and Zend, which is what I was looking for. It would be nice to be able to find a general approach working for other potential runtimes as well, but if not, I would be happy to either accept and answer from you or have this marked as a duplicate. – csvan Jul 15 '14 at 15:10

1 Answers1

1

I might suggest testing for the actual behavior itself, not what platform you're using. Even if you can determine whether you're running PHP or HHVM, you'll end up needing to know the precise version once either PHP or HHVM fixes / changes this behavior. You can use e.g. the code in this GitHub comment and compare its output to test which type of behavior your platform exhibits.

This is the code which can distinguish the behaviors:

<?php
function start_elem($parser,$name,$attribs) {
   echo "<$name>";
}
function end_elem($parser,$name)
{
   echo "</$name>";
}

$parser=xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_set_element_handler($parser,"start_elem","end_elem");
$buf = '<F>';
echo xml_parse($parser,$buf,strlen($buf)==0);

Alternatively, you could also just use the answer to this question, which indicates that HHVM defines HHVM_VERSION whereas PHP (obviously) does not, so you can use that to determine the runtime.

Community
  • 1
  • 1
CmdrMoozy
  • 3,870
  • 3
  • 19
  • 31