Can any one let me know what's wrong with the above code :
the logic for the code is as:
user enters his name as : chandan tiwari
the function assign_value() used inthe code assigns the particular number to each character i.e.
c h a n d a n t i w a r i
2 3 4 5 67 8 9 0 1 2 4 5 (assuming)
the for loop should now calculate :
2+3+4+5+6+7+8+9+0+2+4+5=55

The $sum should have value=55 at the end.

The code is :

$name = $_POST['name'];
$sum = 0;
$name = (string) $name;
$name = strtolower($name);
$num = strlen($name);
for($i=0;$i<$num;$i++)
{ 
  $name = $name[i];
  $value = assign_value($name);
  $sum = $sum + $value;
}

$name_number = $sum;

but the above code is not calculating the right value, it is giving me answer but that is not he right answer.
If you you want I can provide the function assign_value() also.

Please sugget me something.

Thanking you.

    for($i=0;$i<$num;$i++)
    {
      $value = assign_value($name{$i});
      $sum = $sum + $value;
    } 

    just for general code streamlining:

    $sum = 0;
    $name = strtolower((string)$_POST['name']);
    
    for($i=0;$i<strlen($name);$i++)
    {
      $sum += assign_value($name{$i});
    }
    
    $name_number = $sum;
    

      thanks for reply.

      But it not giving me the right answer.
      It should not calculate the spaces .

      Here's what I'm doing in assign_value() function:

      function assign_value($word)
      { 	
        if($word=='a'|| $word=='i' ||$word=='j' ||$word=='q'||$word=='y')
         {
           $num = 1;
            return($num);
          }
        if($word=='b'|| $word=='k' ||$word=='r')
         {
           $num = 2;
            return($num);
          }
         if($word=='u'|| $word=='v' ||$word=='w')
         {
           $num = 6;
            return($num);
          }  
      if($word=='o'|| $word=='z') { $num = 7; return($num); } if($word=='c'|| $word=='g' ||$word=='l' ||$word=='s') { $num = 3; return($num); } if($word=='d'|| $word=='m' ||$word=='t') { $num = 4; return($num); } if($word=='e'|| $word=='h' ||$word=='n' ||$word=='x') { $num = 5; return($num); } if($word=='f'|| $word=='p') { $num = 8; return($num); } }

      please suggest

        If I would venture a guess that you actually want to strip out whitespace rather than just spaces, I suggest using [man]preg_replace/man. For example:

        // Assume $_POST['name'] is known to exist.
        // Strip off whitespace and turn it into lowercase.
        $name = strtolower(preg_replace('/\s/', '', $_POST['name']));
        
        $name_number = 0;
        $len = strlen($name);
        for ($i = 0; $i < $len; $i++)
        {
            $name_number += assign_value($name[$i]);
        }

        As for your assign value function: I recommend taking advantage of if-elseif-else instead of a whole bunch of if conditionals. You should also keep the return statement at the end, and initialise $num at the beginning. Alternatively, use a switch selection structure. $word is a rather misleading variable name since you are dealing with characters, not words.

          Thank you laserlight

          Thank you for your advice, now i will the use the as you have suggested .
          I am going to use ifelseif statement.

          Thanks ...Thanks alot.

            it's not giving me the right answer .
            if the user inputs 'chandan'

            then it should give an answer as - 26

            but not giving

            What do i do ?

              then it should give an answer as - 26

              It should give you 24.

              c = 3
              h = 5
              a = 1
              n = 5
              d = 4
              a = 1
              n = 5

              sum = 24

                Well, this gives me 24:

                <?php
                function assign_value($word)
                {     
                $num = 0; if($word=='a'|| $word=='i' ||$word=='j' ||$word=='q'||$word=='y') { $num = 1; } elseif($word=='b'|| $word=='k' ||$word=='r') { $num = 2; } elseif($word=='u'|| $word=='v' ||$word=='w') { $num = 6; }
                elseif($word=='o'|| $word=='z') { $num = 7; } elseif($word=='c'|| $word=='g' ||$word=='l' ||$word=='s') { $num = 3; }
                elseif($word=='d'|| $word=='m' ||$word=='t') { $num = 4; } elseif($word=='e'|| $word=='h' ||$word=='n' ||$word=='x') { $num = 5; } elseif($word=='f'|| $word=='p') { $num = 8; } return $num; } // Strip off whitespace and turn it into lowercase. $name = strtolower(preg_replace('/\s/', '', 'chandan')); $name_number = 0; $len = strlen($name); for ($i = 0; $i < $len; $i++) { $name_number += assign_value($name[$i]); } echo $name_number; ?>

                  well,
                  I am not doing this code particularly for 'chandan' it could be anything.
                  The user enters his name, with spaces and all this thing, how can I manipulate this to do that.
                  It should only calculate the characters which are there in the name.
                  please suggets me .

                  and suppose,
                  i have 24 and i want to add it like = 2+4=6
                  this is done for any number which I get after above code (covert it in to single digit by adding.)

                  can the following function be used or any modification :

                  function math_calc($num){ 
                      $str = (string) $num; 
                      while(strlen($str) > 1){ 
                          $num = $str[0]; 
                          for($i=1; $i<strlen($str); $i++){ 
                              $num += $str[$i]; 
                          } 
                          $str = (string) $num; 
                      } 
                      return (int) $str; 
                  } 
                  $namank = math_calc($sum);

                  the $namank should be = 6

                    The user enters his name, with spaces and all this thing, how can I manipulate this to do that.
                    It should only calculate the characters which are there in the name.

                    My test example does exactly that, except that the input is hardcoded as 'chandan'. You would replace it with $_POST['name'], as per your original code snippet.

                    and suppose,
                    i have 24 and i want to add it like = 2+4=6
                    this is done for any number which I get after above code (covert it in to single digit by adding.)

                    You are basically calculating the sum of digits recursively in base 10. If no overflow is expected, you can use a shortcut from modular arithmetic:

                    function recursiveDigitSum($num) {
                        // No overflow expected => use modular arithmetic shortcut.
                        $num %= 9;
                        return $num == 0 ? 9 : $num;
                    }

                      Thanks

                      Thank you very much

                      It works !

                      Now, my code is doing perfectly right calculation for me..

                      Thanks again, Laserlight.

                        Write a Reply...