Although most people use PHP primarily as a Web development scripting system, it possesses all the characteristics of a proper general-purpose language that can be useful in a variety of other environments. In this article, Marco Tabini illustrates how it’s possible to use the command-line version of PHP to perform complex shell operations, such as manipulating data files, reading and parsing remote XML documents and scheduling important tasks through cron.
The language itself is fine.
It feels mostly like C/C++ without type system and only simple OOP support.
Arrays are dominant, and very powerful/good supported. Reminds me a bit of the dominance of list data types in the functional languages. And PHP has a couple of array functions that support that kind of programming style.
I wish the startup time of command-line PHP would be little faster, to speed up some of my scripts.
But it is possible that I don’t know of some trick to improve that.
Regards,
Marc
Does anyone know about some resources that would help me with embedding PHP as a scripting language in my program? I’ve been considering other languages such as Lua or Python but PHP would be ideal since I’m more familiar with it.
Basically I need to strip the extensions, plug in my own and put the whole thing in a library, right?
Well, don’t Apache and IIS basicly do this? You might want to take a look at the modules for those webservers located in the PHP source code.
well, to be a real multi purpose langaguage, php should have a gtk2 or better, wxWidget binding. I know that a php-gtk1 exist, but it’s not really usable for a real application.
you don’t alway have the nedd for a Gui, but a multi-purpose language must be able to create one
php5 gtk2 is rolling quite along.. and some is command line support
If you’re impressed by PHP’s similarity to functional languages, you might want to check out Ruby. I’ve yet to see another language that, while not being designed as a functional language, allows one to use it as much like one as Ruby.
Im doing most of my tasks with PHP. Thanks to PHP-Cli and *NIX env. I can manage everything, from cronjob to my Desktop with advance of having everything in SQL database. Also I can sync my work, to-do list and everything. This language is just amazing. And if you’re advanced developer, you can make it to do any stuff. It’s like geek heaven. :}
Emil, do you think that there is something stopping you from syncing your work, todo list, cronjob, sql stuff in *everything else* ?
being mainly a php developer myself (force to do it ๐ i always wonder why php is so dominant compared to python.
It has extensive library support (compareable to php) and you get a slight performance boost. furthermore it’s incredibly easy to learn, maintain and extend (c++).
[flame mode] ๐
cli jobs must be done with the right tool
python bash perl.
php is childish…
python is ok, but somehow I get more work done in PHP.
Ruby feels pretty much like smalltalk, it is very object oriented.
What I like to use in my free time is a totally different animal (Erlang).
Regards,
Marc
PHP isn’t too bad compared to say…VBScript. But why use php when there are better, simpler, more powerful, and more expressive languages available?
The PHP crowd seems to be full of people that simply aren’t interested in learning a new language.
Nothing, except no script language has syntax so similar to C. So, I could do it in C/C++ (and start wastin’ time with gdb;-) or write a nice tool in PHP. Yeah, I use Bash/Perl when it comes to do some stuff, but when I have to parse XML, grab data from SQL or do something different — there’s only one sane chooice for me :->
,,The PHP crowd seems to be full of people that simply aren’t interested in learning a new language.”
For me it’s quite oposite. I enjoy PHP so mutch because I didn’t had to learn it. I’ve choosen PHP because it didn’t take a 5h to get it work and do stuff, because it’s so C alike. Somethimes I even add some // to my source with hints how it could be redone in C. That’s another way of sharing work in workplace. You develop something in PHP, yet, this function is almost usable, if you change few parts and add types on the top. I would love to grab Ruby code and give it to C++ developer from another company and order him: rewrite it, add GUI and send me back. But he’ll probably have no idea about what Ruby is.
OTOH, Im reading Ruby docs at home, and it’s very powerful langauge. Outstanding OOP features. But until I’ll master it, it’s just a hobby. And we can not afford for hobby-solutions at work.
Bah! ;->
um.. in python a feed parser can be done sweety with just
little lines of code
import feedparser
f = feedparser.parse(“http://www.feeders.com/rss“)
d.feed.title
Looks nice, mastering Python is my next task, after ruby. And then, Rebol. ๐
a real multi-purpose langaguage
In that case what, pray tell, is PHP’s singular purpose? I would think it self evident that the fact it is being used on the command-line, i.e. not as a PHP Hypertext Preprocessor or whatever the acronym is now, is fairly good evidence of it being multi-purpose.
I haven’t looked at PHP in along while (approximately two years) but when I did, it didn’t strike me as a particuarly nice language. I’m sure that a lot of people are very happy with it and features like the Zend Optimizer cache thingy are pretty cool, it just doesn’t (or rather, didn’t) blow my horn.
They’re documentation is VERY cool though. The function reference on php.net is amongst, if not the best, documentation for an open source project anywhere.
PHP aims to be a single purpose language, however nothing is stopping you from using it for other purposes. But if you look at the language features you’ll notice that it’s aimed towards web development. Few multi-purpose languages would include those features since it adds bloat for a lot of people.
But if you don’t have the motivation to learn another language or if you’re just comfortable with PHP it can be used for other tasks, and it does a fairly good job at it.
I used to hate PHP about 4-5 years ago, because it was pretty buggy and had nothing that Perl didn’t have. Also the C-style syntax didn’t appeal to me. But I picked it up again about half a year ago and I like it a lot more now. It’s a very comfortable language for web development. I’ve also used it to make a few small cli scripts. But I would never use it to make a desktop application, for that task I prefer Python, C++ and Rebol.
so is in ruby, IIRC:
require ‘rss/2.0’ #or 1.0 or 0.9 or dublincore or whatever
rss = RSS::Parser.parse(rss_source)
puts rss.items[1].title
And RSS::Parser is part of the standard library in ruby 1.8.2.
But making this a language debate is way OT I was just saying that the tasks described were doable in any language.
I have spent the past year using PHP as a non-web language for a commercial software project, and it has gone quite smoothly.
In my case, I needed to get something out quickly that would normally have been done with C/C++. I decided to try PHP with Mozilla/XUL as the front end. Note that I am not using any webserver, but connecting directly to the Mozilla framework with sockets. It turned out quite nicely, giving me an app that can be run client-side, but can also allow the front end to be distributed as a cross-platform thin client.
In case anyone wants an idea how this was accomplished, feel free to pick up the July 2004 issue of PHP|Architect, where I wrote an article called “Socket Interprocess Communication”:
http://phparch.com/issue.php?mid=34
(Disclaimer: in case anyone doesn’t know, Marco Tabini is also the publisher of PHP|Architect)
I think Marc hits the nail on the head in the first post. PHP’s strengths are its simplicity and similarity to C/C++, and the ease with which it handles arrays. Basically, in PHP an array can contain an anything of anythings ;-). It is very useful for doing such things as function overloading, returning multiple values from functions, etc… Also, the fact that it has array_walk, array_map, along with create_function gives it some cool functional/declarative capability. I, for one, wish they would take another look at functional support, instead of just OO support, in PHP 5.x. Closures and list comprehensions would be nice.
It’s funny, though that PHP is going through such growing pains right now with PHP5. PHP’s main strength up until now has been its simplicity, even with all the warts of inconsistent function naming, inconsistent parameters, etc… The real reason for this is, as everyone notes, the documentation. The function inconsistencies are no problem because every PHP developer works with a browser open to http://www.php.net/%5Bfunction_name%5D. Basically, you can just start coding, without preamble. The limitation that everything is in one big namespace was actually also a strenth, because you didn’t have to worry about “import [module]”. Of course, this is all stuff aimed at making a new programmer productive in as few hours as possible, but it has caused difficulties for more advanced developers. Now, with PHP5, we are gaining many of the things the advanced developers need (with the notable exception of namespaces, unfortunately).
One commenter here claims that people cling to PHP because they don’t want to learn other languages. Maybe there is a certain amount of truth to that, but also it is because PHP let’s you get work done quickly, and is especially suited for fast development of projects with one or few developers. Simple economics come into play here: most freelance web developers have to either learn PHP or ASP, maybe Coldfusion. The ones who learn PHP of course tend to go the way of open source Linux/BSD servers, where PHP is the most common web platform. Also, most of these developers are so pressed for time generating quick web apps for small-to-medium sized companies that they simply don’t have time to mess with Perl or Python.
I have to admit that I have used and loved both Perl and Python, and each has advantages over PHP. But, if you want to be able to write web apps quickly and deploy just about anywhere, the only other real choice you have is Java, or as I like to call it: “Programming by Beaurocracy”, which is just not suited to single-developer, quick web apps.
Thus, when those developers move on to non-web scripting, PHP naturally is a nice tool. It has all the database interaction you would ever need, easy file interaction, very easy string manipulation for shell scripting, and it deals quite nicely with Unix concepts such as streams and sockets. Of course not the best tool for every job, but what language is?
rycamor, beautiful post. I wish I could have put my thoughts together is such an eloquent way.
PHP is a fine toy language for deploying web content.
PHP is totally lame compared to Perl and Python for general scripting.
Right tool for the job folks.
Waa waa, another idiot who cannot tell his opinion from fact, or his asshole from a hole in the ground. Big surprise that you work for Yahoo.
Install a monster interepreter and as a giant improvement over using the libxml.so installed on practically every linux box ever built I’ll never have to endure the agony and simplicity of C as in:
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
doc = xmlParseFile(filename);
cur = xmlDocGetRootElement(doc);
cur = cur->children;
while (cur != NULL){
if ( ! xmlStrcmp(cur->name, (const xmlChar *)”duh”)
dostuff();
cur = cur -> next;
…
wow.
Oh, now I’m convinced.
Now I see. I was so wrong… sorry everyone. I guess the fact that my scripted PHP applications work quite well and robustly is just a fluke. Forgive me for my presumption, O guru.
Other languages could do well to implement a similar style of documentation to PHP, ala syntax reference with embedded comments (yes, some do, but most provide only language reference without comments). That’s where PHP’s strength lies.
I’m curious to know if other folks have pointers to similarly documented languages.
PHP is not even a decent language for web apps. Look at how it encourages (or used to encourage) bad security: register_globals, fopen(URL), no taint checking, etc.
Easy, yes. But decent? No.
Sadly, it’s a crazy world we live in. PHP will rule the world, while Ruby and Python will be obscure but to a blessed few.
I’m curious to know if other folks have pointers to similarly documented languages.
Hmm… I haven’t seen any other languages documented that way, but I think it is worth noting that both PostgreSQL and MySQL attempt similar things with their documentation, although not as well.
In fact, PHP’s documentation shows that the PHP guys understand what it’s all about at the core: databases. Not only do they maintain user comments, but excellent cross-referencing and examples. I’m fairly sure all this is maintained in a central SQL database, rather than some “document hierarchy” as with most languages. This makes it far easier to update documentation, as well as maintain associative lists, versioning, etc…
I’m learning it now for a project, and was just thinking how nice it would be to use it for something non web-related.
The nice thing about it’s easy and straightforward to write in.
Perl is just too convoluted. The “there’s more than one way to do things” philosophy means it’s just too dang hard to do anything. It’s become the C++ of scripting languages: big, overly complicated, and obscure. Plus, it’s tiring having to drag the perl book out every time I have to do something even marginally exciting.
shell is nice, but string manipulation is a PITA in it.
If it works, and is maintainable, then heck, why not?
Do anyone know where the resource to build the configuration language or configuration file by lex/yacc?
PHP is a boring language. It does alright at web development..if the project is small enough. Eventually though, it leaves you yearning for a more expressive language.
It’s pretty obvious that a good deal of those advocating the “use php for everything!” crap haven’t really touched much besides PHP. Dynamic arrays, hash tables and array mapping are not news. Every decent scripting language has these.
What has yet to happen is for someone to mention something that PHP *as a language* offers over ruby.
It’s pretty obvious that a good deal of those advocating the “use php for everything!” crap haven’t really touched much besides PHP. Dynamic arrays, hash tables and array mapping are not news. Every decent scripting language has these.
What has yet to happen is for someone to mention something that PHP *as a language* offers over ruby.
This is because it doesn’t offer anything. The large standard library is it’s only advantage, and that’s annoyingly inconsistent, as well as being frustratingly omnipresent. What if I don’t want all of those thousands of functions available? Well, I don’t have a choice.
As with so many others things, though, the choice between a heavily (I hesitate to say “well” or “thoroughly”, since that doesn’t always apply) documented system full of inconsistencies and flaws, and a clean, straightforward system ends up going to the system with larger documentation.
What has yet to happen is for someone to mention something that PHP *as a language* offers over ruby.
Maybe nothing. That’s not the point of this discussion. It’s not a “my language is more [x] than yours” discussion. Nothing PHP has is “news”, but what it does have happens to work. Why can’t you anti-PHP guys understand that? As I said above, I think Python is a *very* cool language. I don’t know much about Ruby, but what I know looks good also. But just because Python is l33t doesn’t mean you can’t do perfectly decent work in PHP. Yes there are gotchas, but as I recall, Python has a few oddities and gotchas too:
http://www.ferg.org/projects/python_gotchas.html
And in the end, if you want the ultimate “programmer’s language”, you still have to go all the way back to LISP (which I also think is a very cool language)
Point is, while PHP may be “boring” to you, you can still get the job done, and quite succinctly, so there is no reason why someone should just throw away everything they have done in PHP.
I don’t understand why every time OSNews posts an article about PHP, the trolls come out and start flaming it. We get the point. You don’t like PHP. Fine, go use something else. We like PHP and will continue to use it, unless WE decide if and when learning another language would be beneficial.
Is it really any skin off your nose that we use PHP? Really?
Jared
By the way, did you ever think how it would be nice for PHP to have the equivalent of a ‘tied hash’ as in Perl? (see http://www.perl.com/pub/a/2001/09/04/tiedhash.html )
Well, with PHP5, you can make any object behave like an array by using the ArrayAccess interface. This means that you can define any class to present its public variables as array elements, and overload the array access and update operations to call any methods inside the class. It behaves like a normal associative array (hash), except that all the mechanisms for updating those values happen behind the scenes, even such things as database interaction, saving to file, etc… When you access an element of that array, the query or file fetch occurs, and when you update a value of that array, the UPDATE query or file save occurs behind the scenes. A very useful abstraction in certain situations.
See other new features:
http://www.omniti.com/~george/talks/OSCON2004-OOPPHP5.pdf
It is skin off my nose. The continued perception that PHP is the greatest thing since sliced bread makes it harder to convince potential clients to allow the use of languages and technologies that are in every conceivable way better, whether you like it or not.
PHP also means I have to compete with a lot of really bad, often lazy programmers who call themselves web developers, and more often than not they win, because the client has heard of PHP and has been convinced it’s the latest, greatest thing.
It’s the same reason “anti-MS trolls” get so worked up. In that case, people using Windows is hurting them. It creates an enironment that’s harder to work in.
Yes. It’s really frusterating to see crap languages get popular when there’s better technology out there that is losing attention for it.
Attention = industry use, jobs, books, etc.
As he said…it’s quite like why ppl get upset about MS making news over nothing at all.
*still waiting for one good reason to use php over ruby*
and whining about “having to learn a new language” is bs. if that’s your mentality you shouldn’t be programming at all.
seriously. people that can’t evolve, grow and learn new skills need to stay away from software development.
Sorry folks, those are non-issues. PHP isn’t as popular as Java for lots of development scenarios. Lots of people have heard of Perl. Python is growing by leaps and bounds. If you can’t compete with crappy PHP programmers, that’s too bad, but that’s no reflection on PHP or the people who use it to create quality software. There are crap Web designers out there — does that mean Photoshop and HTML are evil? I can’t compete with them sometimes either. That’s business.
I also have to make sure sites I create are compatible with IE. Do I like that? No, but that’s what most people use, and I have to make sure that everyone, big and small, is supported.
To the people complaining about PHP people not wanting to learn another language, how about yourselves? Learn PHP and then you can compete. If you don’t want to learn another language, then Ha!
PHP is not a toy language. It powers many huge Web sites by companies who handle millions of hits a day. Just like MySQL, another product that narrow-minded geeks like to complain about. If all you folks do is whine, perhaps that’s why people aren’t hiring you. Just a thought, eh?
Regards,
Jared
Yes. It’s really frusterating to see crap languages get popular when there’s better technology out there that is losing attention for it.
Yes, I agree. However, I don’t consider PHP a crap language for simple web development. I used to, but now it has evolved a bit and it’s actually quite nice IMO. There are however some really annoying things like magic quotes. But if you learn to live with that, then it’s a comfortable language.
As I said in an earlier post, I wouldn’t use PHP for making desktop applications. To me it’s just not a suitable language for that. I use whatever language suitable for what I want to do, and I learn new languages all the time. I’ve still found PHP to be pretty nice nowdays. And that’s coming from someone who tends to feed the underdogs.
actually, people add this in lots of languages for, well, decades. At least I can think of python,perl,ruby,c# and smalltalk that let you mimic the array access.
Guess what? Even Hash like! Oh, wait, hashes are arrays in php for some strange reason..
Also what should “php is great at handling arrays” means?
What language were you using that were so crappy at handling arrays to not reach the level of php?
The huge cool feature list of php5 is what many other languages took for granted since the last millennium.
PHP is there cause it’s simple, c-like, and fitted a soft spot in its starting days.
If you want to leave with lotta parentheses, a dumb OO system, lack of functional goodies (closures etc), and inconsistent library names it is the way to go.
There are lots of people that can live happy with this,and this is ok, but please don’t even think that php innovated someway..
PHP is not a toy language. It powers many huge Web sites by companies who handle millions of hits a day. Just like MySQL, another product that narrow-minded geeks like to complain about. If all you folks do is whine, perhaps that’s why people aren’t hiring you. Just a thought, eh?
Actually, I know PHP, and I know MySQL. I can’t compete with really bad web developers, though, because they have me so hugely outnumbered. ๐
Yes, you can use these tools to build really cool stuff. However, that requires a lot of discipline, and a lot of code written to accomplish tasks that programmers using either more advanced programming languages or more advanced databases get for free. So I say kudos to those folks, but I also wonder what they could have accomplished if they’d used better tools.
Unfortunately there aren’t nearly enough of them out there to pardon PHP.
Until it supports multi-lingual features.
In Far east asia, this is essential feature to almost any applications. But PHP-CLI fails even on any CJK version of “Hello World”
i guess ruby’s getting more popular. i’ve seen more ruby posts in this thread than python! i have yet to find a language better than ruby, or scheme. those two are without a doubt my favorite two langauges to code it. ruby for oop, scheme for functional goodness.
actually, people add this in lots of languages for, well, decades. At least I can think of python,perl,ruby,c# and smalltalk that let you mimic the array access.
I never said this was an innovation. Just that it’s great that PHP finally has this feature. Go back to your cave. And by the way no PHP user in this whole thread has said that PHP is the greatest thing since sliced bread. Nor has any PHP developer said that it “should be used for everything”. I actually like using Python, and would definitely use it for most client-side GUI apps.
I don’t know why all you people are so angry at PHP. If you can do so much better with Python or Ruby, what’s stopping you? I personally think PostgreSQL is vastly superior to MySQL, but I don’t complain about MySQL users. In fact, they make me look good, when I migrate an app to PostgreSQL and actually manage to guarantee some data integrity. So, go and use this horrible mediocrity that is PHP to make yourselves look so much better. You have my permission ;-).
Alas, I cannot claim it’s mine, but Rails seems to be demonstrating the power of Ruby to a lot of people.
http://www.rubyonrails.org
Come now, come now, you don’t have to be so dumb now.
Even Ruby sites (http://www.rubyforge.org) use PHP!
and i’ve seen linux sites using, asp. what’s your point?
Reg Globals
ini_set(‘register_globals’,’off’);
that fixes fopen problems (along with 95% of the other php security vunerabilities) unless it’s user input, in which case you turn Safe Mode on, don’t run as root, and do input validation.. then they can fopen whatever the hell they want
Taint Checking
striptags(), mysql_escape_string(), etc etc
did I miss anything? php is a secure language.. you just have to know how to use it