Asked  8 Months ago    Answers:  5   Viewed   29 times

Are there any differences between...

if ($value) {

}

...and...

if ($value):

endif;

?

 Answers

46

They are the same but the second one is great if you have MVC in your code and don't want to have a lot of echos in your code. For example, in my .phtml files (Zend Framework) I will write something like this:

<?php if($this->value): ?>
Hello
<?php elseif($this->asd): ?>
Your name is: <?= $this->name ?>
<?php else: ?>
You don't have a name.
<?php endif; ?>
Wednesday, March 31, 2021
 
Sagar
answered 8 Months ago
69

rename your function to something other than "goto". That is a reserved name.

Wednesday, March 31, 2021
 
Pegues
answered 8 Months ago
57

You are using curly quotes.

Replace all the “ ” and ‘ ’ to " and ' respectively.

Wednesday, March 31, 2021
 
Anand
answered 8 Months ago
73

The warning message:

  the condition has length > 1 and only the first element will be used

tells you that using a vector in if condition is equivalent to use its first element :

[if (v == 1)] ~ [if (v[1] == 1)] ## v here is a vector

You should use the vectorized ifelse. For example you can write your condition like this:

create_dummies<-function(data, categorical_preds){
  ## here I show only the first condition 
  data$setosa_flg <-
       ifelse (categorical_preds=="setosa",1,0)
  data
}
Tuesday, June 15, 2021
 
Packy
answered 5 Months ago
55

OK, that's quite a few questions. Let me attempt to answer them:

  1. What is the difference between when and if.

    The questions inside a when clause are only "active" at the instant that the conditional expressions used in the when clause becomes active. In contrast, equations inside an if statement are true as long as the conditional expression stays true.

  2. What's the purpose of foo?

    Probably for visualization. It has no clear impact on the model that I can see.

  3. Why is impact listed in the when clause.

    One of the problems you have so-called Zeno systems like this is that it will continue to bounce indefinitely with smaller and smaller intervals. I suspect the impact flag here is meant to indicate when the system has stopped bouncing. This is normally done by checking to make sure that the conditional expression h<=0.0 actually becomes false at some point. Because event detection includes numerical tolerancing, at some point the height of the bounces never gets outside of the tolerance range and you need to detect this or the ball never bounces again and just continues to fall. (it's hard to explain without actually running the simulation and seeing the effect).

  4. What does the , do in the when clause.

    Consider the following: when {a, b} then. The thing is, if you want to have a when clause trigger when either a or b become true, you might think you'll write it as when a or b then. But that's not correct because that will only trigger when the first one becomes true. To see this better, consider this code:

    a = time>1.0;
    b = time>2.0;
    when {a, b} then
      // Equation set 1
    end when;
    when a or b then
      // Equation set 2
    end when;

So equation set 1 will get executed twice here because it will get executed when a becomes true and then again when b becomes true. But equation set 2 will only get executed once when a becomes true. That's because the whole expression a or b only becomes true at one instant.

These are common points of confusion about when. Hopefully these explanations help.

Tuesday, August 24, 2021
 
user2725742
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :