Want to use a relational database? You don't need to install a library (often one that requires a C extension in a language like Python or Ruby). It's just part of the language. Just do `$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); $result = $mysqli->query("SELECT * FROM x");`.
Want to get things out of the HTTP request? PHP has already parsed everything into the `$_REQUEST` associative-array (map/dictionary). You don't need something like Flask or Node to deal with decoding the request.
Cookies, response writing, uploaded files, GET/POST vars, sessions, and many more things for web development are built-in. Heck, PHP even simplifies templating because PHP is literally its own templating language.
If someone gave you Python without any packages, would you be able to handle a request, see a GET variable, and put it into a page? I haven't used PHP in a decade and I can immediately think how to do that. OK, create index.php with whatever HTML I want in there and then put `<?= $_GET['theVarName'] ?>`. Done. That's it. If I want to get something from a db based on that get var, it literally has the functions/objects to connect to and retrieve stuff from the db. With Python, are you going to write your own database driver? With Python, are you going to open a socket and start manually handling all the HTTP stuff?
I'm not arguing that PHP is a better language than Python. I'm just saying that it was literally built to handle web requests and create the kind of web app that a lot of people make. The point is that with PHP, you usually don't need to do anything. You can just create a file with the `.php` extension and put in bits of dynamic code and the language has everything you need to create an app of the kind users expect without having to add libraries.
Does this matter? I don't think it does most of the time. I think OP's point was that PHP gets new people from nothing to "WOW, it's showing up on the screen!" with almost no effort. "OMG, I can save the form to the database and then display the information!" Tools have gotten a lot better for a lot of languages over the past decade. Still, nothing beats the ease of not having to deal with anything. PHP offers that.
PHP also offers a dead-simple deployment. You put your files on a server with PHP installed and you're done. Lots of shared hosting supports this precisely because the PHP language includes most of what people want to use. Because the language includes these things, that code is shared between all the users on the server rather than re-parsed and re-stored in memory and re-executed for every user. I mean, if a bunch of people are using Python, they're all going to bring DB libraries with them and many will be slightly different versions, etc. Likewise, some will use Django, some Flask, some other libraries - and again, all different versions. That makes hosting a bit more expensive (though that has gotten cheaper with serverless).
Most of the time if you want a library, you can download a directory of files and then use `require` or `include` to bring it in using relative paths. No worrying about environments or installing things. Just copy a folder!
That's the thing about PHP: it's basically a language and web framework in one with a simple development model.
I don't think PHP is a great language. However, I think it's important to understand why it was so successful. Back in the day before Xen and VPSs, it was hard (expensive) to get a server with root or get someone to install a package on a server for you. Virtualenv wasn't around forever (I remember easy_install before pip). You'd come across things that you just couldn't use. You'd end up fighting with a lot of things. You'd have to start long-running processes or use fcgi or all sorts of things that were annoying. Today, it's a much better landscape - partly because tooling has gotten better and partly because compute and RAM costs have gone down. But back in the day, you could just stick an `index.php` file in a directory set up for you and go to town. Instant gratification with zero setup. Even today, we use tools like NPM, Yarn, Virtualenv, pip, RVM, Gems, etc. to manage things. And I think that libraries are a lot better than having all those things built into the language. However, if you're looking for someone to be instantly productive on a live server, PHP does that. You never have to reload things or stop processes or set up a reverse-proxy. You never have to install anything (presuming the server has PHP set up).
Again, I think that libraries are better. They let you upgrade code without a language update. I think package managers are better than just downloading directories and copying them into a folder. I think that a long-running processes has a lot of benefits over re-evaluating the code every time (and PHP has modes that support this, I believe). But the interesting thing about PHP is that you don't need to do any of those things which do incur cognitive overhead. I mean, I don't want to be a PHP programmer, but if you're looking to take a student with one CS course under their belt and give them some instant joy, PHP really hits the mark.
Even the way you can program simple things is so easy. Ok, we all get directory trees and so if I go to `/apple/pear.php` the web server will execute that file. I don't think that routing in Flask is that hard, but there's more indirection. We can just go to pear.php and make it whatever we want. Again, this isn't about clean design and many advanced PHP projects use their own routing. This is about someone who doesn't know much being able to access GET/POST variables, call functions with SQL statements in them, and generate some HTML. No MVC, no DRY, no whatever. Just code that executes top to bottom in that file. Maybe you've used `require` or `include`, but again, that's just like copy/pasting that file at that point in the code - and it can be done anywhere and do anything including output HTML.
To be fair to PHP, I don't think good PHP is written that way. I just think there's a somewhat special simplicity to being able to just start writing random code in a file and have a web server execute it - and that code can do real stuff. No libraries, no configuration, no worrying about how the whole application is laid out. Just a file that executes. And before VPSs and such, it was so much easier. If you don't remember years like 2008, there are amazing threads about people trying to deploy things like Rails apps and the problems they encountered. It wasn't like today where we have much better tooling - but PHP always just let you upload some files that it would execute.
Sorry for this rambling thing. I don't think you should be learning PHP or anything, but I'll always have a bit of a special place for it because it made programming real things really accessible when I was an undergrad. I wasn't going to be renting a server with 1GB of RAM for $100/mo to run a Java project. Even a smaller server to run some Ruby or Python would have been more expensive and required a lot more work (which, frankly, I wasted a lot of time doing out of interest, but that's beside the point). PHP let me get something for $5/mo and just stick files on a server that someone else admin'd that would just work. I didn't have to do security patches, monitoring, process management, etc. I could just write some files that spit out some HTML, read GET/POST vars, handled sessions and cookies, connected to databases, etc.
I'm being pedantic here, but technically it's still a C extension that most package managers install by default.
> Want to use a relational database? You don't need to install a library
These are optional packages in most distros, so you (or your admin) effectively need to install a library.
> Want to get things out of the HTTP request? PHP has already parsed everything into the `$_REQUEST` associative-array (map/dictionary).
And yet we have PSR-7 (https://www.php-fig.org/psr/psr-7/) which define HTTP messages and most frameworks comply with it.
> Most of the time if you want a library, you can download a directory of files and then use `require` or `include` to bring it in using relative paths. No worrying about environments or installing things. Just copy a folder!
There are gazillions of libraries at packagist.org, meaning these are installable via composer. Many of these expect composer to resolve dependencies. Major frameworks can't practically be installed without composer.
> PHP let me get something for $5/mo and just stick files on a server that someone else admin'd that would just work.
There are cheap cloud solutions for most languages...
> PHP also offers a dead-simple deployment. You put your files on a server with PHP installed and you're done.
... with push-to-deploy.
> Heck, PHP even simplifies templating because PHP is literally its own templating language.
And yet we had alternate templating engines for PHP since forever (remember Smarty ? know Twig now ?). Stuffing templates with "htmlspecialchars" or "htmlentities" is no fun.
> if you're looking to take a student with one CS course under their belt and give them some instant joy, PHP really hits the mark.
As stated, Javascript is better at this.
I could go on, but my point is that you're describing qualities that were competitive advantages in the past (somewhere between 1 and 2 decades ago), but are no more.
One of the best decisions after I decided to stick with PHP for my web site was ditching all those alternatives and go with PHP alone, using functions as tag libraries.
Care to post an example ?
You have the PHP business logic code as usual.
Then instead of doing
<a href="http://somewhere">A link</a>
You have a view function that does that for you function web_link($url, $title) {
echo "<a href="$url">$title</a>";
}
And then on the HTML markup, you place those calls as tag libraries in other similar frameworks, web_link("http://somewhere", "A link")
Very low tech, but does the job similar to JSP tag libraries and provides some organization.Other than being slightly more concise and appearing more functional, I don't see the benefit of this over something like Twig or Smarty. At some point you're going to wind up re implementing features that already exist in a framework anyway.
I used it only for my web site, and many alternatives did not exist back when I did this.
So with php is definitely possible to build large web apps as it is possible with other stacks. However compared to other languages with php it is very easy to get started as well as mainly the first dev setup is quite easy, although for professional work there might be no difference between the different stacks...
Is vulnerable to XSS
This I assume is another reason why HTML "aware" templating engines are now popular in PHP rather than toggling with <? and ?> between HTML and PHP
PHP makes it really easy (IMHO) to write insecure code, and most samples you find online are. Not many people starting with PHP learn
`<?= htmlspecialchars($_GET["theVarName"]) ?>`
or equivalent.
Well, it depends. On Linux you may need to install a package like php7.3-mysql, depending on what was installed as a part of the initial setup.
PHP has a package manager IF you want to use the vast offering of open source PHP libraries and frameworks.
https://getcomposer.org/download
To use it, create a composer.json and run:
php composer.phar install
On my systems, I like to alias it so that I just have to type: composer install
Alternatively, if you use a Jetbrains IDE (i.e. PHPStorm), you can usually install dependencies with one click.> And to be fair, as a naive non-PHP user, I have 0 clue how you would go about setting up long running scripts that don't necessarily correlate to a web request.
php /path/to/script.php
It can run as long as you'd like.
If you want to do it on demand (i.e. writing a webserver in PHP), that's difficult. But generally: Why would you want to do that? It becomes an X-Y Problem.
I wasn't trying to debunk a claim that something is impossible.
No, you didn't.
What you probably meant is "run it from cron, or create an OS service that starts from init.d/systemd/whatever". Which answers the question, but isn't very impressive from a technology stack.
If they wanted something more specific, they should ask for what they really want.
I also meant long-running processes, which it sounds like is still problematic. To be fair, it doesn't sound like PHP was ever designed to be used for long-running processes, to which I guess "Fair enough", but that might make things like a job worker queue more complicated if you want to write the workers in PHP (it sounds like you'll need a dispatcher in a different language because that would be a long-running process).
Have you reported them upstream? That implies a memory safety or garbage collection bug.
Is this not the same with PHP? If you want to use packages then you'll need to use a Package manager, you still need command line experience etc.
Node.js comes with a package manager NPM, and has no need for anything like virtualenv's to keep packages separate per project.
Python doesn't need virtual env's at all, it's just good practice.
And to be fair, as a naive non-PHP user, I have 0 clue how you would go about setting up long running scripts that don't necessarily correlate to a web request.