比较一些数字并输出结果


Compare some digits and print a result

朋友和所有的人。我有这个脚本的问题,找不到如何解决它。我有一些DB的电话号码。这些号码来自不同的移动运营商,具有标准的首位数,如(运营商代码)38098(电话号码),38066(电话号码),38063(电话号码),38099(电话号码)。我需要这个脚本比较第一个数字并打印这个号码的运营商(对不起,我的英语不好),例如:38066 344-56-67这个用户有沃达丰;38098 344-56-67该用户有Kyivstar。所以我这样做:

<? if ($mobile="38099") { ?>This user have Vodafone number
<? }else if ($mobile="38066") { ?>This user have Vodafone number
<? }else if ($mobile="38067") { ?>This user have Kyivstar number
<? } ?>

但是这个脚本什么也没做。只有当电话号码数字等于"if"answers"else if"中的数字时才有效。请帮我解决这个问题

因为是赋值,而不是比较,所以这里需要比较值

类似:

if(variable == value)

这叫做,if ($mobile="38099")赋值,你不能像那样比较值,=号是用来赋值的

对于比较,您可以使用==,用于比较相等或不相等的值,例如:

if ($mobile == "38099")

如果你想比较值和数据类型在一起,你可以使用===符号,这将比较值或等于或不与数据类型检查。

手册将帮助您了解更多:http://php.net/manual/en/language.operators.comparison.php

<?php
$test = '38066 344-56-67'; 
$mobile = substr($test, 0, 5);
if ($mobile=="38099") { 
  echo "This user have Vodafone number";
}
else if ($mobile=="38066") 
{ 
  echo "This user have Vodafone number";
}
else if ($mobile=="38067") 
{ 
  echo "This user have Kyivstar number";
} 
?>

我认为如果你的例子真的像" 38066 344-56-67 ",你应该使用

$nra = explode(" ", $mobile);
并测试新表中的第一个索引,如
if($nra[0] == "38099")
{
     echo"This user have Vodafone number";
}
else if($nra[0] == "38066").....

并使用==代替"="进行比较