Viewed   49 times

Are there any differences between...

if ($value) {

}

...and...

if ($value):

endif;

?

 Answers

4

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; ?>
Friday, October 28, 2022
5

${ } (dollar sign curly bracket) is known as Simple syntax.

It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.

If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name.

<?php
$juice = "apple";

echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
// Valid. Explicitly specify the end of the variable name by enclosing it in braces:
echo "He drank some juice made of ${juice}s.";
?>

The above example will output:

He drank some apple juice.
He drank some juice made of .
He drank some juice made of apples.
Thursday, October 13, 2022
1

global is a keyword that should be used by itself. It must not be combined with an assignment. So, chop it:

global $x;
$x = 42;

Also, as Zenham mentions, global is used inside functions, to access variables in an outer scope. So the use of global as it is presented makes little sense.

Another tip (though it will not really help you with syntax errors): add the following line to the top of the main file, to help debugging (documentation):

error_reporting(E_ALL);
Friday, August 19, 2022
4

VB has the following If statement which the question refers to, I think:

' Usage 1
Dim result = If(a > 5, "World", "Hello")
' Usage 2
Dim foo = If(result, "Alternative")

The first is basically C#'s ternary conditional operator and the second is its coalesce operator (return result unless it’s Nothing, in which case return "Alternative"). If has thus replaced IIf and the latter is obsolete.

Like in C#, VB's conditional If operator short-circuits, so you can now safely write the following, which is not possible using the IIf function:

Dim len = If(text Is Nothing, 0, text.Length)
Tuesday, December 27, 2022
 
3

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
}
Thursday, November 17, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :