News for hackers. Stuff that matters.
Re:What I Like About PHP by xyby on Tuesday December 09, 2014 @07:51PM (#8726946)
I'm a programming language geek, a code lover and a syntax fanatic. And there are a ton of things I love about php:

    class Animal // yeah! real classes!
    {
      function hello($name="nobody") // nice default variables
      {
       echo "hi $name"; // sexy variable expansion!
      }
    }

    class Dog extends Animal // logical syntax :)
    {
     ...
    }

    function f()
    {
     $x=123; // variables are local by default and need no initialisation
    }
I could go on for a long time, listing all the language constructs I like in PHP. All other languages I tried have quirks that annoy me.
Re:What I Like About PHP by chc on Tuesday December 09, 2014 @08:12PM (#8727055)
Remarking on the fact that variables are local by default makes me wonder exactly how many programming languages you're familiar with. That's an extremely low bar, not really something to love.
Re:What I Like About PHP by xyby on Tuesday December 09, 2014 @08:21PM (#8727106)
Low bar or not, if you do anything web-facing, there is no way around javascript. And you will do a lot of stuff in a language that makes variables global by default.
Re:What I Like About PHP by Shish2k on Tuesday December 09, 2014 @09:40PM (#8727391)
A car metaphor for this conversation:

"I love my skoda! It has four wheels, and a roof, and a passenger seat!"

"That's a pretty low bar for things to love about a car. Pretty much all other cars also have those features, and they do them better."

"Yeah, well I also ride a bicycle, and my bicycle doesn't have those features."

Re:What I Like About PHP by arenaninja on Tuesday December 09, 2014 @08:19PM (#8727101)
Conversely, there's a long list of languages where variables would not be local by default
Re:What I Like About PHP by chc on Tuesday December 09, 2014 @08:53PM (#8727235)
Insofar as there is a ridiculously long list of languages nobody has ever heard of, I guess this is true. But it's not true if you're comparing it to languages that are actually used in modern programming. Even if you're considering languages as obscure as, say, Standard ML, you'll still not find many that have a default global scope for variables inside functions. There's pretty much JavaScript, Perl, Fortran and Cobol. I can't think of any others that are even worth mentioning nowadays.
Re:What I Like About PHP by srgpqt on Tuesday December 09, 2014 @10:58PM (#8727607)
Lua.

At least there is Moonscript to save it...

Re:What I Like About PHP by tiglionabbit on Tuesday December 09, 2014 @08:20PM (#8727103)
I'm a programming language geek, a code lover and a syntax fanatic. And there are a ton of things I love about coffee-script:

    class Animal # yeah! real classes!
      hello: (name="nobody") -> # nice default variables
       console.log "hi #{name}" # sexy variable expansion!

    class Dog extends Animal # logical syntax :)
     ...

    f = ->
     x=123 # variables are local by default and need no initialisation
Re:What I Like About PHP by xyby on Tuesday December 09, 2014 @08:31PM (#8727153)
CoffeeScript is interesting. So far I did not experiment with it too much. So I'm not sure if I would like to do work in it.

On one hand, it looks nicely terse. On the other hand it looks a bit cryptic.

The thing that stops me from trying it for a project is not the language, but the available environments. Having an additional compliation step adds complexity to the system and the workflow.

One thing I cannot resist to say is that to me "hello $name" is sexier then "hello #{name}" :)

Re:What I Like About PHP by tiglionabbit on Tuesday December 09, 2014 @08:53PM (#8727234)
Well sure, but you can put arbitrary expressions in the #{} syntax. PHP has a similar syntax for that.

I just run `coffee -wc .` on my source directory and pretend I'm using JavaScript everywhere else.

Re:What I Like About PHP by califield on Tuesday December 09, 2014 @08:06PM (#8727025)
`f` in your example is a global function that cannot be redefined.
Re:What I Like About PHP by imaginenore on Tuesday December 09, 2014 @08:25PM (#8727124)
Don't like global functions? Don't use them.

It's really that simple.

Re:What I Like About PHP by xyby on Tuesday December 09, 2014 @08:15PM (#8727078)
Which is a good thing. When you have ...

    function f($x) { return $x*$x; }
...in your code, you know that you can always call f() to get the square of something. Hallelujah. Functions as they are meant to be.
Re:What I Like About PHP by k__ on Tuesday December 09, 2014 @08:06PM (#8727020)
You're joking, right?!
Re:What I Like About PHP by Shish2k on Tuesday December 09, 2014 @10:23PM (#8727516)
> nice default variables

If you think that's nice, I'm about to show you something which will BLOW YOUR MIND :D

    >>> def hello(name="anybody", age=10):
    ...     print "hello %(name)s, aged %(age)d" % locals()
    ...
    >>> hello(age=42)
    "hello anybody, aged 42"
But wait, there's more!

> yeah! real classes!

You think PHP's classes are real? Just wait until shit gets REAL real when you subclass a primitive!

    >>> class ExtremeInt(int):
    ...     def __new__(cls, val):
    ...         return super(ExtremeInt, cls).__new__(cls, val)
    ...     def __add__(self, other):
    ...         return (self * other)  # Multiplication is EXTREME! addition
    ... 
    >>> 
    >>> x = ExtremeInt(42)
    >>> print x + 2
    84
Dare I stop there? I dare not!

> sexy variable expansion!

You think formatting strings based on local variables is sexy? Sure, we can do that:

    >>> name = "bob"
    >>> print "hello %(name)s" % locals()
    hello bob
But why not format a floating point number to a set number of decimal places based on a dictionary?

    >>> things_to_template = {"pi": 3.14159}
    >>> print "My pi is %(pi).2f" % things_to_template
    My pi is 3.14
What about formatting based on an object's attributes?

    >>> class Animal():  # HOLY SHIT REAL CLASSES!!!!
    ...     def __init__(self, name):
    ...         self.name = name
    ... 
    >>> my_ferret = Animal("Mr Awesome")
    >>> print "My ferret's name is %(name)s" % my_ferret.__dict__
    My ferret's name is Mr Awesome
Is that the end? It's 4am here and I'm barely conscious, but no!

> logical syntax [for single inheritance] :)

Ready to have your mind blown FOR A SECOND TIME?!?!?

    >>> class Animal:  # SUCH CLASS.
    ...     def __init__(self, spec):
    ...         self.spec = spec
    ... 
    >>> class Awesome:  # SO REAL. WOW.
    ...     def __init__(self, awesomeness):
    ...         self.awesomeness = awesomeness
    ...
    >>> class Ferret(Animal, Awesome):  # Logical syntax for MULTIPLE INHERITANCE OMGOMGOMG
    ...     def __init__(self, awesomeness):                                        
    ...         Animal.__init__(self, "Ferret")                                     
    ...         Awesome.__init__(self, awesomeness)                                 
    ... 
    >>> mr_awesome = Ferret(9001)
    >>> print "Mr Awesome is a %(spec)s and his awesomeness is %(awesomeness)s" % mr_awesome.__dict__
    Mr Awesome is a Ferret and his awesomeness is 9001
And that final part?

> Variables are local by default

...

...

Actually I can't think of a way to improve on that. It's like saying "I love this language for being turing complete". Sure, that's a good thing. The best thing, perhaps. But you sound kind of weird when you list it as a particular high-point of a language...