views:

118

answers:

7

My website already runs PHP for it's UI. I now need to code some scripts which will fetch data from certain URL's on a daily basis and populate the database.

If PHP 5, with its enhanced object oriented features and rich libraries is on par with Python on the command line, then wouldn't it make better sense for me to use PHP for both web and CLI/GUI development? But, I can't ignore the fact that people do use python almost exclusively for high level CLI/GUI scripting.

Which of them is a better choice?

+13  A: 

If you were just asking between PHP and Python without any background, I would say "take the one you know the best".

Here :

  • You (and your team) obvisouly know PHP
  • You already have a lot of PHP code to maintain
  • Your whole application is developped in PHP
    • Which means you probably have some components that could be re-used by the new scripts

Considering those three points, wouldn't be PHP a wise choice for the new components (Even if it's not necessarily primarily meant for CLI development) ?


I've been in your situation a certain number of times -- each time, the points I talked about were true...

And instead of having to teach my team a new language, and re-develop the components I already had in PHP, I always choose to write my CLI components using PHP.

Yes, sometimes, it's a pain (I'm thinking about memory_limit and/or safe_mode and/or max_execution_time, for instance) ; but most problems like that can be solved using a different php.ini file for CLI.

Pascal MARTIN
This is the way to go. Why add another language to your overall system just to handle a script that your main language can handle fine? The overall system engineering issue is important. To add a new language without a really good reason is the sort of mistake that gives a bad name to professional programmers and drives managers crazy.
Larry K
I would add that the question title and actual question asked are different; anybody got the rep to edit the title?
Beau Martínez
@Beau : I've edited the title ; hope it's more clear now -- and that the author of the OP will also think so
Pascal MARTIN
A: 

Usually I would say whatever your happiest with but Python vs PHP? Please!

Python wipes the floor in terms of simplicity and library support. It's also probably the easier language to learn for programmers of another language.

Conversely if you're integrating with an existing PHP site, you might want to share the codebase (provided it's not mired in HTML markup).

But really, unless you've got gallons of logic in PHP that you could reuse, Python FTW. And I'll add, this might be a good opportunity to free yourself of PHP for good. The first step to converting out your legacy PHP sites.

Oli
Its interesting however, that PHP has beaten Python in terms of popularity. I read an interesting article about it not long ago, that even though Python had a big head start, it just failed for becoming that ubiquitous language whcih we all know on the web.
Laykes
@Laykes PHP caters to the web in a very particular way. Python isn't quite so forgiving. It's very easy to get up and running on the web and do some interesting and useful things with little knowledge of the language and just a few lines of code. PHP's low barrier of entry is probablyt he most oft-cited reason for its (absurd?) popularity.
Andrew Noyes
Windows has more users than Linux and OSX. Doesn't make it a better system. PHP is so popular because it's so available. It's been a development option on cheap Linux hosting for well over a decade. Python has been around longer but mostly as a "proper" programming language and only recently have projects like Django really made it rock the web. It's playing catch-up.
Oli
@Andrew Noyes, yeah that was pretty much the conclusion of the article I read. The speed to a nice working development stack meant that people could becoming PHP developers in a matter of minutes. I think thats also attributable to mySQLs success. In the early days, they used to limit build time to 5minutes. If they couldn't get it up within 5 minutes then woul rethink it.
Laykes
@Laykes Yes, I think specific implementation details like those are important as well. Despite the fact that Python is probably the quickest language to learn (the syntax is very easy), PHP borrows most of its syntax from C/C++, with minor variances where its Perl roots show. It makes it easier for those already familiar with similar language like C++, Java, or JavaScript. Python is a breed of its own, which means the platform isn't the only limiting factor, but learning the language as well. With PHP, once you learn to precede your variables with a dollar sign, you're good for simple scripts.
Andrew Noyes
A: 

On top of the previous posters answer, I would say use PHP, not only because you may know it best, but because there are countless tools already out there which will assist you on this.

If you provide more detail, I wouldbe happy to link you to some.

Laykes
+1  A: 

Since you already know PHP, I think the main barrier there is learning a new language, especially in a team situation. If you were just making the choice yourself, that'd be one thing, but asking your team members to learn a new language doesn't really seem an effective use of time if the scripts are going to be a small part of what you do.

Regardless of which language you choose, the behavior that you're talking about strikes me as a "web scraping" system, so you might want to check around for pre-existing libraries that would help you do that, it's a pretty well solved problem. http://github.com could be a good place to start searching.

Tchalvak
A: 

Having used both, I advocate Python simply because of its elegance and simplicity. I would keep in mind PHP is aimed at a web-based domain and hence where it is most featured whereas Python is a general purpose programming language.

Beau Martínez
+1  A: 

I think on some fundamental level, even those of us who are more familiar with PHP than we are with Python, myself included, know that PHP really is a mess and Python is better. However, if you know a language well enough, those quirks that make PHP such a pain in the butt to deal with are a non-issue.

Just because PHP is a mess doesn't make it incompetent. It just makes it "harder" to work with, which is arguable. Python is designed and maintained in a very deliberate and academic fashion. While it caters much better to the intellectual needs of computer programmers, there's no reason to add that language to your workflow in the midst of a team full of experienced PHP programmers. PHP can do all of those things Python can do.

The purpose of a programming language is to make it easy enough to get work done. There's no reason to consider introducing another programming language to your workflow because it is, in some sense, better than the one you're already using. The importance is what the code does, i.e., the semantics of what your programs do, not the clarity or (lack of) verbosity of the language it's written in.

Andrew Noyes
A: 

As a separate answer to my previous one, people need to recognise that server-language-driven websites are never made up of one language or one skill-set.

In most cases you need PHP (et al), HTML, JS, SQL, CSS and .htaccess commands. All different languages but all just as essential. Tools for the job. You wouldn't tell somebody not to learn one of those if they wanted to create a website.

I think the same applies to utility scripting. PHP isn't neccessarily the best tool for the job just because it exists in the rest of the site. An amount depends on what's being done (and if code for that already exists in other parts of the site that could be reused by a PHP script) but at some point you have to weigh up how much easier Python might make something over PHP if the codebase will cover unique things.

Scraping in Python+BeautifulSoup is about as tough as breathing. Talking to an SQL database is not hard. But doing this in PHP isn't exactly tough either. I'd probably say in this case PHP would be simpler but if you have a spare 10 minutes, have a go with Python. It can't hurt.

Oli