News for hackers. Stuff that matters.
Re:Learn PHP – Free interactive PHP tutorial by leokun on Tuesday November 26, 2013 @08:48AM (#6801129)
> how to avoid what PHP has been infamous for : spaghetti code

Spaghetti code is not what PHP is infamous for. Spaghetti code can be written in any language. PHP is infamous for all library functions being in the global scope, lack of sane naming conventions for those things, terrible type conversions, unsafe libraries, starting php with every request, being able to drop raw php right into html templates - what it was initially designed for, and being just a general web programming language not much useful in any other context. It's also got a bad wrap for being the language of choice for people who do not wish to learn any other language or anything about computer science or computers.

PHP is what happened when people learned HTML and saw that once they got to forms they needed something else.

Re:Learn PHP – Free interactive PHP tutorial by meritt on Tuesday November 26, 2013 @09:34AM (#6801425)
> all library functions being in the global scope

Ever used C?

> lack of sane naming conventions for those things

Absolutely annoying but also fairly minimal. Mostly an issue in the string & array functions.

> terrible type conversions

Weak typing has to coerce somehow. Javascript is equally "terrible" at times.

> unsafe libraries

Such as? All the major libraries are thread-safe that I'm aware of

> starting php with every request

PHP exists outside of the web, and even in the web environment, you can run it as fastcgi since inception as a persistent daemon and php-fpm is the standard daemon now as well.

> being able to drop raw php right into html templates

I view that as a feature (which is often abused by beginners)

Re:Learn PHP – Free interactive PHP tutorial by mavroprovato on Tuesday November 26, 2013 @10:28AM (#6801829)
> > all library functions being in the global scope

> Ever used C?

Come on, this is not a good argument. C is a 70's language, we can do better than that, even in 1995 when PHP came out.

Re:Learn PHP – Free interactive PHP tutorial by leokun on Tuesday November 26, 2013 @11:06AM (#6802104)
By unsafe I meant insecure. Also with C you have to import headers.
Re:Learn PHP – Free interactive PHP tutorial by riquito on Tuesday November 26, 2013 @11:06AM (#6802108)
> Weak typing has to coerce somehow. Javascript is equally "terrible" at times.

>>> "0" == false true !!!!!!!

>>> "" == false true

>>> "" == "0" false

and here we lost the transitive relation...

Re:Learn PHP – Free interactive PHP tutorial by rmrfrmrf on Tuesday November 26, 2013 @11:59AM (#6802502)
Good. We didn't want it anyway!
Re:Learn PHP – Free interactive PHP tutorial by neovive on Tuesday November 26, 2013 @09:26AM (#6801379)
I think frameworks such as Laravel and Symphony go a long way towards addressing a lot of these concerns. Although things such as global functions and naming persist, PHP offers easy deployment for new developers and is available on almost all low-cost web hosts that is a major benefit and a must-have for less experienced developers.

Depending on your project, if most of your code lives in the front-end, using Laravel/PHP for an API back-end is a nice option these days. It all depends on what you are trying to accomplish and your available knowledge and resources.

Re:Learn PHP – Free interactive PHP tutorial by jmspring on Tuesday November 26, 2013 @09:53AM (#6801596)
A bad programmer in any language will write poor code.

I've seen just as many good code bases as bad in pho.

Re:Learn PHP – Free interactive PHP tutorial by username42 on Tuesday November 26, 2013 @11:58AM (#6802499)
One cause of the spaghetti code was the mixing of business logic and of presentation. The separation of concern was easier in smarter languages (like ruby or scala) than in php. Nowadays, templating and presentation is mostly performed on the client side and the server handles only the business. As a result, there is less difference between ruby and php. Nevertheless, the event style of javascript (scala ?) provides better performances.
Re:Learn PHP – Free interactive PHP tutorial by ronreiter on Tuesday November 26, 2013 @09:18AM (#6801330)
If PHP would enforce MVC by not being part of an HTML file, it would have been a very nice language (minus the $ signs everywhere)...
Re:Learn PHP – Free interactive PHP tutorial by homersapien on Tuesday November 26, 2013 @09:26AM (#6801374)
Why should PHP "force" anything? That's one of PHP's strengths: the ability to be everything from a simple dynamic webpage to a full blown app. That's like saying "I wish someone would make a hammer that only strikes nails, because I'm a poor craftsman and keep hitting my thumb."

Love it or hate it, PHP will be around long after elitist devs turn their guns on noSQL and server side JS.

Re:Learn PHP – Free interactive PHP tutorial by neovive on Tuesday November 26, 2013 @09:36AM (#6801451)
I agree. At it's PHP is just a template language. There are many great PHP MVC framworks that have evolved over the years to incorporate many "best practices". The choice should be up to the developer depending on the project.
Re:Learn PHP – Free interactive PHP tutorial by jtreminio on Tuesday November 26, 2013 @09:45AM (#6801535)
I'm assuming you meant "At its core, PHP is just a template language." That's a ridiculous, extremely over-generalized assertion.

I don't know of many "template languages" that treat classes as first-class citizens, has an in-depth, thorough unit testing framework, traits, closures, and and and.

I'm sure you get my idea? PHP may have been much simpler at its inception - it is not the same beast now.

Re:Learn PHP – Free interactive PHP tutorial by Killswitch on Tuesday November 26, 2013 @10:06AM (#6801685)
ITT: People who haven't used PHP since 4.2 think they know what the language is like and base all their opinions on a language version over 10 years old.

I hate when PHP pops up here on HN, 90% of the people flapping their gabs don't or haven't used PHP for many many years, and when they did it was some Indian's crap code so they spend their time talking shit about a language because they had to fix a bug in Hinderbar's PHP spaghetti from 2003.

Re:Learn PHP – Free interactive PHP tutorial by jon_r on Tuesday November 26, 2013 @05:09PM (#6804615)
If there's one thing I've learned in my years of software development it's that software is the great equaliser.

Everyone is capable of writing crappy spaghetti code.

Re:Learn PHP – Free interactive PHP tutorial by anu_gupta on Tuesday November 26, 2013 @11:29AM (#6802289)
Yeah, always those damn Indians and their crap code. Every other country's PHP developers write amazingly elegant PHP - it's just those damn Indians.
Re:Learn PHP – Free interactive PHP tutorial by leokun on Tuesday November 26, 2013 @09:39PM (#6805922)
You didn't really have to add the racism to make that point.
Re:Learn PHP – Free interactive PHP tutorial by captainmuon on Tuesday November 26, 2013 @09:38AM (#6801470)
Actually the ability to mix PHP and "HTML" is the only reason I still use PHP occasionally. Sometimes, I have a plain-text file, and would like to use variables or simple loops in it. The best way to do this is PHP!

For example, say I want to plot a couple of data columns with gnuplot (an open source plotting program) into several images. I can write a PHP script "myplot.plt.php" that generates a gnuplot script (untested):

    set terminal png enhanced
    <?php
    for ($i = 2; $i <= 4; $i++) {
        $outputfile = "plot$i.png"
        echo <<EOT
        set output "$outputfile"
        plot "data.txt" u 1:$i
    EOT;
    }
    ?>
and then invoke it from gnuplot with

    load "< php myplot.plt.php"
This is a stupid example, but it gets really powerful when you dynamically calculate labels, element positions, etc.. PHP may be a crappy language for web development, but it is a really nice macro language, and has really powerful string processing features, making it ideal for this kind of hack.
Re:Learn PHP – Free interactive PHP tutorial by easy_rider on Tuesday November 26, 2013 @09:31AM (#6801406)
That's so funny. When I look at $'s in PHP, it feels so natural. Then when I see someone use $ in variable names in JS, it gets me agitated.
Re:Learn PHP – Free interactive PHP tutorial by ceejayoz on Tuesday November 26, 2013 @11:59AM (#6802513)
I typically reserve $ in JS variable names for a version of something that's jQuery-ified. i.e. $(this) = $this;
Re:Learn PHP – Free interactive PHP tutorial by georgehotelling on Tuesday November 26, 2013 @09:30AM (#6801403)
While not enforced at the language level, PSR-1 states that "Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both."

Basically it stipulates that you should keep classes outside of the main page loader to be compliant.

Re:Learn PHP – Free interactive PHP tutorial by splawn on Tuesday November 26, 2013 @09:39AM (#6801484)
> It's also got a bad wrap for being the language of choice for people who do not wish to learn any other language or anything about computer science or computers.

That explains why its infamous for spaghetti code.