H. Shindoh H. Shindoh - 1 month ago 12
PHP Question

Build system of Sublime Text 3 executes a different version of PHP

I am using SublimeText3 on OSX. The problem is:


  • If I execute PHP script on SublimeText3, then it is executed by PHP 5.5.

  • But if I execute the same script from shell (iTerm), then it is executed by PHP 7.



How can I execute a script with PHP7 on SublimeText3?

The build system is following.

{
"cmd" : ["php", "$file"],
"file_regex": "php$",
"selector" : "source.php"
}


And the PHP script is just:

<?php
phpinfo();
?>

Answer

Specify the absolute path to the command (cmd).

Let's say $file is /path/to/file, then the command ['php', "$file"] expands to php /path/to/file. This is like running the following on the command line:

$ php /path/to/file

Because the command (php ) is relative, the system path (on linux the system path is in the environment variable PATH) is searched to find php.

You can specify an absolute path for the command. Let's say you have the following versions installed:

  • /path/to/php/versions/7.0.0/bin/php
  • /path/to/php/versions/5.5.0/bin/php

Then you can configure a command to use v7.0.0:

{
    "cmd" : ["/path/to/php/versions/7.0.0/bin/php", "$file"],
    "file_regex": "php$",
    "selector"  : "source.php"
}

And v5.5.0:

{
    "cmd" : ["/path/to/php/versions/5.5.0/bin/php", "$file"],
    "file_regex": "php$",
    "selector"  : "source.php"
}

And so on...

Further reading