Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I to run php script from powershell-commandline?

Tags:

php

powershell

How can i setup my powershell to run php scripts in like a commandcall like this

php test.php

I'm able to do this on a server at work which I connect to by putty, but would be nice if I was able to execute those scripts directly from my own without having some server running. I also know about Xaml, which I don't like since its require you to refresh some browser.

like image 405
Wisienkas Avatar asked Sep 23 '13 16:09

Wisienkas


People also ask

How do I run a script in a PowerShell script?

To run a script, open a PowerShell window, type the script's name (with or without the . ps1 extension) followed by the script's parameters (if any), and press Enter.

Can PHP be used for command line scripts?

As of version 4.3. 0, PHP supports a new SAPI type (Server Application Programming Interface) named CLI which means Command Line Interface. As the name implies, this SAPI type main focus is on developing shell (or desktop as well) applications with PHP.


2 Answers

You can do all the stuff suggested above or ...

Go to php.net and download the php file stack for windows.

Copy the file stack into say c:\php or if you want multiple versions, say c:\php5 or c:\php7 etc.

Open powershell and type c:\php\php.exe -h, you will get the php help output. Yay you are up and running, whoot.

(Note: you may need to rename php.ini.development -> php.ini

Advanced instructions:-

Type env into os search (cortana) and select environmental variables.

Add your php location to path (c:\php) and create a variable php (or php5 etc) pointing to c:\php\php.exe

Now you can run php in powershell with php (php -h to test).

Note: while not the question, this also works in the git bash shell.

like image 136
lucsan Avatar answered Oct 20 '22 00:10

lucsan


I'm assuming windows since you said powershell. You can just install php on windows but that means also installing apache or enabling IIS.

Or there's apparently a built-in webserver for command-line functionality that might minimize the amount of headache involved in configuring that stuff.

This might help get you going also: http://php.net/manual/en/install.windows.legacy.index.php#install.windows.legacy.commandline

like image 20
Neil Neyman Avatar answered Oct 19 '22 22:10

Neil Neyman