dsvjmc0907 2012-07-23 17:41
浏览 121
已采纳

PHP未定义的变量错误新手在这里,似乎无法弄清楚如何修复错误

I am trying to create a so-called Greek Numerals Converter tool. The input here will be letters and these letters will be converted into numbers. For every letter, there will be a corresponding number for it and also taking into consideration the digits. Now, my idea here is to create an array wherein all the letters and their corresponding numerical values will be put. So, when the user enters a combination of letters (up to 3 digits only), the convert() function will be looking for the corresponding values of those letters entered, get the values, and sum everything up. Unfortunately, as I went along the coding, I came up with this error (the one mentioned in the title) and I can't seem to figure out how to solve it. This might sound very stupid, but please do understand as I am still in the process of learning PHP.

Here are the codes that I was able to come up with.

<?php error_reporting(0);
    class GreekNum
    {


        public function setText($text)
        {
            $this->text=$text;
        }

        public function getText()
        {
            return $this->text;
        }       

        public function Convert($text)
        {

            $TextLength = strlen($text);
            $text = strtoupper($text);
            $text = str_split($text);



            $collection = array(
                                    "A" => 1,
                                    "B" => 2,
                                    "G" => 3,
                                    "D" => 4,
                                    "E" => 5,
                                    "#" => 6,
                                    "Z" => 7,
                                    "Y" => 8,
                                    "H" => 9,
                                    "I" => 10,
                                    "K" => 20,
                                    "L" => 30,
                                    "M" => 40,
                                    "N" => 50,
                                    "X" => 60,
                                    "O" => 70,
                                    "P" => 80,
                                    "Q" => 90,
                                    "R" => 100,
                                    "S" => 200,
                                    "T" => 300,
                                    "U" => 400,
                                    "F" => 500, 
                                    "C" => 600,             
                                    "$" => 700,
                                    "W" => 800,
                                    "3" => 900, 
                                );

            if(isset($text))
            {
                $total = 0;
                for($i=0; $i<$TextLength; $i++)
                {
                    if(array_key_exists($text[$i], $collection))
                    {
                        $total += $collection[$text[$i]];
                    }

                    if(end($text)!='.')
                    {
                        echo "Please make sure your line ends with a period. <br><br>"; 
                        return false;
                    }
                }
                return $total;

            }                   
        }

    }   

    $GreekNum = new GreekNum();

    $text = $_POST['text'];
    $GreekNum->setText($text);

    echo "<br>";
    echo "<b>Entered Values: </b><br>". $GreekNum->getText();
    echo "<br>";
    echo "<br>";

    echo "<b>Decimal Number Equivalence: </b><br/>". $GreekNum->Convert($text);




    ?>

The error is about the $total and I can't seem to figure out how to fix this. Any help would be very much appreciated. Thanks a lot!

This has been resolved already, but for the sake of whoever might need this, I'm editing this again to the code that currently works for me.

  • 写回答

4条回答 默认 最新

  • dongxing7530 2012-07-23 17:43
    关注

    You need to declare $total before you can use it, try the following

    ....
    $total = 0;
    for($i=0; $i<$TextLength; $i++)
    {
          .....
    

    The reason it doesn't work in your case is because you are doing $total += $text[$i]; this expands to $total = $total + $text[$i]; unfortunately the $total on the right hand side of the equals does not exist so it is undefined.

    Im also wondering why you are accessing $text as if it were an array, its a String, I believe $collection is the array with your values in.

    So what you need to do is split the $text string first into an array of its characters. Then for each of these characters look it up in the collection, if it exists add its value to the running total.

    ....
    
    $text = "AQ3";
    $TextLength = strlen($text);
    $splitText = str_split($text);
    
    $total = 0;
    for($i=0; $i<$TextLength; $i++)
    {
        $currentCharacter = $splitText[$i];
        if(array_key_exists($currentCharacter, $collection))
        {   
            $total += $collection[$currentCharacter];
        }
    }
    
    ....
    

    As you can see $splitText[$i] is the current character, this is used as a key in the search but is also needed in the following statement $total += $collection[$splitText[$i]]; as it is the key used to lookup.

    For the string AQ3 this will return the value 991 which I presume was your intended result?

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

报告相同问题?

悬赏问题

  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan