Is there a PHP syntax checker plugin for Notepad++?
Please don't answer "Use another editor instead"
Try NppExec plugin for Notepad++. Using it create a command to be something like this:
cmd.exe /K c:\your\path\to\php.exe -l "YOUR_FULL_FILE_NAME"
Instead of YOUR_FULL_FILE_NAME you should use appropriate Notepadd++ macro -- I think it is $(FULL_CURRENT_PATH) but double check with NppExec manual (installs together with plugin).
P.S.
But any IDE will be better for sure (I'm using PhpStorm). If IDE is too heavy for your PC then look for php-oriented editors, like Blumentals RapidPHP etc (it's lighter than full IDE but may have all really important features).
As LazyOne said above, you can use NppExec which you can install using the plugin manager (Plugins>Plugin Manager>Show Plugin Manager) You'll also need to have PHP installed. Lastly, the command I use to do PHP syntax checking with NppExec is
"C:\Program Files (x86)\PHP\php.exe" -l $(FULL_CURRENT_PATH)
I recommend you find a true IDE (not a glorified text editor). I've used Notepad++ for years but it can't do much beyond syntax highlighting.
I personally use PHPStorm (but it's not free, it is very good though :D ). You could also use NetBeans or Eclipse.
Adding to #LazyOne's answer:
I don't like NetBeans, it's too strict, has a tough time finding includes, and it's slow. I dig N++ for its speed and simplicity. I have php installed on my PC really just to run validation. If you're using N++ (or any other text editor) you can use the following Powershell script to batch check all of the files you've downloaded and are working on. Just fire up Powershell ISE, enter the correct path to check and PHP.exe path for your environment and the results get output to the ISE console.
cls
$pathToCheck = "C:\Users\BigDaddy\AppData\Local\Temp\fz3temp-1"
$phpExePath = "C:\PHP\php.exe"
Get-ChildItem $pathToCheck -Filter "*.php" | foreach {
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $phpExePath
$pinfo.Arguments = "-l", $_.FullName
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$output = $p.StandardOutput.ReadToEnd()
$output += $p.StandardError.ReadToEnd()
$output
}
I hope someone else finds this as useful as I have.
Cheers!
I use Komodo Edit 7 (free version) which has a built-in php syntax checker. I dunno how robust it is, but it works fine for me. I’m not a pro web designer, but I like it better then Eclipse and Bluefish. Komodo is smaller than Eclipse and more stable than Bluefish (in my Win XP environment).
PHP can syntax check your file using the -l lint option. Install PHP (if you haven't already) on your computer and use the Run function in Notepad++ and run a command like this:
cmd.exe /S /K ""C:\Program (portable)\php-8.0.12-Win32-vs16-x64\php.exe" -l "$(FULL_CURRENT_PATH)""
Change the path to your install location. After running it via Run you are able to save it, give it a name and assign a custom keyboard shortcut.
Breakdown of the command:
cmd.exe /S /K open a new Command shell which will stay open after executing, /S for being able to wrap the whole command to execute in cmd.exe in quotes
"C:\Program (portable)\php-8.0.12-Win32-vs16-x64\php.exe" launch php.exe
-l option of php.exe to use their lint service rather than executing
"$(FULL_CURRENT_PATH)" Notepad++ specific which gives the full path to the current open document
Related
Thank you for taking the time to help me today. I have what I hope is a simple question. I have been attempting to use php exec() or any related PHP command to Open up the Developer Command Prompt for Visual Studio 2013 and use it to compile a file and save the output to a file on my local machine. I have it working fine from Run on Windows, but I can't seem to get it to work with PHP exec(). Here is how I have the command set up currently.
$cmd = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat && cd C:\wamp\www\csc424Try3\app\uploads && cl /EHsc basic.cpp && basic >> C:\wamp\www\csc424Try3\app\outputs\output.txt';
exec($cmd, $result);
As you can see, I am chaining together commands. The first command allows the prompt to open, the second changes to the proper directory, the third runs the command for compiling in the prompt, and the fourth outputs to a text file.
Any ideas where I'm going wrong? I had a feeling it had something to do with formatting, but perhaps chaining the commands together does not work in PHP the way it does on the system.
You're forgetting quotes around most of your arguments in the exec call's string, meaning that things like Program Files will be seen as two separate things, not a single directory.
$cmd = '"C:\Program Files (x86)\....VsDevCmd.bat" && etc...';
^-- ^-- need these
as is, you're trying to execute a program called C:\Program, with some extra arguments like Files (x86)\......
I found the answer to my own question after some research. The way I was trying to do it before just wasn't working. After talking to a colleague, I thought writing a .bat file for the background would work better, and it absolutely did. I wrote the chain of commands in a .bat file and executed it like follows;
system("cmd /c C:\wamp\www\csc424Try3\app\uploads\stuff.bat");
Success! The file can now be successfully compiled and ran from PHP. :) It is a good day.
Suppose I have made a project on PHP-GTK. Now how can I run the PHP-GTK without opening the command prompt?
Like in other software, we have a icon on the desktop, by click on that we can run the program, I want to do the same with my program made in PHP-GTK.
You can make a .bat file which executes with a double click. If you are under windows, I guess there must be something similar for unix. This is how you do it for windows:
"%CD%\..\..\PhpGtkRuntime\php.exe" "%CD%\test.php"
Of course you will adjust it to your folder depth but the idea is the same.
You can also look this answer which is closely realated and talks about things of immediate interest: Check here
this solution workes for me on windows XP SP3
let the phpGtk binary be C:\monitor\bin\php\php and the phpGtk main script file to be C:\monitor\system\monitor ui\monitor.php
use a bach script (monitor.bat) file like this:
#echo off
rem Hideself
"C:\monitor\bin\php\php" "C:\monitor\system\monitor ui\monitor.php" %*
rem Hideself
pause
rem is for comments in commands line,
the %* part is to pass any arguments provided to the phpGtk script
use this bat to exe converter to convert Bash .bat file to an executable .exe
rem Hideself and rem Showself are advanced commands and
they are just what we need
with this program you can:
- set an icon,
- include files
- set a password for the executable
Hint: you can phpGtk binarys but you should use %MyFiles% to refer to them
Advice: when your project is all done you can trim the pause command (it is just to see the errors php cli logged to you)
Caution: using multiple icons needs .NET framework
Hint: you can use Ressource Hacker to replace the single icon with multiple ones
Hint: you can use the WinRAR's SFX feature to handle the installation for you
hope this help somebody's life ! :)
if you use Linux, you can create a shell script and put the command with & to run in the background?
[command] &
If you use Windows, try
start [command]
I'm currently developing a couple of plugins for Sublime Text 2 on OS X and I would like to make them cross platform, meaning I have to find out if and where php.exe is installed.
Right now I call /usr/bin/php in Python, which obviously works only on OS X and Linux:
phppath = '/usr/bin/php'<br>
pluginpath = sublime.packages_path() + '/HtmlTidy/tidy.php'<br>
retval = os.system( '%s "%s"' % ( phppath, scriptpath ) )
But on Windows, there seems to be no definitive default path for php.exe. The more I googled for it, the more possibilities showed up. So far I think I would have to check each of the following paths for existence:
c:\php\php.exe
c:\php5\php.exe
c:\windows\php.exe
c:\program files\php\php.exe
c:\wamp\bin\php\php5\php.exe
c:\xampp\php\php.exe
That's already quite a collection, but what I'm asking for is either a complete list covering all possibilities - or another way to figure it out that should be as robust as checking each possible path.
So if you have php.exe installed in some place other than these, please leave a comment with your path and I will add it to the list above.
Besides, there seems to be php.exe and php-cli.exe. I guess it would be OK to loop through each possible path. Check first for php-cli.exe, check for php.exe, and take the first match. Is that correct or is there a better practice?
If the user has added PHP's bin folder to the system PATH, then you should just be able to try and execute php -v to check that it's present.
If you want to obtain the full path to the PHP executable and the target system is Windows Server 2003 or later (so Windows Vista, and Windows 7) then you could use the WHERE command, i.e.:
C:\> where php.exe
C:\Program Files (x86)\WAMP\bin\php\php5.3.5\php.exe
Also see possibly related question: Is there an equivalent of 'which' on the Windows command line?.
If you are really desperate to find any file on the user's computer, you could try executing the equivalent of a find - but it's going to be slooow!
C: && cd \ && dir /s /b php.exe
On powershell or commad prompt
php -r "echo PHP_VERSION;" gives the version
See other PHP constantas:
https://www.php.net/manual/es/reserved.constants.php
to answer your question
php -r "echo PHP_BINARY;" gives the full path to php.exe
if needed, to remove the php.exe
php -r "echo str_replace('php.exe', '', PHP_BINARY);"
From PHP 5.4 and later, you can use the PHP_BINARY constant.
Use:
C:\xampp\php\php.exe
It should work if your XAMPP instance is on D: or E:. You change it accordingly from C:.
I found a way for you to find out where the php.exe is stored. In C:, click the search bar and search for "php.exe". Then, there will appear an application called php and click it. Then, cmd will open and in the cmd tab it will say php.exe at the end. That's where your php.exe is and you can put the link address of the file in VS Code.
Try
echo PHP_BINDIR;
It works. I tested it on macOS as with PHP 5.6.
Is there a command line php shell available for windows? I looking for something similar to the python shell, that will allow me to open and immediately begin executing code.
There is the windows command line for PHP: http://php.net/manual/en/install.windows.commandline.php
Try this:
<?php
$fh = fopen('php://stdin', 'r');
$cmd = '';
$bcLvl = 0;
while (true)
{
$line = rtrim(fgets($fh));
$bcLvl += substr_count($line, '{') - substr_count($line, '}');
$cmd.= $line;
if ($bcLvl > 0 or substr($cmd, -1) !== ';')
continue;
eval($cmd);
$cmd = '';
}
Save this code to a file (eg shell.php) and then run it from console:
php shell.php
Hit CTRL+C to exit.
Have a look at either running php.exe with the -a argument, or maybe the phpsh project.
Another simple variant, influenced by other answers. Create and run the following cmd-script:
#echo off
:loop
php.exe -r "while (true) { eval (fgets (STDIN) ); echo PHP_EOL; }"
goto loop
Immediate execution, Ctrl+C for exit. Insert correct path before "php.exe".
There is php-shell - it is not great, but still way better than php -a. (Also, it is dead simple to install, just run pear install http://jan.kneschke.de/assets/2007/2/17/PHP_Shell-0.3.1.tgz.) There is also phpa-norl, but I haven't tried it.
I found these two on github as well:
ubermuda/PHPInteractiveShell
https://github.com/ubermuda/PHPInteractiveShell
d11wtq/boris · non windows
https://github.com/d11wtq/boris
PsySH is the best REPL shell I have seen recently. Tab-completion, phpdoc support, proper namespace handling, interactive debugging, plugin support and many more. It is pure PHP and has composer support so it should be easy to install on Windows. I have only tried it on Linux but looking at the code, it seems to even have some history support on OSes without readline.
#Pavle Predic 's answer (https://stackoverflow.com/a/15742101/2752670) works for me on Windows.
But I want to use the shell.php file anywhere, so my solution is:
new and save shell.php in php installed dir (or where else you like), e.g. C:\Program Files\php-5.6.12-Win32-VC11-x64\shell.php
new a Windows environment variable, key: shell.php, value: "C:\Program Files\php-5.6.12-Win32-VC11-x64"
restart computer
use anywhere in the system:
php %shell.php%
Maybe answered - Read here: https://stackoverflow.com/a/19085620/2480481
I think i found easiest way to do it because im so interested to use myself :)
I try all options from here and from some other forums/pages... Windows cannot use readline.
I would like to know how if it's possible to validate if a PHP file is valid in VIM without closing VIM every time?
Thank you
You can execute shell commands in vim. This is the same as calling php -l filename.php from the shell:
:!php -l %
I have this mapped into my ~/.vim/after/ftplugin/php.vim file so that I only have to press F5:
map <F5> :!php -l %<CR>
Use :make with the following php specific settings:
:set makeprg=php\ -l\ %
:set errorformat=%m\ in\ %f\ on\ line\ %l,%-GErrors\ parsing\ %f,%-G
Your syntax errors will be in the Quickfix window. You can open this buffer with :copen or :cope for short. If you only want to open the window only if their are errors use :cwindow.
You can use :cnext and :cprev to move through the quickfix list to jump to the corresponding errors. I suggest Tim Pope's excellent unimpared.vim plugin to make moving through the list as simple as [q and ]q.
To simplify the workflow I suggest a mapping like this one:
nnoremap <f5> :update<bar>make<bar>cwindow<cr>
Now you can just hit <f5> and the buffer will be updated (if necessary), linted, and any errors will appear in the quickfix window.
To make this a bit more robust, add these commands to ~/.vim/after/ftplugin/php.vim. Example ~/.vim/after/ftplugin/php.vim
setlocal makeprg=php\ -l\ %
setlocal errorformat=%m\ in\ %f\ on\ line\ %l,%-GErrors\ parsing\ %f,%-G
nnoremap <buffer> <silent> <f5> :update<bar>sil! make<bar>cwindow<cr>
For more information:
:h quickfix
:h makeprg
:h errorformat
:w !php -l
The real credit goes to Can I see changes before I save my file in Vim? for the idea so up vote there.
But to explain on this post (mostly taken from above):
The above command works as follows:
The syntax for saving a file in vim is:
:w <filename>
The syntax for executing a shell command in vim is:
:!<command>
Executing the save command without a filename but rather a shell command behind it causes vim to write the files content to stdin of the shell instead of saving it in a physical file. You can verify this by executing
:w !cat
This should always print the files current content (which would have been written to a file instead).
4 You can check code with php -l from STDIN by piping it in
The file is "saved" to stdin, php lint is run with the stdin as input.
To check PHP syntax without having to save first you can use:
map :w !php -l
http://vim.wikia.com/wiki/Runtime_syntax_check_for_php
You should try a plugin like Neomake. It will show you in the gutter error markers for every PHP syntax errors. Go on the line to see the error itself. You can as well link it to PHP Mess detector and PHP Stan to show you errors and possible improvements in your code.
This combo is very powerful!
In case you are interested I wrote an article how to make a Vim PHP IDE. This is basically a list of essential plugins you should try out! Of course Neomake is part of it.
For just syntax highlight (what sometimes gives clues about errors) a non yet saved file (usually black and white on vim) just source the syntax file:
:source $VIMRUNTIME/syntax/[the syntaxfile].vim
Examples
:source $VIMRUNTIME/syntax/sh.vim
:source $VIMRUNTIME/syntax/php.vim
:source $VIMRUNTIME/syntax/javascript.vim
You can manually run the PHP linting commands, but if you're doing full time development in PHP then it's easier to use a generic syntax checking plugin.
Syntastic (as recommended by #lucapette) has long been the main syntax plugin for Vim with fairly relaxed requirements:
Syntastic itself has rather relaxed requirements: it doesn't have any external dependencies, and it needs a version of Vim compiled with a few common features: autocmd, eval, file_in_path, modify_fname, quickfix, reltime, statusline, and user_commands. Not all possible combinations of features that include the ones above make equal sense on all operating systems, but Vim version 7 or later with the "normal", "big", or "huge" feature sets should be fine.
Since Vim 8, which allows for asynchronous syntax checking, there are now two good plugins. These work by continuously linting your code so you don't have to save your code for errors to show up:
Asyncronous Lint Engine (ale)
Neomake (as recommended by #matthieu)
All of these should automatically call php -l for the current file as long as the syntax for that file is set to PHP (:set syntax=php if Vim doesn't automatically recognise the syntax).