skip to Main Content

I am trying to run a seed command for an application but windows power shell runs it with PHP 5, when the app is running on PHP 7. I need it to run that one command assuming php as version 7, that is, without changing the version of PHP powershell is using permanently.

I have tried all the solutions I’ve managed to find online, but unfortunately the majority of them are targeted to Linux systems, not windows.
This includes:

  1. editing composer.json/lock;
  2. defining an alias of php pointing to the path of the version 7 and using it to run the command;
  3. altering the PHP version using PHP manager in IIS

Among others.

Is this a possible thing to do? No matter what I do, my seed command always assumes php5 as the one to use, which results in several “syntax” errors namely complaining about the double question mark “??” null syntax only introduced in PHP 7.

4

Answers


  1. Chosen as BEST ANSWER

    The way I solved it was by adding in my script files "set PATH=C:Program FilesPHPv7.0" before the instructions.

    Also needed to add in my environment variables the parameters to accept CMD commands in Powershell.

    This was my script file:

    set PATH=C:Program FilesPHPv7.0
    
    cmd "/C php artisan (...)
    

  2. First of all you must have php 7 installed.

    Then in windows:

    1. Go to Control Panel and open the System icon (Start → Control Panel)
    2. Go to the Advanced tab
    3. Click on the ‘Environment Variables’ button
    4. Look into the ‘System Variables’ pane
    5. Find the Path entry (you may need to scroll to find it)
    6. Double click on the Path entry
    7. replace your PHP 5 directory path with php 7 path)
    8. Press OK
    9. Open terminal again and try again.
    Login or Signup to reply.
  3. You could use different version of PHP in IIS side by side.

    IIS support to specify different version PHP-CGI.exe for each sub-application or folder by following steps.

    https://learn.microsoft.com/en-us/iis/application-frameworks/install-and-configure-php-applications-on-iis/using-fastcgi-to-host-php-applications-on-iis

    You could map PHP5 as global Version and create a sub-application to handle the specific Powershell script, then set module mapping to PHP 7 for this sub-application’s handler.
    So that the specific page with PHP 7 will not interfere global application.

    Login or Signup to reply.
  4. Consider we have "C:php" directory which includes 3 sub-directories for 3 version of php: "C:php56" , "C:php74" and "C:php81".

    1. Open PowerShell as an administrator.
    2. Run notepad $PROFILE command to open your PowerShell profile in Notepad.

    If you don’t have a PowerShell profile yet, this command will create a new one for you.

    1. Add the following lines to your PowerShell profile:
    $phpVersionsPath = "C:php"
    
    function Set-PHPVersion {
        param([string]$version)
        $phpPath = Join-Path $phpVersionsPath $version
        $env:PATH = "$phpPath;{0}" -f $env:PATH
        $output = Invoke-Expression 'php -v'
        Write-Output $output
    }
    
    1. Save and close your PowerShell profile.
    2. Restart PowerShell to load the updated profile.

    After following these steps, you should be able to use the Set-PHPVersion function from any PowerShell session, regardless of the current working directory.

    Run Set-PHPVersion 56 , Set-PHPVersion 74 or Set-PHPVersion 81 to use the version you want.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search