Archive:PHP and MySQL Programming/Command Line Programming: Difference between revisions
wikademia>Darklama m recat sort |
wikademia>Wikademia m moved PHP and MySQL Programming/Command Line Programming to Archive:PHP and MySQL Programming/Command Line Programming over redirect |
||
| (6 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
PHP was traditionally used to help web administrators automate various mundane tasks and to add dynamism to the web pages. However, PHP (in its modern incarnation) is capable of much more. It is now being used in ways traditional programming languages are used. It is now possible to remove PHP applications from the constraints of the | PHP was traditionally used to help web administrators automate various mundane tasks and to add dynamism to the web pages. However, PHP (in its modern incarnation) is capable of much more. It is now being used in ways traditional programming languages are used. It is now possible to remove PHP applications from the constraints of the Internet server. Now, PHP is being used to write GUI applications as well as command line applications. We shall now look at writing PHP applications for the command line. | ||
==Getting the PHP-CLI== | ==Getting the PHP-CLI== | ||
| Line 19: | Line 19: | ||
Note: You do not include this line in Windows! | Note: You do not include this line in Windows! | ||
This should be the first line of the file, as it tells | This should be the first line of the file, as it tells Linux which interpreter to use for the file. | ||
The next thing you should do, is to make the file executable. This is done by the following Unix command : | The next thing you should do, is to make the file executable. This is done by the following Unix command : | ||
chmod +x filename.php | chmod +x filename.php | ||
==Getting Keyboard Input== | ==Getting Keyboard Input== | ||
| Line 38: | Line 37: | ||
We can now use $stdin just as if it were a normal file that we had opened for reading. (We obviously could not write to it, as it is Standard INPUT) | We can now use $stdin just as if it were a normal file that we had opened for reading. (We obviously could not write to it, as it is Standard INPUT) | ||
Here is a | Here is a useful function to get user input : | ||
<? | <? | ||
| Line 52: | Line 51: | ||
==Output Formatting== | ==Output Formatting== | ||
The main consideration to make when | The main consideration to make when formatting output in command line programming as opposed to generating HTML output is that a new line is not created by a <BR> tag, in fact, any HTML tags you put into the output, will simply be displayed as plain text. | ||
In order to create a new line in the command line, we need to use a control character, \n . Another | In order to create a new line in the command line, we need to use a control character, \n . Another useful function when doing command line programming is this : | ||
<? | <? | ||