views:

1258

answers:

13

I am not really familiar with PHP, but I get the impression that it is like JavaScript (syntax-wise).

What are the benefits of a dynamically typed language, when compared to a strongly typed language like C# or Java, and how would this help in the context of web development? What would make a dynamically typed language so attractive? Or, does the popularity of PHP have more to do with it being free?

Okay, I think I better give a little more background to get more meaningful answers, because I am not wanting a flame war.

I come from a C background, and when I moved into C# and Visual Studio. Having code completion, integration with an SQL database, huge existing class libraries and easy to access documentation, as well as new tools such as LINQ and ReSharper was like heaven. I didn't enjoy JavaScript before JQuery, but now I love it as well. Recently, I ported a PHP project over to C# and I used Zend to help me debug and understand more while porting - instead of maintaining two code streams. That also cut down on the cost of the server and maintenance.

Getting into PHP would be nice. I think that Visual Studio has spoiled me - but again Eclipse is also equally spoiling.

It would be nice to have an answer from someone who has experience developing both under PHP and .NET.

+5  A: 

If find it very similar to Perl, but with a more conventional syntax for doing OO.

Brian Rasmussen
+2  A: 

PHP is not like javascript. PHP is serverside and javascript is clientside and that is a major difference.

Although I don't like to make huge projects in php it is most definately possible to do it. There's frameworks supporting seperation of code and design just as well as ASP.NET does.

Check out http://www.smarty.net/

If you use Eclipse you can have full code completion and if you use PHP 5.0 or later you can work with typestrong objects, public and private classes.

In general a quite structured language with good OO support, but also a language that can be used for scripting purposes.

facebook.com is php, so it can be used for bigger projects :)

The real napster
you can actually run javascript on the serverside, but I agree that they are not the same
fredrik
I am aware that javascript can run from the server but in general it is a clientside language. I have never seen a project with the code behind beeing made entirely with javascript.
The real napster
This does not really explain anything imo ...
OIS
Although there's controversy to whether smarty should be used for developers as it's supposedly slower. For example, systems like WP don't use it.
Kezzer
Okay.. I have just tried smarty out a few times.. I haven't had a real focus on performance since the project wasn't performance critical.
The real napster
Smarty is not required if you separate your logic and output with something like MVC.
OIS
+14  A: 

Free is nice, but code maintenance can be a chore if you (or your team) aren't designing their project properly, or writing maintainable code. With an MVC framework (I use Zend, personally) and some discipline with keeping code clean and organized neatly, it can be fine (dare I say, a pleasure even) to code in.

It takes some time to learn the nuances (like any language), and it is not the answer to every problem, but I feel PHP gets a lot of bad press from developers who have taken over unmaintainable messes, or created them themselves.

As far as typing goes, you only have to set conventions for yourself (File and variable naming, return values on functions), stick to those conventions, and comment your code. A good developer can code elegant solutions in any language.

Brett Bender
+1 for the last paragraph
Mats Fredriksson
+3  A: 

This question was so weird... why asking what PHP looks like? If you wanna know how PHP look and act you should try it first... then compare with your previous experience with other programming language (in your case, it was Javascript which was client-side programming language)

PHP is more like Perl, and it's syntax & writing style was like C/C++ (perl, javascript & java has some similar to C/C++ too).

If you didn't like with PHP it's ok, you can use JSP which was based on Java programming language or ASP.NET which you can code your web with C# or VB.NET. Or if you want some exercise you can use ASP Vbscript which syntax closely resemble basic (visual basic) language.

Dels
+2  A: 

In my experience, most php code you will encounter is tag soup.

You will start off with good intentions but eventually you will make a fix that is inline and then another, in the end the code will be a maintanence nightmare.

Also the lack of decent integration/debugging - I can't get into the Eclipse PDT mindset and I've been spoilt by VS2008 - really is a turnoff.

However, that said some of the largest sites are PHP based: Wordpress, phpBB, Facebook.

graham.reeds
Facebook source code is supposedly very neatly structured and ordered. It is up to the developers how they want to maintain their code a year from now.
OIS
PHP holds the position that was held by Perl CGI a decade ago. It is (probably) the easiest language to find cheap hosting for and it is easy to get up and running with. Result: most of the newbies pick it, and they write bad code and mix program logic with display logic, which puts a lot of such code "out there". I wouldn't blame PHP for bad PHP code, its a consequence of having a higher proportion of new programmers using it.
David Dorward
And actually, with the new ASP .NET MVC model, it's starting to look like PHP.
seanlinmt
Although my experience with ASP.NET MVC is limited, it doesn't look anything like PHP. Although you could probably code like it was PHP. You could probably code PHP like it was A.NM.
graham.reeds
+2  A: 

PHP is arguably the simplest way to get started if you need to have DB connectivity on you web project. That said, it's fabulously ugly, as a previous poster has suggested. However, I challenge someone to name an alternative that is any more elegant. JSP, ASP, Perl, it's all a big bunch of nonsense. You start out with something that looks decent and 5 edits later you have all your display logic intermixed with your business logic and it's all downhill from there. I would argue that the best way to develop is to pick a language (any language) in which to write code that ONLY delivers JSON objects to the client via AJAX (or if you have to use Flash/Flex, you can deliver XML on an open socket) but then handle ALL you display with javascript/actionscript and css and not mix the two unless someone is holding an anthrax coated gun to your nuts.

Dr.Dredel
+25  A: 

I am not really familiar with PHP but I get the impression that it is like javascript (syntax-wise).

It falls into the group of languages that take inspiration from C, but that is about as far as it goes with similarity to JavaScript (which is a prototype-based OO language while PHP is a classical OO language).

And I can't imagine doing a huge project with javascript.

I can. JavaScript is rather nice, once you get into the right mindset.

My main objections to PHP are the amount of stuff it puts into core (rather then organising things into optional namespaces) and the inconsistency it has with argument order and function naming conventions.

Wouldn't it be nicer using a strongly typed language like C# or java?

That is somewhat subjective. I've talked to people who love strong typing and people who hate it.

So I wonder why PHP is still so attractive?

  • Hosting is easy to find
  • Upload a script and go - very low barrier to entry
  • Lots of cheap PHP developers out there
David Dorward
+1 to "the inconsistency it has with argument order and function naming conventions" =/
Svish
Other benefits of PHP are: Reasonable performance. Has a class-model which is similar to Javas. Battle-hardened and somewhat stable. Has a torrent of extensions and libraries available.
troelskn
The number of cheap PHP developers shrinks somewhat if you start looking for *good* ones. Still, the argument certainly holds that it is comparably easy to find PHP developers.
Pekka
Cheap, good developers are rare for any language.
David Dorward
“So I wonder why PHP is still so attractive?” If we’re comparing programming languages to human languages (and if we’re not, well, I am), I reckon PHP is like English: a messy, unnecessarily complicated bastard tongue that’s winning hands down because it’s everywhere.
Paul D. Waite
+9  A: 

Some reasons why PHP is nice:

  • It's a server-side scripting language, so it's great for writing scripts that display, or work with web pages. Also, as a server-side language, it has access to information about the server that client-side languages do not have.
  • Built to work with databases. Built-in support for MySQL.
  • Does not need to be compiled.
  • Open source, free to use
  • Relatively easy language to pick up. Ease of entry for non-programmers.
  • Loosely typed, making it quick and easy to get something working
  • php.net - Makes learning, and finding answers, extremely easy
  • There are a wealth of scripts, already written, available for free, all over the internet
  • Huge development community. Lots of other people who know PHP, whom you can ask for help.
  • Frameworks available to speed up development (Zend Framework, CakePHP)

Some reasons why PHP is not so great:

  • It's only a scripting language. It's not meant for desktop applications. Only really useful for creating dynamic web pages. PHP is usually compared to ASP or Ruby, not C# or Java. They are in a whole different league.
  • Loosely typed. Sometimes a feature can come back to bite you if you are not careful.
  • Easy to hack together something that works. Again a feature that can result in ugly, inefficient code.

I think that PHP gives you the ability to get something working fast, easy, and cheaply, but doesn't hold your hand while you make mistakes. To be a good PHP developer, you must set personal policies rather than relying on the code to force you to be a good developer.

Andrew
Many good points here, nice answer.
Brett Bender
actually.... php can be used to develop desktop applications http://gtk.php.net/
Jeffrey04
+36  A: 

so, why is php so popular?

hacking

php is a typical "hacker" language - it's easy to get started on simple scripts and rapid hacks. it takes a lot of discipline to stay organised and clean when it comes to big projects, but it definitley is possible. that's because of phps heritage - it started as a very thin layer between the database and the webserver. often functions have incoherent naming conventions, because they once were wrappers for the native unix functions, mimicing their names. one example of this is array_merge versus sort (instead of array_sort).

another thing is the mixing of php code and html. on the one hand thats an advantage - for small, manageable scripts. it is important to separate those two in bigger projects. you can do that with templating (e.g. smarty) and discipline. some frameworks enforce this, so you're not tempted. a problem? only if you don't know better or have no discipline.

a lot of programmers have a low opinion of php and web programmers, and they are mostly right. html is easy (by design), everybody can do it. the next step after learning html (and a little bit of javascript hacking) is php - and php makes coding very easy; it's the beginners language. so there is a huge crowd of amateurs with no understanding of the underlying principles of programming, algorithms or even the http protocol, calling themselves "web developers" and hacking together websites that are badly designed, unreliable and insecure. the barrier of entry is just incredibly low, and there are virtually no costs (you don't need books, IDEs or other fancy stuff to start). php doesn't enforce anything on you - do as you like. great for trying, learning, playing!

php is popular and well liked - by the amateurs. that doesn't mean all experienced programmers dislike php (many do - even those who use it every day -, because php is just not elegant). it still has certain advantages ...

strict typing versus loose typing

loose typing is not a penalty per se. often, it can greatly increase your productivity, because you don't have to manage type conversion manually. especially in web developement, in the end the input and output are strings most of the time. in a typed language you would have to cast-to-string or cast-from-string permanently.

<?php $string1 = "44"; $string2 = "35.4"; echo $string1 + $string2; ?>

just works as expected (79.4) - at least through the eyes of a beginner (who doesn't understand the intricacy and pitfalls of types, loose typing and automated type conversion).

yes, it introduces the possibility for type errors; but very often they're handled gracefully or you can catch them with automated unit tests. a lot of (well known) programmers think that loose typing is not a problem for big projects if you (unit) test your code accordingly.

interpreted

no compilation cycles, whooo! code, test, deploy, code, test, deploy! rapid developement, rapid deployment!

hosting, lock-in and distribution

  1. it's easy and cheap to get php-enabled hosting.
  2. a lot of projects are already hosted with php, and often people don't want to change servers when updating or redesigning.
  3. other programming languages may be more elegant, more modern, simply better (but be careful, my language is better than your language is like smoking while bathing in a pool of gasoline). but as a customer, if you already got a website running on php it is often problematic to install an addon (e.g. a forum) in ruby, python or serverside javascript. even, if 60% of websites use php and 0.7% use, say, rebol as a backend language, and you want to sell your product ...

maintenance and lock-in

because there are so many sites in php, you need php programmers for updates and bug-fixes. additionally php programmers are cheap (pun yay!), because there is a huge market.

developer lock-in

if you work at a web design company that used php since the dawn of time (because the founder was a typical php-amateur herself) it's really a pain in the a$$ to convince anybody to switch. almost all of your collegues are typical cheap php amateurs. almost ALL of your (legacy) codebase is php. many bosses think it's too much of a risk switching to another language. even google allows only a small pool for server side developement.

steve yegge:

The thing is, Google's decision to limit the number of languages allowed for production work is actually pretty smart. It's one of those things that might sting for a few weeks after you start (and not to put too fine a point on it, but why aren't you sending me your resume?), and if you're a bumbling fool like me it might take you a little longer to figure it out. But programming languages have a core of standard functionality that everyone knows, followed by a long tail of murky semantics that become increasingly difficult to figure out, especially for dynamic languages with no actual spec like Perl, Python, Ruby and their ilk. Google very prudently keeps the number of languages as small as practical, so that we can build up large groups of experts on the semantics of the languages we've chosen.
It's also to cut down on the combinatorial explosion of components required for language interoperability -- a huge tax at other companies I've been at, a tax that Google has managed, by and large, to minimize.

libraries, code snippets & community

  1. every problem you encounter in web developement has already been solved by some other guy who posted the php code or mentioned it somewhere (very often in the php documentation comment section itself). copy and paste hacking, again.
  2. all the big tools and services you may use have connectors for php. there are 21 (!) supported database systems mentioned in the php doc)
  3. APIs: if someone designs an API, she most likely includes a tutorial how to use it with php.
  4. extensions: if you need something to be really performant (at least, faster as native php could be), you can always create an extension in c.

essentially, php has got everything you need to build (big) applications (with the exception of namespaces maybe, but those are coming in the next release).

documentation

the php documentation is great, period.

performance

lets face it: php is old (at least in terms of the internet). this brings disadvantages, like the before mentioned incoherent naming they just can't get rid of for compatibility reasons). on the other hand, the interpreter is well tested and optimized. php may not be as fast as other languages, but it is fast enough 99% of the time.

IDEs and editors

there are several useable IDEs available (eg. php-eclipse, zend studio, php-netbeans, ...) but you can also use simple text editors without much hassle. when was the last time you used notepad to quickly fix something in java?

compatibility and developement

  • the php5 interpreter is (mostly) compatible with php4, so you can run all the old legacy apps and even mix it with new code.
  • php gets frequently updated, new features are added constantly. the SPL, for example, tries to offer more experienced programmers an alternative to the legacy naming problems by reimplementing lots of the old, messy stuff in a clean new object oriented manner. hey, even first class functions and namespaces in 5.3! this language is pretty alive and evolving.


javascript and php

they don't have much in common, other than being interpreted, loosely typed languages primarily used in web developement. together with html and css (and maybe mysql) they form the trinity of web developement*. i don't have numbers to prove it at hand, but i think it's still true.
javascript is most often used in the browser, but is also available as a serverside scripting language (mozilla rhino, experimental google mod_v8).
is more elegant and modern, supports first level- and anonymus functions and has a different object oriented design paradigma (prototyping). javascript is fast, because the browser developers have a my-peni..., i mean, my-interpreter-is-better-than-your-interpreter argument at the moment, with JITting to native machine code and everything.

(* i know, there are perl, asp, python, jsp and (runner-up) ruby (because of ror). does anyone have numbers?)


i use php on a daily basis for more than 10 years (almost half of my life!). i had (mostly superficially) contact with lots of languages, including assembler, c, server-side javascript, java, haskell, prolog, ruby, python and countless more (but not perl, thankfully!), so i know a bit of the world outside of web developement. and i wouldn't mind leaving PHP for a more elegant and modern language like ruby or ss-javascript ...

but it's just not possible (ok, just very, very hard!). it's everywhere! and even if my boss allowed another language, i'm most fluent in php; so for really critical and complicated apps, i'd probably come back to PHP anyway.


summary

essentially, the reasons ordered by percieved importance are ...

  1. ease of use, easy learning, low barrier of entry
  2. size of the community and market, including lock-in
  3. ease of deployment (hosting)
  4. it's good enough, so why change?
Schnalle
Wow, thanks for the long and excellent answer. It's going to be benefecial to a lot of people. I'm actually an electrical engineer by training and I hated programming in varsity but I'm addicted to it now. I probably only have aboout 6 years of programming experience and I started out with C and assembly on notepad. But yeah, what you said makes sense. Programming languages are just tools anyway.
seanlinmt
Talking about performance, it wouldn't really matter what langauge you're using wouldn't it? Because to get the performance, everyone will just be caching as much as they can.
seanlinmt
yes, performance doesn't matter that much - it's not about being fast, it's about being fast ENOUGH. caching may help performance, but it's no magic wand - it increases complexity and it's not available all the time. additionally, php is scalable because of the nothing-shared architecture; just adding hardware can solve a lot of problems. and yeah, programming is a lot of fun because you solve problems and actually *create* something.
Schnalle
+2  A: 

One of the reasons for PHP's popularity on the Web is that it allows you to just get things done.

A lot of (most?) web "applications" are trivial.

Pull 5 fields from a database, and print out the content in an HTML template. About 10 lines for the database query, and a few tags like <?= $title ?> in the template to echo the content at the right spot. Done.

Quick, no nonsense, tons of ready-made functions to manipulate strings, extremely flexible arrays that never bug you about what you store inside. Most everything in a simple web application is meant for output on the page, so it will usually end up as a string anyway.

PHP does what it was made for and it does it pretty fast.

OO support isn't bad and using xdebug, you can debug a PHP application with the main features you expect from a debugger (step in, step over, breakpoints, variable content access...), so you can build something quite complex, as evidenced by the fact that some of the top sites are run on PHP.

All and all, PHP is pretty neat and you just got to love the documentation (mandatory).

Sylverdrag
I'm not sure I buy that argument. What's the analogy to an automobile? It's fine to get a car that only turns left because given 3 extra turns, each time you want to go right, you'll eventually get where you're going? It seems the argument you're making is that because web apps are simple the tool doesn't have to be well designed?I've seen a lot of PhP. Either 99% of PhP coders are REALLY bad, or the tool is just not designed to be elegant. I'm leaning towards the latter.
Dr.Dredel
I didn't make an analogy with an automobile, but if I take your analogy, PHP doesn't take 3 extra turns, quite the contrary. In fact, it will even let you go by red lights and take 1-way streets in the wrong direction. That can be dangerous (not necessarily), but is often faster, and well, just because you can cross a red light doesn't mean you have to. PHP is a great language to get started which means a lot of the code is beginner's code, but you can also write good code in PHP.
Sylverdrag
A: 

PHP and JS have similar syntax, but JS is much looser (semi-colons optional!). The reason I like PHP's type insensitivity is that it lets me program without having to think to hard about what a function is returning, or am I storing a double into a float. As others have said, the function naming is terrible in PHP which means that half the time you are looking through the PHP manual trying to find out if a function is called strip_slashes or stripslashes. The last big difference between the two is that PHP is widely supported. You can find free hosting for PHP sites almost everywhere. Certain libraries are made or supported only for PHP.

Samuel
but what about when you're debugging your code? The type uncertainty just seemed like another unknown to factor in. But in saying that C# now has anonymous types and you could just use "var" as the type, but only local scoped. But I guess it can be a good thing sometimes, it like js, don't really have to worry about the type.
seanlinmt
A: 

I was a PHP developer for 8 years, since PHP 3.x till 5.2.

In my oppinion, PHP is a very bad language:

  • It has many design flaws.
  • Its "official" implementation relatively slow (compared to e.g. Java, Python, C#).
    • By the way - it's Java implementation is claimed to be 4 times faster.
  • Its developement has no firm plans, the syntax and constructs are added ad-hoc. See the recent discussion about what will be added or not.
  • The unicode support is being developed for several years and still not complete.
  • Stack overflow makes httpd die without any log message.

And that's only about the language. If you asked me about other things like overall concept of how it serves web, works with memory, handles errors/exceptions, treats references, ... I'd add much more flaws. I've summed it at PHP advantages and disadvantages (Google-translated & ugly, but you can make an image.)

Ondra Žižka
+1  A: 

I'm a PHP "web developer"...for what it's worth - and here's my take on the subject. Other languages may be better - but do the job markets reflect that? If I search on Jobsite in the UK for Ruby/Python, I get a fraction of the volume of opportunities compared to a search for PHP. Similairly, PHP hosting is ten a penny. Java jobs tend to be more "heavyweight".

I agree, PHP is a "dirty" language - but then so is English, a real mongrel language cobbled together from bits and bobs borrowed from our neighbours. As time passes, even slang passes from "the streets" into the dictionary to the horror of academics. This is a bit of a VHS/Betamax (or X360 vs PS3) type question. On paper, the latter in each example is the better hardware - but the market favoured the former.

It's one thing to ascertain the merits of a language, but it's another entirely to base your career choices on those merits.

case in point: http://www.langpop.com/

sunwukung