Posts

Showing posts with the label Conditional Statements

Are "elseif" And "else If" Completely Synonymous?

Answer : From the PHP manual: In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior. Essentially, they will behave the same, but else if is technically equivalent to a nested structure like so: if (first_condition) { } else { if (second_condition) { } } The manual also notes: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error. Which means that in the normal control structure form (ie. using braces): if (first_condition) { } elseif (second_condition) { } either elseif or else ...

Can You Use If/else Conditions In CSS?

Answer : Not in the traditional sense, but you can use classes for this, if you have access to the HTML. Consider this: <p class="normal">Text</p> <p class="active">Text</p> and in your CSS file: p.normal { background-position : 150px 8px; } p.active { background-position : 4px 8px; } That's the CSS way to do it. Then there are CSS preprocessors like Sass. You can use conditionals there, which'd look like this: $type: monster; p { @if $type == ocean { color: blue; } @else if $type == matador { color: red; } @else if $type == monster { color: green; } @else { color: black; } } Disadvantages are, that you're bound to pre-process your stylesheets, and that the condition is evaluated at compile time, not run time. A newer feature of CSS proper are custom properties (a.k.a. CSS variables). They are evaluated at run time (in browsers supporting them). With them you could do somet...

Can I Use Conditional Statements With EJS Templates (in JMVC)?

Answer : For others that stumble on this, you can also use ejs params/props in conditional statements: recipes.js File: app.get("/recipes", function(req, res) { res.render("recipes.ejs", { recipes: recipes }); }); recipes.ejs File: <%if (recipes.length > 0) { %> // Do something with more than 1 recipe <% } %> Conditionals work if they're structured correctly, I ran into this issue and figured it out. For conditionals, the tag before else has to be paired with the end tag of the previous if otherwise the statements will evaluate separately and produce an error. ERROR! <% if(true){ %> <h1>foo</h1> <% } %> <% else{ %> <h1>bar</h1> <% } %> Correct <% if(true){ %> <h1>foo</h1> <% } else{ %> <h1>bar</h1> <% } %> hope this helped. Yes , You can use conditional statement with EJS like if else , ternary operat...