标签: conditional-operator

PHP中的反向分配(LEFT到RIGHT)

我有以下条件:

if (isset($x)){
   $y_class = 'new value';
}else { 
   $z_class = 'new value';
}
Run Code Online (Sandbox Code Playgroud)

我可以将上述if情况改为三元吗?通过这个,我的意思是,有没有办法将LEFT分配 给RIGHT?

想象代码:

'new value' = (isset($x)) ? $y_class : $z_class;
Run Code Online (Sandbox Code Playgroud)

php conditional-operator

2
推荐指数
2
解决办法
480
查看次数

PHP:NOT Equal(!=)运算符的多个条件未按预期工作

我正在研究以下代码,其中我想使用php if conditions多个非运算符的多个(示例如下).

但是当我执行跟随时php code,它总是返回true(括号中的平均内容总是执行)甚至条件为假.

我想问一下下面代码中的问题是什么,是否有使用多个的特定语法或规则!= operator in php conditions?我很惊讶地看到,如果我用替换下面的代码!= operator与== operator它工作正常.

if( $ext!="exe" || $ext!="html" ||  $ext!="htm"  ||  $ext!="js" ||  $ext!="iso" ||  $ext!="zip"  ||  $ext!="rar" )
{ // ececk extension
    echo $ext."extension";

}
else{
    echo "not match";   
}
Run Code Online (Sandbox Code Playgroud)

等待你的回复.抱歉我的英语不好

php if-statement conditional-operator

2
推荐指数
1
解决办法
2661
查看次数

似乎无法使我的IF语句正常工作

我似乎无法获得这些if语句按预期工作.无论我输入"字符串答案",它总是跳转到第一个IF语句,其中条件设置为仅在答案正好为"n"或"N"时执行块或者答案恰好为"y"的块或"Y".如果你输入任何其他内容,它应该返回0.

    // Game Recap function, adds/subtracts player total, checks for deposit total and ask for another round
    int gameRecap() {
    string answer;
    answer.clear();

    cout << endl << "---------------" << endl;
    cout << "The winner of this Game is: " << winner << endl;
    cout << "Player 1 now has a total deposit of: " << deposit << " credits remaining!" << endl;
    cout << "-------------------------" << endl;

    if (deposit < 100) {
       cout << "You have no remaining credits to …
Run Code Online (Sandbox Code Playgroud)

c++ if-statement conditional-operator conditional-statements

2
推荐指数
2
解决办法
199
查看次数

Python三元运算符和else中的赋值

三元运算符非常有用,为什么它在这种特殊情况下不起作用:

c="d"
d={}
d[c]+=1 if c in d else d[c]=1
Run Code Online (Sandbox Code Playgroud)

它给:

d[c]+=1 if c in d else d[c]=1
                           ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我没有看到任何错误,因为没有三元运算符的同样的事情:

c="d"
d={}
if c in d:
    d[c]+=1
else:
    d[c]=1
Run Code Online (Sandbox Code Playgroud)

python dictionary conditional-operator python-2.7

2
推荐指数
1
解决办法
722
查看次数

Javascript ternary operator with object

I came across a piece of code I am trying to figure out, the code basically stores the occurrence of the amount of time a word appears in a text document, so the function countWordsIntext takes in the desired text and displays the word and the number of occurrence in the text, e.g would: 3 but: 5 very: 6

while looking at the function that counts the word in the text I cant figure out how the conditional tenary operation …

javascript arrays conditional-operator

2
推荐指数
1
解决办法
1万
查看次数

逻辑运算的快捷方式

我只是想知道,是否有逻辑运算符(&&,||)的快捷方式。就像如果我想做类似的事情i = i + 10,我可以做i += 10

我搜索这个的原因是因为我有一个验证功能,它分为几个功能。下面是一个模拟:

function f1(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f2(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f3(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function f4(){
  return Math.ceil(Math.random()*10) %2 === 0? true:false
}

function validate(){
  var valid = true;
  valid = valid && f1();
  valid = valid && f2();
  valid = valid && f3();
  valid = valid && f4();
  
  console.log(valid);
}

validate(); …
Run Code Online (Sandbox Code Playgroud)

javascript conditional-operator

2
推荐指数
1
解决办法
501
查看次数

当datetime为null时,如何将可空的datetime值转换为string.empty?

我正在读回一个可空的DateTime?属性,然后以短日期格式将该值分配给字符串属性.

我可以将日期时间值转换为短日期字符串并分配给IT_Date_String属性.但是""如果IT_Date为null,我不确定如何为字符串赋值.

你如何转换日期时间?datetime时为string.empty的值?一片空白?

这是linq中的赋值:

var status_list = query_all.ToList().Select(r => new RelStatus
{
    IT_Date_String = r.IT_Date.Value.ToString("yyyy-MM-dd") != null ? r.IT_Date.Value : null
}).ToList();
Run Code Online (Sandbox Code Playgroud)

以及模型中的属性:

public DateTime? IT_Date { get; set; }
public string IT_Date_String { get; set; }
Run Code Online (Sandbox Code Playgroud)

c# datetime nullable conditional-operator

2
推荐指数
2
解决办法
3363
查看次数

2
推荐指数
1
解决办法
79
查看次数

为什么我在字符串上的索引+问号运算符上得到多个数字?

当我运行以下代码时

public class Program
{
    public static void Main()
    {
        string s = "480";
        Console.WriteLine(1 == -1 ? 0 : s[1]);
        Console.WriteLine(s[1]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我明白了

56
8
Run Code Online (Sandbox Code Playgroud)

我不明白我是怎么得到56的.

c# conditional-operator

2
推荐指数
1
解决办法
39
查看次数

在条件运算符中使用对象两次是否会产生UB?

正如我所了解的那样,条件运算符(三元运算符“?”)保证了其操作数的求值顺序,我想知道是否向变量分配了?在两个表达式之一中使用该变量的返回值。是否成为UB。这是我所拥有的:

我编写了这个程序,尝试根据字符串是否str包含有效的数字字符将字符串转换为整数,例如:+-.0123456789,因此我在一个表达式中使用了std::stoiwith std::string::find_first_of和conditional运算符:

std::string str = "a^%^&fggh67%$hf#$";
std::size_t pos = 0;
auto val = (((pos = str.find_first_of("0123456789.+-")) != std::string::npos) ?
    std::stoi(str.substr(pos)) : -1);

std::cout << val << std::endl; // 67

str = "a^%^&fggh__#$!%$hf#$";
pos = 0;
val = (((pos = str.find_first_of("0123456789.+-")) != std::string::npos) ?
    std::stoi(str.substr(pos)) : -1);

std::cout << val << std::endl; // -1
Run Code Online (Sandbox Code Playgroud)

正如你所看到的一段代码看起来做工精细,我知道,如果值-1是在无意中发现str,我们不知道是否val拥有-1成功运作或失败(?运营商)的。但是我只想知道我编写的这段代码是否具有UB?

  • 感谢在上一个主题UB中指导我的成员,我已经将其更正。

c++ conditional-operator sequence-points

2
推荐指数
1
解决办法
68
查看次数