比较整数和字符串PHP [英] Compare integers and strings PHP

查看:111
本文介绍了比较整数和字符串PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要通过POST捕获两个变量。

Need to capture two variables via POST.

如果它们是不同的整数,保存$ mensalidade = 4和整数以及任何其他状态保存$ mensalidade = 8.

If they are different integers, save $mensalidade = 4 and a integer and any other state save $mensalidade=8.

我试试这个但是没有用..

I try this but dont work..

if (is_int($_POST['linha_ida']) != is_int($_POST['linha_volta'])) {

   $mensalidade= $_POST['mensalidade']=4;

} else  {

   $mensalidade= $_POST['mensalidade']=8;
}

打破了一点头,现在它是完美的!
感谢所有

Broke a little more head and now it's perfect! Thanks to all

代码看起来像这样

if($linha_ida === $linha_volta || preg_match( '/[A-Z]/' , $linha_volta )|| preg_match( '/[A-Z]/' , $linha_ida )){
    $mensalidade= $_POST['mensalidade']=8;
    }  else{
    $mensalidade= $_POST['mensalidade']=4;
}


推荐答案

只需使用(int)解析器将字符串转换为整数。

Just use the (int) parser to convert the string to an integer.

if ((int)$_POST['linha_ida']) != (int)($_POST['linha_volta']) && is_int($_POST['linha_ida']) && is_int($_POST['linha_volta']) {

    $mensalidade= $_POST['mensalidade']=4;
} else  {
    $mensalidade= $_POST['mensalidade']=8;
}

取自caCtus的问题评论

Taken from the Question comments from caCtus


caCtus:
is_int()如果参数是整数,则返回true或false。如果将is_int($ iamaninteger)与is_int($ iamanintegertoo)进行比较,则比较true和true,而不是变量的值。

caCtus: is_int() returns true or false if the parameter is an integer or not. If you compare is_int($iamaninteger) to is_int($iamanintegertoo), you compare true and true, not your variables' values.

这篇关于比较整数和字符串PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆