Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Personally, I _like_ PHP because, at the core, it is a collection of utility functions to write webapps quickly. Here's a small set of those function that I would like to see somewhere else (like in Python for example):

Save a file: file_put_contents()

Load a file: file_get_contents()

Trim whitespace: trim()

Print a string (even numbers!): print $str

Dump an array: print_r()

Print the stack trace: print $e->getTraceAsString()

Want to embed HTML? Go ahead, _without_ any of this nonsense: print "<table><tbody><tr><td>"+str(val)+"</td></tr></tbody></table>"

Want to check for errors and improve the quality of the code? Go ahead! Use lower level functions like fwrite, fread, etc; most functions return FALSE on error, use try/catch, throw Exceptions, create custom Exception classes, go nuts with over-architecting if that's your thing.

Also, as someone who knows C and C++ rather well, I find PHP completely intuitive, very easy and flexible, as opposed to:

  if __main__=="__main__": 
      main()
and passing self around is just plain redundant.

The icing on the cake is that a huge part of the built-in functions wrap and extend existing C functions: strpos, strlen, so the language rarely gets in your way. The docs are pretty good (there's room for improvement).

So you can be a zealot all you want, but the truth is: the language doesn't make your code reliable - YOU make the code reliable and, from my experience, people who bash a language rarely know what they're talking about and it usually just boils down to personal preferences like braces vs no-braces.

Lastly, I wish you all the best in your fanatic bashing of a good tool that has proven itself appropriate for web applications. Everyone's tired of the following example, but I'm going to say it anyway: Facebook (and thousands of other large websites). Feel free to be in denial about that.



In Ruby:

    # Save a file
    f.write(str)

    # Read a file
    f.read

    # Trim whitespace
    " foo ".strip

    # Print anything
    puts 1 # even numbers!
    puts File.open("foo", "w+") # even file handles!
    puts [1, 2, 3, 4] # even arrays!
    puts [].class # even classes!
    puts [].method(:size) # even functions!

    # Dump an array (see above)
Python is similarly clear:

    # Save a file
    f.write(str)

    # Read a file
    f.read()

    # Trim whitespace
    " foo ".strip()

    # Print anything
    print 1 # even numbers!
    print open("foo", "w+") # even file handles!
    print [1, 2, 3, 4] # even arrays!
    print [].__class__ # even classes!
    puts [].remove # even functions!

    # Dump an array (see above)
Ruby and Python are extremely well documented. They have great standard libraries which wrap existing C functions too...and wrap them in a consistent fashion. They are also the second and third most popular language on Github, respectively...which means tons of great libraries to solve almost any problem you can think of.

Ruby and Python are just as easy to use as PHP.


Not sure about the Python ones, but the Ruby read/write file examples aren't quite fully done/correct here:

  # Equivalent of file_put_contents() in PHP
  # This could also be used for appending if the correct mode is specified.
  File.open("filename", "w") { |f| f.write("some data") }

  # Equivalent of file_get_contents() in PHP
  some_var = File.read("filename")


Python:

with open("filename", "r") as a_file:

    contents = a_file.read()


Ruby 1.9.3 added File.write("filename", "some data").


Cool. Didn't know that. Thanks.


I guess I should have specified "where f is a file handle":

f = File.open("foo", "r") f.read

f = File.open("foo", "w+") f.write(x)


I get the feeling you aren't very familiar with Python. Actually I'm hard-pressed to name a popular language in which writing a string to a file is particularly difficult.

You don't have to check for errors in Python; most functions raise exceptions on error, rather than letting your program continue with a known bad state.

The `if __name__ == "__main__":` pattern is a cute trick to distinguish a module from a file being run directly. It's not strictly necessary; just a nice habit. It's spiritually similar to the checks you have to do in PHP library files to make sure they're not being run directly.

You don't have to pass `self` around. You just list it as a parameter. There are design reasons for this, it's a tradeoff, blah blah. Think of it as the price you pay for getting to type 'def' instead of 'function'.

I'm not a C programmer, and I doubt many people writing web applications are, so how does wrapping C functions with C names help me? Are `'b' in 'abc'` and `len('foo')` that difficult to understand?

I like to think I've covered my reasons for disliking PHP well enough by now.

Okay. You're not the target audience of this article, then.


> a cute trick allow me to disagree about the cuteness of that. there are ways to distinguish a module from an executable file. For example, 'main' could be a special function (like in C).

> you just list it as a parameter yes, that's what I wanted to say. the fact is, in other languages I don't have to list it as a parameter and I don't have to get used to it. Personally, I find it annoying (but I could live with it, like everyone else).

> how does wrapping C functions with C names help me forgive my forward reference, but I will argue that "you're not the target audience" for this language. I found that PHP works best if you're coming from a C background and you treat it as a C-based scripting language (and this is kind of what PHP claims to be). I've always suspected that people coming from other backgrounds are the ones who will dislike PHP the most (and it doesn't surprise me), but I also don't know why they seem so vocal about it. I, for one, don't post negative comments about Ruby/Python (unless I'm comparing PHP with them) because I acknowledge that other people with, say, LISP backgrounds will find those more intuitive and will make efficient use of the them.

Anyways, cheers.


Strictly speaking, `main` isn't even a special function in C. It's only special to your C library and linker and something something I forget the details. Anyway it doesn't matter at all; you could leave off the `if` in Flask's example and never notice any difference. Just emerged as common practice to bracket off stuff like that /in case/ you ever decide to import your script as a module.

Part of my problem with PHP is that it doesn't seem to know what it is; there are the C parts, and the C++/Java parts, and both sides are sort of glued together with Perl influence, yet don't very well interoperate otherwise.

I have a pretty patchwork background and I could get work done in just about any language you'd pay me to use. I'd even happily write a web app in C. Doesn't make PHP any more attractive.


In implementation terms the main function isn't special, but its prototype is part of the C specification: http://groups.google.com/group/comp.std.c++/browse_thread/th...


> Personally, I _like_ PHP because, at the core, it is a collection of utility functions to write webapps quickly. Here's a small set of those function that I would like to see somewhere else (like in Python for example):

As another commenter has already posted, these are pretty trivial in Ruby and Python(and plenty of other languages).

> Want to check for errors and improve the quality of the code? Go ahead! Use lower level functions like fwrite, fread, etc;

fwrite/fread improve quality of code? As opposed to what?

> The icing on the cake is that a huge part of the built-in functions wrap and extend existing C functions: strpos, strlen, so the language rarely gets in your way.

I don't see how your conclusion follows from your premise. How does a language using strlen equate to not getting in my way?

> from my experience, people who bash a language rarely know what they're talking about and it usually just boils down to personal preferences like braces vs no-braces.

None of the languages are perfect. Though a lot of people bash languages without knowing their shit, it doesn't mean all criticism is invalid. PHP has warts - warts that go beyond personal preferences.


> these are pretty trivial in Ruby and Python I wasn't arguing that they're difficult, I was saying they're quick, dirty functions you can use to get something up quickly (the RAD way).

> fwrite/fread improve quality of code? As opposed to what? as opposed to just using file_put_contents() and never checking for the number of bytes actually written. The point I was trying to make is that you could use quick, high level functions (like file_put_contents()) and then replace them with more low-level functions like fopen()/fwrite()/fclose() to have more fine-grained checking of errors (you could complain that the script failed to open the file when calling fopen() and then you could print the exact reason why, as opposed to just printing 'could not write to file' when using file_put_contents).

> How does a language using strlen equate to not getting in my way? I switch between languages pretty often and so keeping an active context for each language in my mind is rather difficult. I have to recall if it's len(), str.len(), str.length, str.length() and so on for every other type of task I need to deal with. If the language is really similar to C, it is more natural for me to use strlen() even if I have to come back to it after 6 months. That's what I meant by 'not getting in my way'.

> it doesn't mean all criticism is invalid. PHP has warts - warts that go beyond personal preferences.

I agree. No language is perfect, but why do you feel compelled to say 'PHP has warts [...] that go beyond personal preferences' without actually bringing evidence. It makes it sound like you're speaking purely out of hatred for PHP and I can't respect that.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: