|
This is NOT TRUE when we're talking about array lookups inside a double quoted string. See here[^]: the example includes an unquoted string indexer. PHP won't try to resolve recid in the context of a double quoted string.
You can use the braced syntax but it is unnecessary complexity to do so when there is a well defined shortcut.
|
|
|
|
|
That's stupid, how would you go about using a defined constant as an array index within a double quoted string then? You would have to resort to the curly braces. This means there is a double standard, one when you are outside a double quoted string and one when you are inside. (My point here is not that PHP is a bad language it is that "quirks" such as this shouldn't be seen as a feature of a language)
If that is true I would argue its not a well defined shortcut if you have to apply the rule "well it works in that situation but not in this situation"
However, I still stand by my statement that it is not a good idea under any circumstance to tell people to use $array[index] as opposed to $array['index'] even in the case where it may work without generating a E_NOTICE error, the chance of the user misunderstanding and using it in other places simply because it works is too high.
|
|
|
|
|
ItsTobias wrote: This means there is a double standard, one when you are outside a double quoted string and one when you are inside.
Well, uh, yeah. Different contexts have different rules in every language. That's why you can write code inside a method but not in a class in C#, for example, or why you can't have 'local methods' in Java: because class, method, double-quoted-string etc are different contexts and different rules apply.
However, I still stand by my statement that it is not a good idea under any circumstance to tell people to use [context dependent syntax], the chance of the user misunderstanding and using it in other places simply because it works is too high.
I generalised your quote to indicate what you're actually saying. If the context isn't clear (for example if you can use something inside a loop but not inside an if block, or something like that) then sure, but when the context rule is simple then I don't agree. Learning the rules of each context is part of learning a language.
|
|
|
|
|
I agree learning rules about when you can and cant do certain things in a programming language is important and therefore there are context dependent rules for every language that must be learned from an early point. However I do not feel that (in this situation) within a double quoted string is enough of a context switch to cause the user to feel like they are now applying a different rule set.
This is especially true in this specific case because PHP will let you do the wrong thing(use un-qouted array indexes outside double quoted strings), other languages, if you apply the context rules in the wrong place you will likely get errors thrown at you and your code will not run (be it a compiled or scripted language) however in PHP in this specific situation(context) the PHP engine will "fix" your incorrect code, almost silently (How silent it is about this depends on your systems error display level), since this is a scripted language PHP has to fix your code every time. Since the user is none the wiser about their error, they will keep doing it that way in the future because "it just works", and then you end up with questions like "This works on my dev box but when I upload it to my live machine it throws errors all over the place!!!! this is why I hate PHP!!!!!"
The simpler your rule set, the easier it is to teach and to remember, I would (and am) strongly argue that teaching the use of $array[index] in any context should not be done and that the use of {} to wrap variables is a far more robust system, but I cant stop you, I can only comment on why I think teaching its use is bad in any situation.
I will not argue whether PHP is a good or a bad language to write websites in. It has its flaws(some which are quite major) but it has its positives to, like pretty much every programming language, I think its a great pick up and code language, kind of the VB of web languages
|
|
|
|
|
|
BobJanova wrote: Or you can use "... $s[recid] ... etc" (note no single quotes in the array indexer).
Don't omit quotes.
php helpfully thinks a word without $ or quotes is a constant with the string value of the word.
Until somebody defines the constant...
Imagine you did $arr[version] instead of $arr['version']. It works until somebody
define('version','3.1415927Beta');
Leif
|
|
|
|
|
Did you read the subthread? I already linked to the manual page about double quoted strings (the context we're talking about here) where that's not true.
|
|
|
|
|
W∴ Balboos wrote: (who NEVER eats bacon)
Burn the heretic!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
OriginalGriff wrote: Burn the heretic!
Crispy, I would suppose. (Sigh)
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "As far as we know, our computer has never had an undetected error." - Weisert | "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
|
This solution was posted a bit ago.
http://www.codeproject.com/Feature/WeirdAndWonderful.aspx?fid=392254&select=4324500&fr=1#xx0xx[^] - This user, however, put the $ outside of the curly braces.
I actually end up doing what you could describe as the long-hand of the two options I suggested: reassigning the variables to non-array. There's method to this madness, though, as the data I usually work with is coming from a database and some amount of preprocessing is often necessary before it's ready for display.
The curly braces method, however, I'll keep in mind for there's places where readability would be improved.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "As far as we know, our computer has never had an undetected error." - Weisert | "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
I had an opportunity to check this out, and of course, it worked.
+5
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "As far as we know, our computer has never had an undetected error." - Weisert | "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
<blockquote class="FQ"><div class="FQA">Andrei Straut wrote:</div>Full-fledged Java/.NET lover, full-fledged PHP hater.</blockquote>
You must separate things, try using Yii framework and see about good pratices on PHP.
PHP is very powerful depends on the way you use.
http://www.yiiframework.com/[^]
|
|
|
|
|
I don't like PHP either, but this looks like a simple matter of operator precedence to me.
The precedence of operators is well defined in PHP. +/-/. have higher precedence than ?:, so the outcome is IMO reasonable, and in no way weird.
Consider this: x == 2 ? 14 : 2 * 7. Would you expect "* 7" to be evaluated after the ?: in this case?
Some people recommend redundant parentheses precisely for avoiding such situations - it's not just a matter of correctness, it's also a matter of expressiveness - the variant without parantheses is potentially misleading, even when it's intended.
|
|
|
|
|
BobJanova wrote:
The PHP weirdness is that the ternary is a weak binding.
And it's left associative. Which means:
<?php
$i = 2;
echo ($i <= 3) ? "Three or less" :
($i > 3) ? "Over 3" :
"Whaaaat?";
outputs "Over 3" because it is interpreted as:
<?php
$i = 2;
echo (($i <= 3) ? "Less than three" : ($i > 3)) ?
"Over 3" :
"Whaaaat?";
|
|
|
|
|
Might work if the brackets are in the right place
(intval($header->getPlacedTimestamp() != 0)) ? 'Yes' : 'No';
should be
(intval($header->getPlacedTimestamp()) != 0) ? 'Yes' : 'No';
This is why I hate PHP sometimes. How the original code doesn't at least generate a warning is beyond me...
Need a mobile website? http://mobidoo.com.au
|
|
|
|
|
6 words you are dumb, sorry
|
|
|
|
|
Just reading through some javascript the other day when I found this gem.
function GetCursorPosition(node)
{
if (navigator.userAgent.toLowerCase().indexOf("opera") != -1) return 0;
Would now be a bad time to say that I found it here at CP?
+5 to the first person to correctly identify where!
|
|
|
|
|
enhzflep wrote: found it here at CP
Well, someone doesn't like Opera!
I wonder when this will be solved.
(Or if)
Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions.
Dave Barry
Read more at BrainyQuote[ ^]
|
|
|
|
|
Are you talking about this[^]?
|
|
|
|
|
Nope, sorry.
hint: its neither an article nor a user submitted post...
|
|
|
|
|
YOu found it right here[^]
At least artificial intelligence already is superior to natural stupidity
|
|
|
|
|
CDP1802 wrote: I found it right here[^]
I don't know where you found it
Nope, ftfy too.
You've both had it in your browsers today...
|
|
|
|
|
enhzflep wrote: You've both had it in your browsers today...
Then it must be on the 'Reply to Message' page.
...
Yes, I found it.
|
|
|
|
|
Yep, tracks cursor position inside the textarea where i'm writing now (hm, i'm feeling watched). At first glance, seems to be responsible for checking if cursor position is inside a tag when copying and pasting code. I may be wrong, but this is what I got in 1 min or so
EDIT: Now, thanks to Jochen I actually took his suggestion, so the 5 should go to him
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
|
|
|
|