Simpler way to check if variable is not equal to multiple string values?

For your first code, you can use a short alteration of the answer given by @ShankarDamodaran using in_array(): if ( !in_array($some_variable, array(‘uk’,’in’), true ) ) { or even shorter with [] notation available since php 5.4 as pointed out by @Forty in the comments if ( !in_array($some_variable, [‘uk’,’in’], true ) ) { is the same … Read more

pandas : update value if condition in 3 columns are met

Using: df[ (df.A==’blue’) & (df.B==’red’) & (df.C==’square’) ][‘D’] = ‘succeed’ gives the warning: /usr/local/lib/python2.7/dist-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead A better way of achieving this seems to be: df.loc[(df[‘A’] == ‘blue’) & (df[‘B’] == ‘red’) & (df[‘C’] … Read more

How do I test if a variable does not equal either of two values?

Think of ! (negation operator) as “not”, || (boolean-or operator) as “or” and && (boolean-and operator) as “and”. See Operators and Operator Precedence. Thus: if(!(a || b)) { // means neither a nor b } However, using De Morgan’s Law, it could be written as: if(!a && !b) { // is not a and is … Read more

Why Switch/Case and not If/Else If?

Summarising my initial post and comments – there are several advantages of switch statement over if/else statement: Cleaner code. Code with multiple chained if/else if … looks messy and is difficult to maintain – switch gives cleaner structure. Performance. For dense case values compiler generates jump table, for sparse – binary search or series of … Read more

If-less programming (basically without conditionals) [closed]

There are some resources on the Anti-IF Campaign site, such as this article. I believe it’s a matter of degree. Conditionals aren’t always bad, but they can be (and frequently are) abused. Additional thoughts (one day later) Refactoring: Improving the Design of Existing Code is a good reference on this subject (and many others). It … Read more

JavaScript – get array element fulfilling a condition

In most browsers (not IE <= 8) arrays have a filter method, which doesn’t do quite what you want but does create you an array of elements of the original array that satisfy a certain condition: function isGreaterThanFive(x) { return x > 5; } [1, 10, 4, 6].filter(isGreaterThanFive); // Returns [10, 6] Mozilla Developer Network … Read more