By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

Interactive PHP shell - can it be done?

P: n/a
I'm interested in implementing a command-line PHP app that takes input
line-by-line (for example, using Readline) and executes commands as
they are entered. It would need to know the difference between this:

echo "hello world";

which should be eval()ed after the first line, and this:

for ($i=0; $i < 10; $i==) {
echo $i;
}

which can't be executed until the block is closed.

I suppose I could try calling eval() and catching errors until it
works, but then how to distinguish between a data-entry error (e.g.,
"ecoh $i") and an error indicating that you're in the middle of a
possibly valid block?

Thanks for your help,
G
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 29 Nov 2004 18:54:02 -0800, gn******@gmail.com (George Nachman)
wrote:
I'm interested in implementing a command-line PHP app that takes input
line-by-line (for example, using Readline) and executes commands as
they are entered. It would need to know the difference between this:

echo "hello world";

which should be eval()ed after the first line, and this:

for ($i=0; $i < 10; $i==) {
echo $i;
}

which can't be executed until the block is closed.

I suppose I could try calling eval() and catching errors until it
works, but then how to distinguish between a data-entry error (e.g.,
"ecoh $i") and an error indicating that you're in the middle of a
possibly valid block?


why not just typ php from the command line? You get a prompt. enter
the stuff. It works.
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
DataBasix | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ۳
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Jul 17 '05 #2

P: n/a
"George Nachman" <gn******@gmail.com> wrote in message
news:e5*************************@posting.google.co m...
I'm interested in implementing a command-line PHP app that takes input
line-by-line (for example, using Readline) and executes commands as
they are entered. It would need to know the difference between this:

echo "hello world";

which should be eval()ed after the first line, and this:

for ($i=0; $i < 10; $i==) {
echo $i;
}

which can't be executed until the block is closed.

I suppose I could try calling eval() and catching errors until it
works, but then how to distinguish between a data-entry error (e.g.,
"ecoh $i") and an error indicating that you're in the middle of a
possibly valid block?

Thanks for your help,
G


Prepend the PHP code block with a simple assignment. If there're no syntax
error, then the assignment would be made. Otherwise the the assignment is
not made. Use the @ operator to suppress the parse error message.

$statement = '$apple = $orange;'; // whatever

$parse_error = true;
@eval("\$parse_error = false; $statement");
if($parse_error) {
....
}
else {
....
}

Set a custom error handler with set_error_handler() to handle non-syntax
errors in the code.
Jul 17 '05 #3

P: n/a
Chung,

I'm not sure I understand how this could distinguish between a syntax
error and a well-intentioned but incomplete script?

Also, thanks for your incisive comments there Gary. Real helpful.
Prepend the PHP code block with a simple assignment. If there're no syntax
error, then the assignment would be made. Otherwise the the assignment is
not made. Use the @ operator to suppress the parse error message.

$statement = '$apple = $orange;'; // whatever

$parse_error = true;
@eval("\$parse_error = false; $statement");
if($parse_error) {
...
}
else {
...
}

Set a custom error handler with set_error_handler() to handle non-syntax
errors in the code.

Jul 17 '05 #4

P: n/a
"George Nachman" <gn******@gmail.com> wrote in message
news:e5**************************@posting.google.c om...
Chung,

I'm not sure I understand how this could distinguish between a syntax
error and a well-intentioned but incomplete script?


Well, you're trying to do the impossible then. PHP can't read mind and can't
see into the future. How can you tell whether, say, the absence of a curly
bracket is imcompleteness or a mistake? What I have give you is a mean to
detect syntax error in the code.
Jul 17 '05 #5

P: n/a
You're describing an interprative PHP environment. Essentiall you would
run the script upon completion. Which is basically what you can do now,
an integrated PHP environment with an external debugger would give you
most of what you want and it's all readily available.
D

Jul 17 '05 #6

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<QJ********************@comcast.com>...
"George Nachman" <gn******@gmail.com> wrote in message
news:e5**************************@posting.google.c om...
Chung,

I'm not sure I understand how this could distinguish between a syntax
error and a well-intentioned but incomplete script?


Well, you're trying to do the impossible then. PHP can't read mind and can't
see into the future. How can you tell whether, say, the absence of a curly
bracket is imcompleteness or a mistake? What I have give you is a mean to
detect syntax error in the code.


I should have been more specific. What I need to know, specifically,
is if the braces, quotation marks, etc. are balanced. The Tcl language
provides a C API to do this--Tcl_CommandComplete
(http://www.tcl.tk/man/tcl8.2.3/TclLi...mplt.htm)--and I'm looking
for an analogue in PHP. Hopefully no mind-reading will be needed :)
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.