Having a python interpreter is really useful as it lets you quickly type in few commands and verify output; making it easier to understand syntax.
However, in PHP, every time I want to try something out I have to - create a PHP script, save it, run it in my browser.
Is there a shortcut that I am missing out on for PHP that would make things simpler?
You can run php CLI in interactive mode ;)
php -a
in interactive mode you need to type the script with start and end tags
<?php echo 'Hello!'; ?>
Use PHP Designer. It will make your job much easier.
Or you could use phpUnit , unit testing for php
Related
There is a console program for morphological analysis of text https://tech.yandex.ru/mystem/.
Work with the program from the console is simple:
./mystem -lwd in.txt out.txt
I need to carry out a morphological analysis of the text by means of PHP, use the exec () function is impossible.
The easiest way, using PHP to write text to a file, then using a bash script and cron to analyze and save into another file.
But I would like a more convenient option as a PHP function mystem (string, param).
Compile and install extensions I can, but do not know how to write them.
I think the problem is very simple, because maybe someone is faced with the decision of this problem is the "fish"?
Total my question is, Does anyone have a ready solution where you can change only the name of the console program and the expansion will be ready? Thank U!
I've been working on a CMS for a few years and I actually implemented a jquery-based console in the admin area, where you could do some handy things like enable/disable modules and so on.
I recently fiddled around with drupal and decided to install cygwin along with drush.
I tried googling around but figured this might be an unusual question: How does one go about creating a CLI for a php-based CMS? And exactly how does exactly drush worK? I mean, I know that it runs from the command line as a batch script in windows. But how does it interact with PHP and so on?
I do know some basic C# but this shouldn't be very hard once i figure out how this fits together. (php, sql, etc).
Any help is appreciated, thanks in advance :)
You can run a php cli from the terminal only when you have php compiled with cli support. Additional you need to specify an interpreter and pass the path to the script as argument. But you can also use a shebang #!/path/to/php. A better practise would be to use the env variable and not hardcode the path to php: #!/usr/bin/env php. Read here about it: http://tech.vg.no/2012/02/21/dont-hardcode-php-executable-path-in-shebang/.
Basically you can write a simple CLI shell with a infinite loop plus a 'exec()' or 'shell_exec()' PHP functions. you should get the user commands and send it to shell_exec() function for execute in a system shell and return the output of that to the user.
i.e:
while(TRUE){
if($input != 'exit')
$output=shell_exec($input);
else
break;
echo $output;
}
you can add other options and customize this simple loop.
you can call external programs with 'exec()' function.
One of the nice features of languages like Python, Ruby or LISP is the availability of an interactive shell. This goes in a Read-Eval-Print Loop and allows to quickly experiment with the language without having to write and execute scripts.
Unfortunately PHP has nothing like that out of the box, but one can find some external tools online. I found three and I'm not sure which are the relative advantages?
Did anyone try one of those shells and can give some advice about which one to use?
Unfortunately PHP has nothing like that out of the box
Yes, it does. php -a or php --interactive are what you're looking for. They're useless before PHP 5.3 (segfaulty promptless <?php-prefix-requiring crap), but they fixed it up pretty well... just don't do anything that will trigger a fatal error.
Oh, and if you need to include a file that tries to do use getopt, you can make it work by opening the prompt thusly:
php -a -- --custom -s -t -u --ff="goes here" --the=first --double-dash --is="Magic!"
php -a
via command line invokes the interactive shell
I've found Facebook's (Python based!) PHP shell to work great, I've never gotten PHP built in interactive shell to work without custom compiling.
If I want to try out something new in Ruby or Javascript, I love getting immediate feedback from irb or the JS console in Firebug.
Is there anything like that for PHP?
Update
As #bernie pointed out, such tools are called REPLs - "Read-eval-print loop"
Run php -a in the command-line. But this does not work on Windows.
For Windows, you may want to try phpa-norl.
Yes, there is a way: php -a,
-a means interactive shell
php -a is the closest thing I know of but it's not quite like irb. irb will execute each line at a time. php -a will wait until you press ctrl-D (for end of line) and will then execute all lines.
How about phpsh? It's not fully compatible with PHP, but it represents a serious effort.
http://www.phpsh.org
Boris
I'm not working with PHP currently, but I just spotted this online and remembered I had once asked this question. Boris looks very nice.
https://github.com/d11wtq/boris
If you have a Mac, I find the PHP Code Tester program a much better way to accomplish the same thing: http://www.macupdate.com/app/mac/37328/php-code-tester
Specifically, I need to automate the encoding of audio files into mp3 with LAME. You don't need to know LAME to answer this, I could be talking about svn or some other program..
I know how to use LAME on the command line to do this, for one file at a time.
I would like to do this via a php script however, so I can convert a bunch at once (for instance, all the files in a directory)
So what I am confused about, is how I should invoke the program, LAME. I could definitely use
shell_exec()
http://php.net/manual/en/function.shell-exec.php
But is that a "screwy" way to do it, since I am going through the shell?
Should I be using lame_enc.dll somehow instead, instead of lame.exe?
It seems like I could somehow do it with exec() also http://php.net/manual/en/function.exec.php
But in that case, how would I supply the arguments?
Or is there a better way to do it, maybe a .bat file? I am running windows
Should I be using lame_enc.dll instead of lame.exe somehow?
You can use exec() and specify arguments just like you would on the command line. Other options are outlined on the Program Execution manual page for PHP.
It's possible to do it with PHP. Not a typical use case scenario but it can be done. Since you are on Windows, a bat file would be better suited since then you don't need the PHP parser to run the script.
Put the same commands you would run in the console to convert your audio files with LAME in a *.bat. Then run the bat as if it was a regular executable file.