dongxieting9623 2015-04-30 05:11
浏览 21
已采纳

php地图数字到字符串

I am trying to write a program in php which first prompts a user for a string. once they enter a string it is supposed to give the equivalent digits if it were on a phone. eg ABC(2) , DEF(3) , GHI(4) , JKL(5) , MNO(6) , PQRS(7) , TUV(8) , WXYZ(9). Im using a for loop and nested if statements but i am not getting the correct output. Am I going about this the right way? my code is below

<?php

$str = $_POST['usersString'];

$len = strlen($str);

for($i=0; $i<$len-1; $i++){
    if($i="a" || $i="b" || $i="c"){
        echo "1";
    }

    if($i="d" || $i="e" || $i="f"){
        echo "2";
    }

    if($i="g" || $i="h" || $i="i"){
        echo "3";
    }
}    

?>
<form action="task17.php" method="POST">
Enter a string <input type="text" name="usersString" />
<input type="submit" value="enter" />
</form> 
  • 写回答

4条回答 默认 最新

  • drtoaamk20278 2015-04-30 05:22
    关注
    <?php
    
    $str = $_POST['usersString'];
    
    $len = strlen($str);
    
    for($i=0; $i<$len; $i++){
    
       if($str[$i]=="a" || $str[$i]=="b" || $str[$i]=="c"){
        echo "1";
    
       }else if($str[$i]=="d" || $str[$i]=="e" || $str[$i]=="f"){
        echo "2";
    
       }else if($str[$i]=="g" || $str[$i]=="h" || $str[$i]=="i"){
        echo "3";
       }
    }    
    
    ?>
    

    your condition is wrong $i="a" It should be $str[$i]=="a"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在