Click to See Complete Forum and Search --> : How dose PHP Work?
ComputerNerd888
01-17-2004, 08:46 PM
Hello,
I am trying to figure out every aspect of how PHP works.
What I mean by this is the application running on the server...
What does it do?
From every thing on if it listens to a port how it knows when a PHP application is called what it dose with that and how it sends back the HTML to be displayed...
I looked in the docs and every where I could find and I found nothing...
Thanks,
Andrew
weekender
01-17-2004, 09:51 PM
a web server at it's most basic works like this
[list=1]
server opens a port and listens for requests on that port
when a request is received, a child process is initiated (the program is forked)
the child process interprets the request (ie what file is being requested)
if the requested file is found, it is sent to the socket (ie to the client). If not found, a 404 error is returned (possibly with a 404 page)
once the file has been sent, the child process is terminated
the server loops back to 1 (ie keeps listening)
[/list=1]
php comes in at stage 3 in this list - if the file requested is a .php file (or another type associated with the php engine) then the php parser will parse the file, and return the result, which will then be passed back to the client in stage 4
adam
ComputerNerd888
01-17-2004, 09:56 PM
Apache calls up PHP paser engine use the MIME types right?
Thanks.
Andrew
weekender
01-17-2004, 10:01 PM
no - mime types refer to the format of the file. A php file is text, just like a .txt or a .htm file - the file extension is used to determine whether to pass the file through the php parser. Extensions that are parsed can be changed in your httpd.conf file
ComputerNerd888
01-17-2004, 10:31 PM
Ok this makes sense now...
Thanks,
Andrew
Moonglobe
01-17-2004, 11:59 PM
actually weekender, you're wrong if you're using the apache module. you have to add a mimetype of application/x-httpd-php to .php files to get the parser to run them. for the CGI you just have to set an action for .php files.
LordShryku
01-18-2004, 12:10 AM
Originally posted by ComputerNerd888
From every thing on if it listens to a port how it knows when a PHP application is called what it dose with that and how it sends back the HTML to be displayed...
Who said PHP has anything to do with HTML?
ComputerNerd888
01-18-2004, 12:23 AM
PHP its self...
After the PHP is parsed it return back an HTML file....
Run a PHP file from a server than go to View/Source and you will see that what PHP returned it ALWAYS HTML.
Andrew
LordShryku
01-18-2004, 12:32 AM
PHP doesn't have to return html. In fact, it won't unless you tell it to explicitely. A lot of people keep their HTML and PHP seperate, having it return nothing but variables
Moonglobe
01-18-2004, 12:59 AM
aren't you being very picky with your use of "return" Lord? :p
ComputerNerd888
01-18-2004, 01:02 AM
Picky picky picky...
Come on you know what I mean...
PHP %99.9 is "told" to return HTML...
....
Andrew
Moonglobe
01-18-2004, 01:09 AM
not really though. lord's right. doing other things is becoming more and more common. dynamic images are always cool, and outputting plain text is nice if you're at a CLI :rolleyes:
drawmack
01-18-2004, 01:13 AM
Lord is making a very valid point though. You can use PHP for lots of things besides scripting html pages. You can use it as a passthrough to download files, show images etc. In those cases php is not returning html. You can use php for shell scripting in those cases you're not returning html. You can use it from cron jobs, in those cases you're not returning anything (if you're smart anyway). With php-gtk you can even write windowed applications in php that have nothing to do with the web or html.
Moonglobe
01-18-2004, 06:39 PM
while we're on the subject, anyone know anywhere i can go read on who the engine actually works? im not interested in the server architecture but more the parser itself... i've been wondering a lot about it lately, and i barely know any C(++)?.
ComputerNerd888
01-18-2004, 07:18 PM
I'm not sure about PHP (thats why i asked) but more than likely they use Flex and Bison...
Parsers can be VERY difficult to make so Flex and Bision made it easy for us...
You fill out a file (it has its own simple syntax's and than it make a a Scanner and a Parser for us...
Than from what Flex and Bision returns PHP engine executes the right functions (i.e. if the PHP you made calls to connect to a database than the PHP engine connects to the database)...
Then returns Apache the infomation... (i..e. All the HTML not inside the <? ?> and anything inside the <? ?> that is the print or echo...
To be more "Correct on What It Returns"...
Thats a crash lesson... Need more info? just ask i can go in it depper...
Andrew...
P.S. I am NOT SURE if PHP USES FLEX and BISION they have MADE there OWN but it is used in THIS example...
Moonglobe
01-18-2004, 07:22 PM
i understood and kind knew almost all of what you said, so thanks, but i was more interested in how "flex" or "bison" (or their equivalent in the PHP equation) actually parse.... i guess i'll have to go a'googlin, but this would seem to be a hard thing to search for... wish me luck...
ComputerNerd888
01-18-2004, 08:29 PM
O but wait I have a answer!
See when I run windows I use Dev-C++ as my IDE for C++...
A while back I was looking into making a compiler and IDE that runs on both Windows And Linux so no matter what OS I am running (and I run Linux more) I can have the SAME IDE and COMPILER that I made ;)....
So that was one of my questions... I really did not want to use Flex and Bision because I wanted the experience in making a Scanner and Praser...
So I went to Dev-C++ source code to check out how I could do this (it turns out it is written in Delphi (which I dotn know) and uses Mingw port of GCC... Which most do...
But I found that the Dec-C++ site has tutorials on them... Well I found one on making your own Parser and Scanner (no using Flex or Bision or anything but C/C++)...
Well its 320 pages so I never got "around" to reading it but it is a free book on the internet to download... Now you don't have to read the hole thing but you should be able to get what you want to know with a little bit of scanning!
Heres the book: http://www.cs.vu.nl/~dick/PTAPG.html
And heres Dev-C++ list of stuff: http://www.bloodshed.net/compilers/index.html
I hope I was of help,
Andrew
Moonglobe
01-18-2004, 09:02 PM
Thanks alot! that'll be an interesting read! :)
drawmack
01-18-2004, 10:23 PM
Cool I grabbed them as well, thanks for the link.
Weedpacket
01-19-2004, 06:23 AM
If we're posting links to books on language design I have to throw in this one: http://mitpress.mit.edu/sicp/full-text/book/book.html
Not parsing per se, but the broader task of implementing programming languages - including issues like memory management, evaluation, recursion, and whatnot.
And (not an online book, but a textbook on my shelves that I'm always referring back to) is Floyd and Beigel (http://www.cis.temple.edu/~beigel/long.html#lom)'s The Language of Machines, which covers (in passing) the fundamental theory of parsing (it says "We will not define LR(1) grammars, but they are equivalent to DCFLs. Furthermore, the syntax of almost every progarmming language is essentially an LR(1) grammar. For more information, see a textbook on compiler design.").
Shrike
01-20-2004, 09:38 AM
Is it not the Zend engine which handles all that kinda stuff?
Weedpacket
01-20-2004, 10:26 AM
Originally posted by Shrike
Is it not the Zend engine which handles all that kinda stuff? Yah; but the Zend engine itself is (allegedly - I haven't seen it used anywhere else yet) language-independent. PHP itself is defined in pairs of flex/bison files (one for the language itself, one for the .ini parser, one for the strtotime parser and one to bring them all and in the darkness bind them).
ComputerNerd888
01-20-2004, 07:21 PM
So PHP dose use flex and Bision?
They do not use there own created Parser?
Andrew
Weedpacket
01-20-2004, 08:58 PM
Originally posted by ComputerNerd888
They do not use there own created Parser? Flex/Bison did the job of turning the text file into a parse tree, then that is passed to the engine.
I sometimes suspect that it could have been done better; there are other parsers out there - but everyone has yacc or bison already...
http://lxr.php.net/source/Zend/zend_ini_scanner.l
http://lxr.php.net/source/Zend/zend_ini_parser.y
http://lxr.php.net/source/Zend/zend_language_scanner.l
http://lxr.php.net/source/Zend/zend_language_parser.y
And while we're at it: http://lxr.php.net/source/php-src/ext/standard/parsedate.y
dalecosp
01-20-2004, 11:39 PM
Bison, at least on FreeBSD:
$make search key=php4
Port: php4-4.3.4_4
Path: /usr/ports/lang/php4
Info: PHP Scripting Language (Apache Module and CLI)
Maint: admin@deleted.com
Index: lang devel www
B-deps: apache-1.3.29_1 bison-1.75_1 expat-1.95.6_1 gettext-0.12.1 libiconv-1.9.
1_3 m4-1.4_1 mysql-client-3.23.58 perl-5.6.1_15
R-deps: apache-1.3.29_1 expat-1.95.6_1 libiconv-1.9.1_3 mysql-client-3.23.58 per
l-5.6.1_15
ComputerNerd888
01-20-2004, 11:52 PM
so PHP uses Flex/Bision (They work together)...
Thats not too surpizing...
Most things that use a parser will use Flex/Bision...
GNU made it and it make parser design extremely simple...
I am using it in my SourceForge project InterC++ (Project is new)
Andrew
dalecosp
01-21-2004, 12:13 AM
Well, maybe; don't overgeneralize. Bison is a "build" dependency, but as someone stated, it doesn't need it to "run"....
PHP Builder
Copyright Internet.com Inc. All Rights Reserved.