Jump to content

[SOLVED] PHP console apps?


Michdd

Recommended Posts

When you say, console apps, I think you have a misunderstanding of how it works.

 

PHP is not a compile and distribute language like C.  PHP requires an interpreter before it can do anything.

 

To run a PHP script in the command line, you can start it via "php script.php".

 

When in the CMD line you loose most Superglobals and gain a few you dont have access to while not in the CMD line.  I know you gain "$argv" and "$argc".  You loose $_SERVER $_GET $_POST $_COOKIE $_FILES as those are set all by Apache or w/e server software is being used.

 

Now, there are some tools you can use that CAN compile PHP with the PHP interpreter.

The one i use is called Bambalam, a little old (PHP4) but it does the job.

http://www.bambalam.se/bamcompile/

 

 

As far as what you can do while in the CMD line, I have personally coded a VERY basic, but functioning HTTP server.  I have also coded an IRC bot in PHP.  There are some very fun projects you can do with sockets and PHP.

 

 

Hope this all helps a little.

Link to comment
Share on other sites

No, I don't misunderstand how PHP works :P I've been using PHP for a very long time, I just read that it's also possible to compile PHP into stand-alone applications. So I was talking about what you said later in your post. I actually found something called PriadoBlender myself which works well, and it does supporting compiling for PHP5 and PHP4.

Link to comment
Share on other sites

I'm actually quite amused by this post. In fact, I never really thought of PHP that way, although I did assume that it could have been possible. Is there any limit or differences in the functionality of having a C++ console app versus a PHP console app?

Link to comment
Share on other sites

I'm actually quite amused by this post. In fact, I never really thought of PHP that way, although I did assume that it could have been possible. Is there any limit or differences in the functionality of having a C++ console app versus a PHP console app?

 

You are limited to what extensions and / or libraries exist for the language.  But in principle they are much the same.  I write quite a few command line PHP programs for work.

Link to comment
Share on other sites

I write PHP CLI programs quite often.  In fact, I would say I write a couple a day.  I write big ones every now and then (back when I used to do some stuff that involved parsing some very interestingly laid out files I used to write fairly complex CLI applications in PHP).

 

 

But yeah, as roopurt said, scripting for PHP running through CLI and through a web interface is very, very similar.

 

 

As for a compiler (in the sense of compiling to machine code), I've never found one that wasn't missing some features, but the last time I looked was a few years ago.

Link to comment
Share on other sites

I'm actually quite amused by this post. In fact, I never really thought of PHP that way, although I did assume that it could have been possible. Is there any limit or differences in the functionality of having a C++ console app versus a PHP console app?

 

You are limited to what extensions and / or libraries exist for the language.  But in principle they are much the same.  I write quite a few command line PHP programs for work.

 

What about performance and speed differences?

Link to comment
Share on other sites

Performance and speed will be based on just how the PHP code is "compiled."  Compiled code always executes faster than interpreted code, so it depends on if the PHP "compiler" actually compiles the code or not.

 

That said PHP is extremely fast.  I've written a PHP program that loops over 9 million MS SQL records, performs a little arithmetic, and it finished in probably less than two minutes.

 

You can also have user input.  On Windows with COM you can also accept strings of text such as passwords in a manner that the user's input will not appear on the screen.

Link to comment
Share on other sites

In that case we are so fortunate that there is a search function on PHP.net:

http://php.net/results.php?q=stdin&p=manual

Okay, I've done some research.. I found something will work like this:

 

#!/usr/bin/php -q
<?php
/* Define STDIN in case if it is not already defined by PHP for some reason */
if(!defined("STDIN")) {
define("STDIN", fopen('php://stdin','r'));
}

echo "Hello! What is your name (enter below):\n";
$strName = fread(STDIN, 80); // Read up to 80 characters or a newline
echo 'Hello ' , $strName , "\n";
?>

 

However, this is PHP CLI, so it would be running it from the command line. Which isn't exactly what I'm doing. I'm using PriadoBlender to compile it is a .exe. Is it possible to allow user input from the compiled .exe?

 

Edit: Nevermind it was a stupid problem, I solved it. Thanks for all your help. I got one last question, how can I make the program 'pause' when it's done instead of closing right away? I know I can run the program through the command line to get this effect, but it's kinda inconvenient.

 

Edit Edit: Never mind, it's the same as in C++, system("PAUSE");

 

thanks for all your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.