2

The following will echo "12334".

However, I would like for the "12334" to be placed in a variable say $wordNumValue. I know it might be a simple thing, but to me it is not. Any help is much appreciated.

<?php
$msg ="hello";
$arrEn = str_split($msg);
foreach ($arrEn as &$value) {
    if ($value == 'h') {
          echo "1";
        } elseif ($value == 'e') {
          echo "2";
        } elseif ($value == 'l') {
          echo "3";
        } elseif ($value == 'o') {
          echo "4";
        } else {
          echo 'NULL';
        } 
}
?>      
1
  • If it helps you can accept the best answer.. Commented Sep 28, 2014 at 22:48

4 Answers 4

2
<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = "";
foreach ($arrEn as &$value) {
if ($value == 'h') {
      echo "1";
      $wordNumValue .= "1";
    } elseif ($value == 'e') {
      echo "2";
      $wordNumValue .= "2";
    } elseif ($value == 'l') {
      echo "3";
      $wordNumValue .= "3";
    } elseif ($value == 'o') {
      echo "4";
      $wordNumValue .= "4";
    } else {
      echo 'NULL';
    } 
}
?>     
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = '';
foreach ($arrEn as &$value) {
    if ($value == 'h') {
        echo "1";
        $wordNumValue .= "1";
    } elseif ($value == 'e') {
        echo "2";
        $wordNumValue .= "2";
    } elseif ($value == 'l') {
        echo "3";
        $wordNumValue .= "3";
    } elseif ($value == 'o') {
        echo "4";
        $wordNumValue .= "4";
    } else {
        echo 'NULL';
    }
}

But as you have many elseifs I, would do this instead:

<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = '';
foreach ($arrEn as &$value) {
    switch ($value) {
        case "h":
            $wordNumValue .= "1";
            break;
        case "e":
            $wordNumValue .= "2";
            break;
        case "l":
            $wordNumValue .= "3";
            break;
        case "o":
            $wordNumValue .= "4";
            break;
        default:
            echo 'NULL';
    }
}
echo $wordNumValue;

And finally I echoed $wordNumValue instead of echoing single numbers several times.

Comments

1

You can add each string onto a variable using the .= assignment operator;

<?php
$msg ="hello";
$arrEn = str_split($msg);
foreach ($arrEn as &$value) {
    if ($value == 'h') {
          $wordNumValue .= "1";
        } elseif ($value == 'e') {
          $wordNumValue .= "2";
        } elseif ($value == 'l') {
          $wordNumValue .= "3";
        } elseif ($value == 'o') {
          $wordNumValue .= "4";
        }
}
?>

You don't need the final else. See this, PHP: Assignment Operators, and this, PHP: String Operators.

Hope this helps.

1 Comment

Hi @Hemn Baker, if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
-1

You don't need to change your code , before foreach add ob_start(); and after foreach add $wordNumValue = ob_get_clean();

1 Comment

That is possibly the biggest sledgehammer I've ever seen to crack the smallest nut in existence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.