by api · 14y ago · 0 comments
It just works.
If you have a web server configured to run PHP, then it's ridiculously simple to get a page to execute. Just put a .php file on the site and it runs. Done.
You don't have to jigger a bunch of components, dink around with a bunch of Tomcat or Ruby on Rails or proxy or other annoying settings. Most other web execution environments have a bunch of Rube Goldberg machine components you have to plug together to get them to work. It also performs very well and supports a lot of things.
...that's not really inherent in PHP. You can configure Python to be just as easy, and some webhosts do this. Node is pretty much always this easy. Django and Rails can also be this easy (if you've ever used Webfaction, you'll know what I'm talking about).
And, on the flip side, if you want to install configure PHP yourself, it's actually not that easy. It's trivial to get a PHP page to execute given a web server configured to make it trivial (but, again, that's true for almost any language), but configuring the web server isn't trivial.
<?php Print "Hello, World"; ?>
I can wrap that in a little HTML, to get valid return to the user.
I don't know what Node.js's hello world looks like. From google it looks like the following. This is a heck of a lot more things to understand.
// Load the http module to create an http server. var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests. var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World\n"); });
// Listen on port 8000, IP defaults to 127.0.0.1 server.listen(8000);
// Put a friendly message on the terminal console.log("Server running at http://127.0.0.1:8000/);
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
That is, admittedly, longer than PHP but it's arguably easier to understand what's going on, much easier to modify and extend, and just as easy to copy and paste off the web.For Sinatra:
require 'sinatra'
get '/' do
"Hello World!"
end
Again, does PHP have any real advantage here? Python solutions will be similar. And in all those cases we can turn this into a full app with only a few more lines.Consider this example of a completely function blog: https://github.com/mitsuhiko/flask/tree/master/examples/flas...
There's 76 sloc of code in the blog app, plus tests, templates, database access, and more. And it's clean, clear, and simple. What does the PHP equivalent look like?
Of course, PHP is still shorter for hello world...mostly because it's running as a CGI app. This is a terrible, archaic idea for all the reasons that get discussed when people talk about Why PHP Is Terrible, but nothing is stopping you from configuring other languages that way. So here's a real apples-to-apples comparison:
Python:
print "Hello world!"
Too long? Try Ruby: puts "Hello world!"
19 characters including white space? Beat that! :)Imagine you're some mom who's made a static website for some group you're a member of. You made 20-30 static pages with Frontpage or something like it.
It's a much MUCH smaller step to rename a file .php and add
<?date('Y-m-d')?>
Than it is to wrap your head around what that node JS or python is doing.It's exactly that integration that makes PHP win. If someone would make a similar integration for a better language (and made it fit the other niches that PHP fits that nothing else does) they'd stand a great chance of changing the world.
The problem is, no one gets it. Just like you you're fundamentally missing what people like about PHP. It's not the language that they like. It's that it's HTML+server scripting. No setup needed, no external files needed, no libraries to learn, no security issues at least at the start. It just works, in small increments above the level of static HTML.
One other big advantage of PHP, which I find as time goes by, is (boringly) its broad stability. I first learnt PHP over 10 years ago, and what I learnt then still works now. Web apps are a very small part of what I do, I knock together the odd small one every couple of years or so.
I did do one app in Rails, but when I next came back to look at Rails about 18 months later, it seemed enough things had changed my app broke in various ways. I find I increasingly appreciate a large community, a book or two, and some stability.
Obviously if your day-to-day life is web design that doesn't apply. My day-to-day life in high performance algorithms, and I was using new C++11 features as they got implemented in compilers, and have played with many languages in that area.
From Slim: (http://www.slimframework.com/)
require 'Slim/Slim.php';
$app = new Slim();
$app->get('/hello/:name', function ($name) { echo "Hello, $name!"; });
$app->run();
From Silex: (http://silex.sensiolabs.org/)
require_once __DIR__.'/silex.phar';
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) { return 'Hello '.$app->escape($name); });
$app->run();
Hello World!
...and saving it as a .php file. I think you'll have a hard time finding something more concise. :)In reality, one of the things that makes PHP an easy language to pick up is that you don't have to have all of the overhead of learning how it works. Novices sprinkle bits of PHP surrounded by HTML-looking tags and all of the sudden they're up and running.
I started with PHP myself, and found it to be an interesting and frustrating language (needle or haystack argument first?). It's often saved by an easily searchable manual and a huge user base.
PHP is a shoot-yourself-in-the-foot language. It'll happily let you do dangerous, awful, bad, bad things. This wouldn't be such an issue if there was a higher barrier to entry, the real problem is it's easy to pick up (is that really a "problem"?), but that's a feature not a bug.
The benefit of being able to output HTML by default, drag+drop files over FTP or SCP to get it working, and a lanaguge that a lot of developers already know through client scripting.
I wonder if ASP still has support for Javascript as a server lanagage.
An example would be something like:
<%
require('mysql');
require('session');
require('cookie');
require('datetime');
blog_data = mysql.fetch('select * from blog_meta');
posts = mysql.fetch('select title, content, author, pubdate, permalink from posts where published=1 order by pubdate desc');
user = session.get_current(cookie.sess_id);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title><%= blog_data.title %></title>
</head>
<body id="home">
<div class="container">
<% for post in posts: %>
<div id="post-<% post.id %>">
<a href="<% post.permalink %>">
<h1><%=post.title %></h1>
</a>
<div class="postmeta">
<%=post.author %> - <%= post.pubdate.format('%m %d %Y') %>
</div>
<div class="content">
<%=post.content %>
</div>
</div>
<% endfor %>
</div>
</body>
</html>
as simple as PHP, but Javascript - and simple webapp modules like session, mysql, etc. etc.However, for building applications that might get deployed somewhere, or talk to a database in a secure fashion, it's a nightmare.
For ruby, python and perl, it's just as easy to make a hello world like that, all you have to do is execute it as a CGI. Node.js is a FRAMEWORK, not a language, you can equally make it super simple to execute javascript the same way by again executing it at as CGI, it's actually even shorter because you don't need <? ?> tags.
The reason the Perl,Python and Ruby communities don't make apps like that is because it's a bad idea. In ruby,perl,python,javascript if you really want to you can. PHP makes it incredibly hard to not be stupid in the way your app is setup. PHP saves you a few minutes of following a rails cast in exchange for a lifetime of hell.
The crux of why most programmers disdain PHP is because of the poor choices for maintainability and ease of building larger applications, also because the PHP community has no idea what they're talking about for the most part.
Most PHP programmers couldn't even tell the difference between a language, a framework, a template system, probably because PHP doesn't have any separation of concerns. The language, framework, standard library, templating system are all mashed into one godawful mess.
But yes, it's really easy to build hello world applications. Alternatively, if you want to build a hello world application you could just write:
Hello World
in a text file and be done with it instead of exposing yourself to all the security vulnerabilities inherent in PHP. (Yes, even hello world is insecure in PHP)In what decade? Ever heard of PDO? Prepared statements? PHP is actually more secure when it comes to database connections than all of the wonderful alternatives you mentioned.
Facebook, Wikipedia are begging to disagree.
I am well aware that PHP has had security issues in the past, but I am also aware of the fact that it gets a bad name because people read blog posts and then decide the sky is falling.
<html>
<body>
<form action="handlerhere" method="post">
<label>Name: <input type="text" name="name" /></label>
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
The handler is a separate file. This whole example is from back around my first year of programming, when (among other silly things) I thought indentation was stupid. (Which was one reason I didn't like Python to begin with even though I love it now.) The PHP handler: <html>
<body>
<?php
if (isset($_POST['submitted'])) {
$name = htmlentities(stripslashes($_POST['name']));
echo "<p>The submitted name was \"$name\"</p>";
}
?>
</body>
</html>
And the Python version (notice the silly one space indent (I use two now!) and the silly semicolons): #!/usr/bin/python
import cgi;
def main():
form = cgi.FieldStorage(); # instantiate only once!
name = form.getfirst('name', 'empty');
# Avoid script injection escaping the user input
name = cgi.escape(name);
print "Content-Type: text/html\n";
print """\
<html>
<body>
<p>The submitted name was "%s"</p>
</body>
</html>""" % name
if __name__ == "__main__":
main();
Oh, and with the Python version I had to add this file to the directory it was in which I didn't totally understand at the time: Options +ExecCGI
AddHandler cgi-script .py
Options +Indexes
Admittedly not that hard. I had an .htaccess file already for fun 404 handlers. I vaguely remember being confused for a while because the .py script didn't have the right permissions on it so the server's apache just refused to execute.The complexity isn't that much different, it's just that Python provided no compelling reason for me to switch to it. (And, frankly, even with Flask still doesn't for simple single-file web-page scripts. Applications, on the other hand...)
console.log('Hello World')http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on...
Looks pretty easy to me.
Then again, I compile my own version of Apache with FastCGI and PHP-FPM all the time.
And if you're relying on your webhost having your language-of-choice already configured for you, well, you can find webhosts with most of the obvious choices already configured. So what does that leave?
I would suggest that PHP does not, today, have any advantage over other competing languages and frameworks in the "it just works" sweepstakes.
What we need is a Douglas Crockford for PHP.
A “PHP: the good parts” and a good PHPlint would do wonders to the language and community.
It's very rare to find webhosts that configure Python, Ruby, Node or the rest to be as easy as "upload a file, click reload."
One reason is the wonderful statelessness of mod_php's execution model: it forks a new process for every request, and destroys it at the end. It's inherently a much more stable environment than the competitors -- you can't save anything inside the interpreter for future requests. This makes server administration easy, and probably shared hosting easier to manage.
My point was that this is true for any language.
You seem to be suggesting that the odds of picking a web server at random and having it be so configured for a given language is pretty low, which seems like quite a different issue, and (in my view) a pointless one. Nobody picks a webhost at random and just hopes that they have support, so the question is can you find support if you want it. :)
In any case, it's also incorrect in the case of Python and arguably Ruby. The first three mass-market shared webhosts I can think of are Dreamhost, Webfaction, and Hostgator. Without any configuration at all, Dreamhost supports both Python and Ruby scripts as CGI programs, Hostgator supports Python scripts as CGI programs, and I believe Webfaction does as well.
(Node is a lot newer and, admittedly, doesn't seem to have any support among mass-market shared webhosts.)
I think, because PHP servers are so stable, that's part of why they're so common on shared web hosts. For example, running a WSGI server is much more complicated. If a WSGI app goes bonkers you have to restart the relevant server. In mod_php, just kill the process.
What would be interesting is a forking-style server for Python or another reasonable language.
It's not like PHP solves any of the real problems, like how to manage upstream changes, how to automate releases, how to rollback database schemas after a bad release, and so on. The reason why nobody ever talks about those things is because everyone handles them with downtime and swearing. That's not software engineering. That's fail.
Edit: To be a bit less snarky here, you are arguing that the sunk costs of learning to use a computer justify more effort spent in getting a web site running just because the effort already sunk dwarfs the additional effort required. But in fact not only is that cost sunk but once I've learned to use a computer my time and energy is suddenly more valuable.
There's a place for doing things fast, and a place for doing things right. You're wrong to assume everything requires the latter.
I worked tech support for a shell provider in the 90s and I helped a LOT of users work through the mechanics of setting up a cgi-bin. One of the most difficult concepts for users without a technical background was file permissions -- specifically the execute bit.
I believe PHP's ease of use primarily stems from what most would consider a security vulnerability: mod_php instructs apache to execute code from php files without the execute bit set. This is inherently the same sort of issue behind email worms on Windows -- exec handlers which pass code to an interpreter without honoring the lack of execute permissions.
It's a terrible idea, and also widely popular. I have personally seen users switch from Perl to PHP purely because they could not figure out how to set the execute bit in their Windows FTP program.
The rest is inertia, as far as I'm concerned.
I also think it's a micro-optimization for the wrong thing. It's like buying a car based solely on how close the dealership is to your house. You only have to do the initial setup once (by definition) but you have to live with the consequences of your environment of choice every single day for the rest of the project.
I'll go to a crappy coffee shop if it's the only one around, I'm willing to go very far out of my way to get to the car dealership that has the right thing for me.
But in the end, what's most important is that the car/language gets you from Point A to Point B.
Really?
The easy of deploying a single-purpose script / hello world is always trotted out as the reason PHP ever became popular.
Yes, even in yesterday’s big item “PHP: A Fractal of Bad Design.”
Comparing the rails app config system to PHP.INI, ini_set, .htaccess and the million other ways to configure PHP and considering the app config system to be a rube goldberg machine is really unjustified, the very configuration of PHP itself pretty much non-deterministic.
As other have pointed out in this thread, you can easily configure a web server to behave like that for files in other languages like Perl or Python.
PHP is a reasonable templating language. That's what it should be used for.
If you use a framework, you're golden, but at that point you could be using any language.
It's like the people 10 years ago saying "if [other OS] was as popular as Windows XP, it'd be overrun with malware too". No. The issue wasn't Windows marketshare, it was the completely crap security.
(...on that note, I just realized how useful it would be if Dropbox's Public folder executed PHP.)
Looking for happiness? Heroine "just works".
Hungry and poor? Eating McDonald's three meals a day is cheap and easy.
It might sound scary. But in reality 'merit' is not the only motivating factor for adoption of anything.