How to run php in interactive CLI in Windows - php

I would like to run PHP in a command-line-interface and I would like to get output after every line I write.
I have php.exe in my PATH environment variable. I use php -r "echo 'Hello world'" or
php -a
Interactive mode enabled
<?php
echo "hello world"
?>
^Z
But I want a real php shell, which reacts after every command. I have used something similar in Python. Is this possible in PHP? You can recommend a program that has this feature.

For a powerful interactive PHP CLI, I suggest you take a look at PsySH. It's very easy to install, you just have to run one command:
composer g require psy/psysh:#stable
Then make sure you have added the composer vendor directory to the PATH system variable (instructions) by appending the following:
;C:\Users\[username]\AppData\Roaming\Composer\vendor\bin\
Then start it just run:
psysh
If you want an even console better experience, I suggest you use cmder instead of the Windows Command Prompt.

As of PHP 5.1.0, the CLI SAPI provides an interactive shell using the -a option if PHP is compiled with the --with-readline option.
Using the interactive shell you are able to type PHP code and have it executed directly.
You need to run the program whith the modifier "-a"
php -a
Then, you need to recompile php whith "--with-readline" to enable this feature and works like in phyton.
I hope I have been helpful.

Related

Link php interpreter from Docker to bash somehow

I'm trying to use my docker php as an interpreter in my terminal. What I need:
I don't keep my dev envs on my host os. That's important to keep it isolated
It should be available as $php or $/usr/bin/env php
I'd like to be able to run something like phpcs in my vim. It requires that thingy above.
I've tried this:
alias php='docker-compose exec php php'
But it's not available through /usr/bin/env
/usr/bin/env tries to locate the executable via the $PATH variable, see https://unix.stackexchange.com/a/12749
So if you put your command in to a script called PHP and add it to your $PATH before other PHP executables, it might work.

How can I run php -a (interactive shell) from inside a php script?

I'm trying to create a shortcut script for setting some environment variables and the include path before running php -a. So it looks like this:
#!/usr/bin/env php
<?php
putenv("ENVVAR=value");
passthru("php -a -d include_path=<path>");
This mostly works, but the php shell doesn't correctly output its prompt.
I.e. normal output from php -a:
$ php -a
Interactive shell
php > echo "hi\n";
hi
php >
Output from this script:
$ ./myscript.php
Interactive shell
echo "hi\n";
hi
Additionally, there is no support for walking through the history (or even right or left along what's already been typed) with arrow keys.
Is there a way to get this to work correctly?
I'm using Mac OS X, PHP 5.4.27. I have already tried redirecting stderr to stdout, in case that was somehow the cause (it wasn't.)
Rather than writing it in PHP, why not do it as a shell script.
#!/bin/bash
export ENVVAR=value
php -a -d include-path=whatever
I'm using bash, and it works fine under Ubuntu 12.04. I'm not sure what shell is available on OSX.

Windows 8: .phar files, how do you want to open

I'm trying to run composer on windows with wamp. I installed composer using the cmd prompt, and now I'm trying to run "composer update" for an SDK. However, when I type in "composer.phar update," windows asks what app I want to use to run this program. I want the command prompt to deal with it! How do I just run it through cmd, without this "what app" window coming up?
You have to set php.exe as your default application for phar files.
.phar stands for PHP Archive
Usually .phars take some arguments, so they are intended to be run from command prompt. Linux/BSD/OS X shell or Windows command prompt.
Linux .phar use case scenarios assume .phars are copied to some /bin and renamed to be without .phar extension, so you can use a php archive as if you would use any other linux command. So I recommend following way of doing the same thing with Windows:
Put all your .phar files to one directory like C:\php\phars
Add C:\php\phars to system environment variables (right-click my Computer -> Properties -> Advanced System Settings -> Environment variables)
Start the elevated command prompt (find command prompt in start menu then right-click and select Run as Administrator)
Type the following commands, replacing the path C:\phpdev\php\php542\php.exe with full path to your PHP executable:
ftype PHARFile=C:\phpdev\php\php542\php.exe "%1" %*
assoc .phar=PHARFile
set PATHEXT=%PATHEXT%;.PHAR
Next time your should be able just to run Windows console (keyboard Win+R and type cmd.exe) and type any of your .phar's like apigen.phar followed by any command and it will work
C:\Users\acosonic>apigen.phar help
Usage:
...
Arguments:
command The command to execute
command_name The command name (default: "help")
Options:
--xml To output help as XML
--format To output help in other formats (default: "txt")
--raw To output raw command help
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--version (-V) Display this application version.
Help:
The help command displays help for a given command:
php C:\phpdev\phars\apigen.phar help list
You can also output the help in other formats by using the --format option:
php C:\phpdev\phars\apigen.phar help --format=xml list
To display the list of available commands, please use the list command.
C:\Users\acosonic>
So this way lets you run .phar archives in a directory where you need to work, for example generating documentation in C:\myproject\controller without specifying full path to .phar as if you would if it's run without adding it to Windows path.
To explain what commands in step 4 did:
Created mapping HKCR.phar → HKCR\PHARFile
Created HKCR\PHARFile\shell\open\command = 'php.exe "%1" %*' [REG_EXPAND_SZ]
Extended HKCU\Environment\PATHEXT = '%PATHEXT%;.PHAR' [REG_EXPAND_SZ]
*.phar gets treated like binary/script, and *.phar execution works as long as a *.phar file is located anywhere in %PATH%.
One can wrap php *.phar with *.bat, then the filename will be the name of the CLI command:
#ECHO OFF
php "C:/Program Files/PHAR/phpDocumentor.phar" %*
One can also use such a wrap to pass along default arguments; eg. wp.bat:
#ECHO OFF
php "C:/Program Files/PHAR/wp-cli.phar" --path="D:/SDK/wordpress" %*
Where pattern %* will capture and forward CLI arguments, as it is supposed to.
Alike this one can run phar alike any other CLI command, in a terminal window.
Keep it simple. Instead of changing .phar's default program, which is not always easy in Windows, try just typing "php" in front of your command.
php composer.phar update

Is #!/usr/bin/env required to run PHP from command line?

Often times when I see PHP that is meant to be ran from the command line, it will have this line #!/usr/bin/env php at the top of the file like this...
#!/usr/bin/env php
<?php
// code
?>
I was wanting to know if this is meant just for when the file is ran on a Linux/Unix system or is needed for running on Windows as well?
The shebang line is required for auto-detection of the type of script. It enables this sort of usage:
[pfisher ~]$ chmod +x run-me.php
[pfisher ~]$ ./run-me.php
That line is not needed if you pass the filename as an argument to the php interpreter, like so:
[pfisher ~]$ php run-me.php
Edit: replace "hashbang" with shebang.
No it's not, you can directly use
#!/path/to/php
Running php (or anything else) through the env utility is a weak security measure. Dpending on the platform, will "fix" PATH, LIB, and other environment variables according to various config files and potentially remove some of the dangerous values in there (e.g. env on HPUX).
It is also to limit the scope of shell-expansions on certain environments. (See man 1 env on Linux).
the magic belongs to the executable flag (chmod +x FILE)
the shebang: and if it exists with that version you may expect
/usr/bin/env cliVersion -> /usr/bin/env php
php -v # tells you which version you have by default as: 'cli' in a shell
alternativly use e.g:
php8.0 /to/php/script.php
to run it without a shebang at the first line of the script.
(it will work even if it stays there but check the real php version on execution if important for you)
"standard"? over the last 10 years is /usr/bin/env depending on what version you set to be the default on your system:
debian systems (debian,unbuntu,kubuntu...): #root: update-alternatives --config php will guide you

How can I run a php without a web server?

I would like to ask if I can run a php without having installed a web server. Do I have to use the php like CGI and run my page via command line? And if so, what are the steps that I do I have to choose through the installation of php? I mean the preferences as CGI and the components after that step?
I installed the php 5.3.3 but is seems not working, I get several message that the php5ts.dll is missing and when I put that file in my ext folder other error messages appear. Are there any configuration files or steps that I have to use?
(is php 5.3.3 suitable for doing something like this?)
If I have to have a web server installed how can I run my php code through the command line?
You should normally be able to run a php file (after a successful installation) just by running this command:
$ /path/to/php myfile.php // unix way
C:\php\php.exe myfile.php // windows way
You can read more about running PHP in CLI mode here.
It's worth adding that PHP from version 5.4 onwards is able to run a web server on its own. You can do it by running this code in a folder which you want to serve the pages from:
$ php -S localhost:8000
You can read more about running a PHP in a Web Server mode here.
For windows system you should be able to run php by following below steps:
Download php version you want to use and put it in c:\php.
append ;c:\php to your system path using cmd or gui.
call $ php -S localhost:8000 command in a folder which you want to serve the pages from.
PHP is a normal sripting language similar to bash or python or perl. So a script with shebang works, at least on linux.
Example PHP file:
#!/usr/bin/env php
<?php
echo("Hello World!\n")
?>
How to run it:
$ chmod 755 hello.php # do this only once
$ ./hello.php
You can use these kind of programs to emulate an apache web server and run PHP on your computer:
http://www.wampserver.com/en/
http://www.apachefriends.org/en/xampp.html

Categories