Interactive php - php

I looking for interactive PHP application like LINQPad for C# where I can test code snippets.

Assuming you have PHP's CLI installed, just do php -a at the command line (preferably on an OS with a decent shell). This launches an interactive mode. Start with <?php and you're good to go.

Related

how to debug c++ code called from php using exec()

I'm calling some c++ code in my php app using "exec" as explained in this tutorial. I'm trying to figure out how can I debug the c++ code once it's executed.
the c++ app starts and ends way faster than me using for example eclipse attach to process.
any ideas how to debug the c++ code once it's called with exec() from the php app?
I'm on linux using eclipse and GDB.
the php app workflow is the following:
get values from html form, pass these values to c++ code called with exec(), get the output from c++, then display it using php onto html. think of it as php is the controller and c++ is the model.
Really depends on how you are using the php code in the first place. If you are using PHP to generate a web-page, where you do some C++ code in the middle of the generation of the HTML (or whatever), then it's a bit tricky. If you are using command-line to run the PHP, you could just change $command from "myprog arg1 arg2 into "gdb --args myprog arg1 arg2" - which will start the debugger gdb instead of just running "myprog".
If you can't debug the actual application (because the output of "myprog" is part of your web-page, for example), then I would simply run $ gdb --args myprog arg1 arg2 on a command-line on the machine, and debug the code standalone.
Or, if you HAVE to debug it as part of the web-app, you could perhaps add sleep(10); to the beginning of the "myprog", and use gdb attach X where X is the process ID (from top or ps aux | grep myprog, for example) - set a breakpoint just after the sleep(10); line, and do the gdb command continue.

PHP Interpreter for Windows

Trying to mess around with PHP, but I don't want to install IIS or Apache and was hoping for a small interpreter that I can pass the scripts to and have them run in like a console or something. Much like Lua does. Does this exist? When I go to download PHP it seems to only talk about running it on IIS or Apache.
PHP can be used on the command line. Just download and extract the executable.
Running can be done 3 ways: a file, supplied code or in an interactive shell
php file.php
php -r "echo 'hello';"
php -a
You can also install a pre-packaged server (e.g. XAMPP) or run your code online on various places (e.g. phpfiddle.org)
With PHP 5.1+ you have an interactive shell too:
launch php with -a parameter
php.exe -a
http://php.net/manual/en/features.commandline.interactive.php
I believe that PHP v5.4 comes with its own webserver built-in. Install that version and you should be fine. Though I really would like to know why you have problem with installing Apache (IIS sucks though).
EDIT:
As a few others have said, you can run PHP from the command line.
check out easyphp, a server for php development
http://php.net/cli
Alternatively, you can write your PHP scripts as you would normally (with some limitations),
And use the following command line:
C:\php.exe -f "D:\phpFile.php"
dtech's answer seems most fitting for the simple script-running things you're looking for. If you decide you want an actual web-server, with as little setup as possible, look at "XAMPP".

php webserver equivalent of mongrel?

Is there a php equivalent of mongrel? I don't want to install Apache, just to preview a simple template.
mongrel2 looks promising!
This is possible starting with php 5.4:
http://php.net/manual/en/features.commandline.webserver.php
As of PHP 5.4.0, the CLI SAPI provides a built-in web server.
This web server is designed for developmental purposes only, and
should not be used in production.
To run it:
$ cd ~/public_html
$ php -S localhost:8000
I know it's not the ideal solution, but if it's just one page, you can render it straight out to HTML.
php /path/to/file.php > /path/to/output.html
As I posted in the other thread - have a look at Photon (http://www.photon-project.com), it might serve your needs quite well already.

How do I execute a PHP shell script as an Automator action on Mac OS X

I'm tempted by Automator.app's ability to create contextual services in Mac OS X Snow Leopard. I would like to create some keyboard accessible shortcuts to manipulate text snippets by calling a shell script. However, Automator only suggests bash, Perl, Python and Ruby (among others) to allow this. But, since PHP also ships with Mac OS (and, to be honest, it's the only scripting language I fully master), I wonder why I can't run a PHP shell script.
This is just a hack, but how about creating a python, or ruby or perl or bash script that calls php command-line interpreter with the php script you want to execute?
For instance in bash would be as simple as this:
#!/bin/bash
php myscript.php
Now save it, give it permission for execution:
chmod +x my_bash_script.sh
And voilá. Automator can now run the bash script, which calls the php script.
dbr's solution above is excellent, but I found that it didn't work for me, perhaps due to a later change in the shell, php, or OS version (I'm using OS X 10.8). After a lot of head-scratching I found that the answer is to quote the heredoc marker.
A useful addition is to use the appropriate syntax to pass the arguments through to the php script. Putting all that together, I'm using:
php -- "$#" <<'EOF'
<?php
print_r( $argv );
?>
EOF
You can use the "Run Shell Script" action to run a PHP script. One self-contained way is to use <<EOF ... EOF to pass the script to the php command:
(I would strongly recommend learning Python, Ruby or Perl.. PHP does has some advantages for web-programming, but is just not intended for command-line scripting.. It's certainly doable, it'll just not be nearly as.. pleasant)
The list of shells that Automator supports is in
/System/Library/Automator/Run\ Shell\ Script.action/Contents/Resources/Shells.plist
You can follow this guy's instructions to add an entry for /usr/bin/php. You may want to copy the .action file to ~/Library/Automator first, and work on the copy instead.
Of course, it's probably easier to just wrap it in a bash script, as others have suggested.
Automator has a "Run Shell Script" Action just add it to your workflow and follow the instructions on the bottom left.

interactive console for Ruby, PHP

How do you start the Interactive Console for ruby and php
For ruby, you want the 'irb' command.
For python, you can enter interactive mode with the 'python' command.
For php, you enter a basic interactive mode with 'php -a'. Because of its limitations, other interactive shells have sprung up. PHP-Shell is one of them.
Also, if you are working with a Ruby On Rails project you could use
ruby script/console
from your Rails application root. This is a good approach since you get an interactive ruby shell and the advantage is that loads the entire application stack for you. Pretty handy if you are testing your new code or trying to debug something.
php doesn't lend itself very well to an interactive shell, because you can't redefine functions and classes. So while there is an interactive shell available, it's not very useful.
Make sure you have php5-cli installed and type 'php -a' at the command line.
you are looking for phpsh same as irb for ruby. php-cli allow you to run scripts for the command line like
myserver> php -r "echo 'hola mundo';"
so what you are asking and all replies about php-cli are different. also php-cli has it own php.ini that allow you to have differents configurations depends of run php over httpd (like apache) or in bash
saludos
There is an excellent script for PHP that allows you to run a php console on a local website.
http://seld.be/notes/php-console-in-your-browser
There's obvious security implications here, but if it's only accessible locally, then there's no need to worry.
Really saves time if you want to double check the function behaviour, or to isolate a piece of code and diagnose what happens.
For a console with a rails 3.x+ project loaded
rails c
this is shorthand for ruby scripts/rails console
See more abbreviations here: http://xyzpub.com/en/ruby-on-rails/3.2/abkuerzungen.html
In php its php or php-cli - different name in different installations

Categories