Object Oriented PHP-GTK2 - PHP, GTK, The Web, Your Computer, and You Object Oriented PHP-GTK2 - PHP, GTK, The Web, Your Computer, and You
it's me, bob. lol. Object Oriented PHP-GTK2 OOPS

PHP, GTK, The Web, Your Computer, and You

An article written by Bob Majdak Jr about PHP, GTK, The Web, Your Computer, and You.

Topic: General PHP-GTK (View All Tutorials)
Keywords: php gtk apache web config help
Updated: A Long Time Ago, 2008/03/05 16:31

PHP-GTK. Anyone who uses Linux often enough probably recognizes the two acronyms there. It sounds like a gift from the deity of choice to anyone who uses the PHP language frequently. PHP (Hypertext Preprocessor) as it stands to most people is one of the most common programming languages used today... on the web. Most are familiar with the need to have a web server with PHP so applications like forums and blogs can be run. The funny thing is that 99% of the people using PHP have no idea it really has nothing to do with Apache (the most common web server application), or that PHP does not even need a web server to run.

That number might be high, I just made it up, it could be closer to 98.9%. In any case this is the most frequently encountered problem we run into when trying to help people get started with PHP-GTK.

Let me get it out right now, PHP does not need a web server to be used. PHP does not need Apache. PHP does not need anything but itself. Thanks to the thing called the Command Line Interface (CLI, or PHP-CLI) you can write a PHP script to do things on your own computer just like a Bash script or Windows Batch file. Check out this from my Linux terminal window:

//. bob@tuvok [temp]$ ./test.php
wee look at me.

Now let's take a look at that PHP file.

#!/usr/bin/php
<?php

echo("wee look at me.\n");

?>

You see the very first line, the one that starts as #!/usr/bin/php. This is called the hashbang (or shebang) line, and it tells the shell, in this case Bash, what program to use to run this script. You might be used to seeing something like #!/bin/sh there instead. I have the PHP-CLI installed into /usr/bin and so that is the program used to run this script.

This allows us to use all of the features of PHP on your computer (also called the localhost) to perform tasks that would normally be a pain to write out in something like Bash script. With the GTK extension to PHP, we can create windows, buttons, drop down lists, all known as part of the Graphical User Interface (GUI). Most people are familiar with GTK as being something they had to install to run The GIMP, GNOME, Pidgin, or X-Chat. Theoretically we could rewrite all these programs in PHP and make them look just as they do now with PHP-GTK.

Not only that but because PHP is interpreted and not compiled, the same PHP script will run and do the exact same thing on any operating system it is run on. Here is the above script on the Windows command line.

C:\temp> php test.php
wee look at me.

Now that the eyes are wide open, we are eager to get typing up our HTML to produce awesome GUI applications I bet.

#!/usr/bin/php
<?php

echo(
  
"<p>Hello World!</p>".
  
"<p>This is my first localhost PHP program!</p>"
);

?>

And the output:

//. bob@tuvok [temp]$ ./test.php
<p>Hello World!</p><p>This is my first localhost PHP program!</p>

What happened here? Why is it showing the HTML instead of making real paragraphs? Simple. Nobody cares about HTML.

  • Your computer does not care about HTML.
  • Your shell does not care about HTML.
  • PHP does not care about HTML.
  • Because of all of this, you should not care about HTML.

All PHP does is output the text it is told to output. The only thing that does care about HTML is your web browser. Your web browser was built to turn the HTML code into paragraphs. You are not building web pages now, you are building real programs on your computer. What are you using to read this document now? Firefox, OpenOffice, Internet Explorer? All of which are programs running on your computer, not from a web server.

On the flip side of that GTK cannot build web pages. You cannot install it into a web server, upload a PHP-GTK program, and get a web page made of a GUI using GTK. You cannot upload a PHP-GTK program and launch it from the web magically creating a Java WebStart type application. Let's recap.

  • PHP and GTK does not use HTML.
  • PHP and GTK does not build web pages.
  • PHP and GTK does not build web applications.
  • PHP and GTK builds computer programs.

Because GTK is for building localhost programs and not web pages, if the PHP.INI Apache is using loads PHP-GTK, Apache will cry saying "I am sorry you are trying to build programs to show on a computer monitor and I want to serve web pages over the internet," and then Apache will die. The solution to this is to use a different config file. For example, my apache uses the file /etc/apache2/php.ini to configure PHP for the web. On KateOS, we use /etc/gtk/php.ini for all programs that are to use PHP-GTK. This way Apache gets what we want, and so do we. On Windows this is not even a problem, because PHP-GTK comes with everything it needs to run packaged together, you just unzip it and go, it does not fuss around with the system at all. See the links section at the end for more information about the actual configuration of PHP and GTK.

So how are PHP-GTK programs run?

They can be called from the command line like in the examples above, or you can configure your system (both Windows or Linux) to do the work for you. We do this by giving PHP files that use GTK the file extension .phpg, and then tell the system that when we double-click .phpg files that it should execute it using PHP. Then you are pretty much launching the program the same way you would double-click any .exe file.

Can I compile my PHP-GTK program so that I have a .exe file?

Simple answer: no. It is beyond the scope of this document to even care about going into everything you have to do to even attempt it.

Links to more detailed help.

Was this document helpful? I appreciate your feedback.
What are the rules about reusing this code?


i can has web two point ooh // copyright © 2007-2008 bob majdak jr
[ xhtml css | firefox ie7 opera ]