4 ms·
I think this shows a remarkable misunderstanding of what PHP users actually like about PHP: * We like semicolons. The culture who thinks saving this one charac
by WilliamLP 16y ago
I think this shows a remarkable misunderstanding of what PHP users actually like about PHP:
* We like semicolons. The culture who thinks saving this one character is valuable tends toward a non-pragmatic purity that is completely missing the point of PHP. We like to know when the line ends and to break up lines easily.
* The $ before all variables is a feature, we like this too. It's jarring coming from other languages but we think it adds readability.
* Inline SQL with no special syntax to denote that it is special? This seems way too far off the deep end of preventing an extra character at all costs. Give me a key word or a syntax if you like but don't make it look just like a variable, please.
* No from/to string auto-conversion. We like this too. Having the sense to not overload "+" for strings goes a long way here.
1 . 1 == "1" . 1 == "1" . "1" == 11 == "11"
1 + 1 == "1" + 1 == "1" + "1" == 2 == "2"
Yes there are warts and some paradoxes but we deal with it, and we have to be very conscious of what data could be provided by users, but our web apps work fine provided we know the language. (Yes, there are very many PHP programmers who don't.)
- goodmitton 16y agothe $ is perhaps my favorite feature. I love being able to easily scan the code which parts are variables, and which parts are strings. This comes in really handy when your page has php, js, and html hacked together because you can more easily tell what language the code you are reading is in.
- jacquesm 16y agoI'd like to add 'we like braces'. Braces are explicit, and guard against dropped lines.
- mcantelon 16y agoAny decent editor can use color coding to contextualize variables and such: no need for syntax cruft. I'm a longtime PHP user and much prefer working in languages with less visual noise.
- KrisJordan 16y agoRe: "The $ before all variables is a feature [...] but we think it adds readability." The argument for the $ sign is not readability. If it were you'd see JavaScripter's prefixing all of their variables with a dollar sign, too. var $foo = 'bar'; is completely valid JavaScript. (Aside: $ prefix is handy in JS when holding references to jQuery objects ie $container = $('#container');) The argument is for simplified string manipulation with magic quotes and is an homage to unix shell scripting. Without the $ prefix string manipulation would be reserved to concatenation/printf/replacement/etc. With the $prefix you can do things like: $base = __DIR__; $user = (object)array('name'=>'Foo','dir'=>"foo"); $size = "lg"; $image = "$base/users/$user->dir/images/$size.jpg"; vs: image = base + "/users/" + user.dir + "/images/" + size + ".jpg" When doing a lot of string manipulation and scripting the $ is quite handy. The dollar sign also enables variable variables and dynamic property access. i.e. $foo = 'Hello, world.'; $bar = 'foo'; echo ${$bar}; // 'Hello, world.' - and - $user = (object)array('name'=>'Foo','dir'=>"foo"); $prop = 'name'; echo $user->{$prop}; // 'Foo' The dollar sign for variable variables is weak because variable variables is usually an indication of code gone awry. Dynamic property access, on the other hand, is much more useful in a dynamic language like PHP/JavaScript. That said, I believe JavaScript's solution is much more elegant. i.e.: var user = {name: "Foo", dir: "foo"}; var prop = "name"; alert(user[prop]); // 'Foo' By overloading the index operator on objects to accept a string that dereferences to an object's property you get the same benefit in a much more elegant way. Thus the $'s primary justification is magic strings and string manipulation.
- wvenable 16y agoThe $ also completely separates variables from the other namespaces, like reserved words. You can have variables named $if or $while and not worry about conflicts now or in the future.
- WilliamLP 16y agoYes, all that, plus readability. I know when something is a variable. Prefixing variables in JS with $ doesn't do this because I still have to look out for things without a $ that are variables as well so it's the worst of all worlds. > (Aside: $ prefix is handy in JS when holding references to jQuery objects ie $container = $('#container');) I really really don't want to see that in a project I'm in charge of. It looks terrible to me, and I really don't like $ as an ordinary alphanumeric character. I like $ in jQuery projects to only be used for jQuery calls. (If it doesn't have to co-exist with something else and use noConflict mode.) > var user = {name: "Foo", dir: "foo"}; I hate that about Javascript, how in object literals the left side is a literal string and the right side is an expression. Except, I really should quote the left side anyway - I think Crockford recommends this - because I might want a hyphen in there, or I might want to use a keyword (like "interface" where it isn't even obvious that it actually is a keyword.) Also, if I have var key, and var value, I want to be able to pass it into a function that takes a dictionary argument as {key: value}, and not have to do var dict={}; dict[key]=value; I also think Javascript absolutely begs for a real dictionary type. Objects don't work so well because I have to worry about someone extending them and I have to use hasOwnProperty - never mind someone could override hasOwnProperty. Also the performance characteristics of JS objects are terrible - Did you know that in Firefox a switch statement is one hundred times faster than an object lookup? PHP's dual purpose variables are ugly as hell (with object -> syntax and array [] syntax) but I actually like being clear on whether I'm using it as an object or a simple array/dictionary.
- cookiecaper 16y agoI really don't like the $ prefix. The rest of your comment is OK.