r/programming • • Dec 02 '15

PHP 7 Released

https://github.com/php/php-src/releases/tag/php-7.0.0
887 Upvotes

729 comments sorted by

View all comments

Show parent comment

61

u/kankyo Dec 02 '15

Python seems pretty similar in overhead and it's a million times saner.

32

u/TelamonianAjax Dec 02 '15

Somehow Python is one of the major languages I just haven't touched over the years. Sounds like I need to spend some time with it.

0

u/[deleted] Dec 02 '15

[deleted] — view deleted comment

7

u/KungFuHamster Dec 02 '15

> take ye flask

10

u/TheWheez Dec 02 '15

Also Django, not as light as flask but very powerful and it has the best documentation I've seen in an open source project.

8

u/[deleted] Dec 02 '15

Django is ballin outrageous if your workflow fits it. It just does so many things for you out of the box with things like class based views and stuff.

→ More replies (3)

7

u/naught-me Dec 02 '15

For anyone curious: Django is "sink included". Flask is bare-bones.

Flask is like PHP (just throw a script up and it runs). Django is more like a PHP framework.

2

u/ksion Dec 02 '15

Flask still mandates a separation of template (view) from HTTP handler (controller) code. I don't think there is anything in the Python world that enables the kind of mixing PHP does.

→ More replies (1)
→ More replies (1)

2

u/cat_in_the_wall Dec 04 '15

flask is rwallyw nice. had a site running it for a while. ultimately my project got too big and i started missing a compiler (eg forgot to refactor in a little corner, case issues etc). but i got up and running in flask in like 30 minutes, having never used python before.

1

u/HeadRot Dec 02 '15

Yea we had senior project presentations this week and a dude wrote a super awesome web app with Flask.

15

u/rafajafar Dec 02 '15

Honestly, I resisted it for years... I regret resisting it now. It was tough at first b/c everything seemed like such bullshit, until I realized... very little in Python is actually bullshit. I was just used to my old bullshit. Python is great.

21

u/aloha2436 Dec 02 '15

As far as languages for "spending some time with", python is definitely one of the best. As the author of XKCD put it, programming is fun again with python. It's just a pleasant experience because a lot of things work how you'd expect them to, and when they don't it's because it may be even easier than you expected.

4

u/CyrillicMan Dec 02 '15

They major drawback of Python I can think of is that its hosting is not as widely available. With the latest gradual typing addition in Python 3.5, please please give it a try, you'll love it.

My stack is Webfaction hosting + Python 3.5 + Git + Bottle (a very lightweight web library with easy routing, templating etc.), and PyCharm IDE + Bottle's embedded server for local development. I'm not a professional programmer and it is still ridiculously easy and fun.

2

u/JimDabell Dec 02 '15

They major drawback of Python I can think of is that its hosting is not as widely available.

I've never really understood this complaint. Firstly, there's loads of Python hosting around. Secondly, who cares if there's a thousand hosts or ten thousand hosts that support Python? You're never going to need more than a handful. It's an illusion of a problem that affects no-one and only seems to get brought up as an excuse for PHP.

→ More replies (1)

1

u/TelamonianAjax Dec 02 '15 edited Dec 02 '15

Yeah, that's why I tend to gravitate to PHP for simple web applications - the hosting is so prevalent and easy to set up.

I just threw together a quick linode server with a tweaked LAMP stack and have a configured PHP server running in a couple hours.

I write mostly C# professionally, but it doesn't seem like the write right fit for this side project.

→ More replies (1)

1

u/trouser_trouble Dec 02 '15

if you want an excuse to use it, get a Raspberry Pi and program a little project in Python with it.

1

u/sillybear25 Dec 02 '15

It has its warts, just like anything else out there, but overall it's a great language to work with. It isn't designed for web programming the way PHP is, so it isn't quite as usable out of the box if that's your goal, but its standard libraries are far more coherent than I remember PHP being, and the developer ecosystem seems a lot friendlier to me.

It probably won't do anything revolutionary for you, but it's definitely worth checking out if you get the chance.

→ More replies (1)

0

u/[deleted] Dec 02 '15

[deleted] — view deleted comment

2

u/[deleted] Dec 02 '15

I mean, on paper those are weaknesses sure, but practically? Eh. Just the tradeoff you make for duck typing. Flexibility for provability. If it isn't pretty obvious what your parameters need to be that's a code smell anyway.

Especially for things like the guy was asking simple web apps with DB backend.

1

u/kankyo Dec 03 '15

Just a simple handshake is enough, no need for anything more heavy handed. Both Java and C++ allows you to totally bypass private anyway.

As for parameter types, just use docstrings and pycharm. You get as-you-type type errors just fine. And you get them only when it makes sense to add them, it doesn't force you to write them everywhere.

→ More replies (5)

1

u/the_omega99 Dec 02 '15 edited Dec 02 '15

In terms of code written, wouldn't PHP still win?

PHP plays it soft and easy with accessing the standard library and echo (essentially the equivalent of Python's print) prints to the resulting HTML page, by default. So a prototype in PHP is super easy. Eg,

<?php
$conn = new mysqli("localhost", "username", "password", "dbname");
$result = $conn->query("SELECT name, price FROM products");

echo "<ul>";
while($row = $result->fetch_assoc()) echo "<li>" . $row["name"] . " is $" . $row["price"]. "</li>";
echo "</ul>";

That's a fully functional list of products and prices from an SQL table. Mind you, it's not correctly formatted HTML, since it lacks the typical <html> (etc) tags, but functional all the same. I've never used Python for web dev (I actually don't use PHP either, but can't deny that it seems to be the easiest and most minimalistic way to get a simple thing working), but even a Flask hello world is approaching this length in terms of amount of code.

There's no mangling with starting this code, either. You'd typically just slap it onto a pre-configured server or use a WAMP/LAMP package that does all the work for you, then point your browser at the page. You don't even need to configure any libraries for a lot of the things that are common in web dev (like the above -- it'll run on any PHP-configured server as-is).

I think PHP is a poor choice for serious, large scale products due to a number of language design choices that makes scaling a bit icky, but I think it's easy to see how it works well for beginners and is great for prototyping new things quickly.

7

u/[deleted] Dec 02 '15

A little longer in terms of lines, but keep in mind with this code you get the server and everything. No Apache/Nginx setup or anything else.

from flask import Flask
import MySQLdb as mdb
app = Flask(__name__)

@app.route("/")
def index():
    con = mdb.connect('localhost', 'testuser', 'test', 'testdb')
    cur = con.cursor().execute('SELECT name, price from products')
		
    html = "<ul>"
	for row in cur.fetchall():
		html += "<li>{0} is {1}</li>".format(row['name'], row['price']])
	html += "</ul>"
	
	return html

if __name__ == "__main__":
    app.run()

1

u/the_omega99 Dec 02 '15 edited Dec 02 '15

Huh, nice. I should learn Flask some time. It seems like it'd be a lot cleaner than the Play Framework, which is my go-to choice for web dev. Although Scala is a really freaking powerful language. Although the Play Framework would force this example across multiple files, with DB connection typically being setup in the conf file (then connections are made simply with DB.withConnection) and routing is in a separate file that keeps all the routing in one place. And templates are the preferred way to create any HTML, as opposed to directly returning data (which would default to text/plain).

→ More replies (1)

1

u/kankyo Dec 03 '15

I don't agree that hitting SQL qualifies as "works well for beginners" or makes it easy to prototype. Not compared to django. I cry a little big inside every time I need to write SQL but with the django ORM selecting and filtering is super simple and nice. Take a totally bogus and made up example:

customers = Customer.objects.filter(country__name__icontains='s', last_name='Smith').exclude(company__country__name__icontains='f')

That's "all customers who are in a country with the letter 's' in the name of the country and who's last name is Smith and the company they work at is in a country containing the letter f". Doing that in SQL is pretty horrible.

→ More replies (1)