conditionals

PHP conditionals, brackets needed?

I was just browsing a forum and someone asked about a php file they had found on the web. It has several spots like this in the code: if ($REMOTE_ADDR == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR); I have always thought brackets are needed to enclose what you want to do if the condition is true. Is there some other alt...

1-line IF statements in java

Is it possible to have IF statements without braces in Java, e.g: if (x == y) z = x * y; else z = y - x; It's possible in PHP, and I'm not sure if I'm doing something wrong. Clarification: Here's my actual code that i'm using: if (other instanceof Square) Square realOther = (Square) other; else Rectangle realOther = (Rec...

How can I write my program without so many if statements?

I have the following program so far: using System; namespace ParkingTicket { class Program { static void Main() { int speed; int yrInSchool; double fine; char choice = ' '; do { Console.Clear(); speed = GetSpee...

Conditional Operators in Javascript

Is it ok to use conditional operators like a statement like so? (x == y) ? alert("yo!") : alert("meh!"); Or is it more correct to use it to assign a value like so? z = (x == y) ? "yo!" : "meh!"; If it's not incorrect to use it like a statement, then is it possible to add more than one line of code for execution like so? Is it more ...

How do I turn a conditional chain into faster less ugly code?

I have 9 different grammars. One of these will be loaded depending on what the first line of txt is on the file it is parsing. I was thinking about deriving the lexer/parser spawning into sep. classes and then instantiating them as soon as I get a match -- not sure whether that would slow me down or not though. I guess some benchmarkin...

How to indent long conditionals for 'if' statements?

My question relates to this previous question, but the solutions offered don't address the problem I have outlined below. After a google search I haven't found any code style guidelines that address the specific problem of long conditionals in an if statement like this. if( isNull(value1) || isToLong(value1) || hasBadFormat(valu...

visitor pattern against conditionals?

I don't seem to find this in usage scenarios for the visitor pattern (or maybe I don't get it). It's also not hierarchical. Let's use an authentication example. A UserAuthenticator authenticates credentials given by a user. It returns a result object. The result object contains the result of the authentication: authentication succeeded,...

Conditionally display content based on List Item value in aspx?

I have an order form with a dropdown list of 4 membership levels and a dropdown list below it for selecting T-shirt sizes that go along with the memberships. All memberships come with a t-shirt except no. 4. The form validates in the aspx code client side that there is a value for Membership and a value for T-shirt size. But when members...

PHP & Conditional

I was searching the net for a random password generator and I come across this code <?php function generatePassword($length=9, $strength=0) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; if ($strength & 1) { $consonants .= 'BDGHJLMNPQRSTVWXZ'; } if ($strength & 2) { $vowels .= "AEUY"; } if ($s...

Fastest/One-liner way to conditionally remove multiple directories on Unix

What is the shortest way I can write: rm -rfv public/stylesheets public/images public/javascripts and make it conditional, with something like: if [ ! -d public/stylesheets ]; then rm -rfv public/stylesheets; fi ... Just discovered/found a use for command-line conditionals :) ...

conditionals for C++ using MSBuild/vsbuild?

I have a C++ project in Visual Studio 2008 and I'd like to be able to compile several versions from the command line, defining conditional variables (aka #define). If it were just a single file to compile I'd use something like cl /D, but this is complex enough that I would like to be able to use VS's other features like the build order ...

htaccess conditionals

I have setup htpasswd authentication on my live site and it works great, but I don't want to be asked for a password when I am working on the development environment. In my httpd.conf file I have: SetEnv APP_ENV "development" In my .htaccess file I have: AuthName "Restricted Area" AuthType Basic AuthUserFile /path/to/file/.htpasswd ...

Where can I read about conditionals done with ? and :

Possible Duplicate: Reference - What does this symbol mean in PHP? I've been doing conditionals with if/else or a year or so now. Looking at some new code, I'm seeing a conditional that appears to use ? and : instead of if and else. I'd like to learn more about this but am not sure what to google to find articles explaining ...