Skip to content
Advertisement

Seperate integer into character PHP

i try to make scoresheet where $row[“first”]=756987 show Math 7, Biology 5, Sport 6, Sociology 9, Chemist 8, English 7 but it keep showing all 7
can someone help solve this and show some example
appreciate your help

foreach (str_split($row["first"]) as $v){}
echo "<table>
<tr><th colspan='2'>Score </th></tr>
<tbody class='tbody'>
        <tr>
            <td>Math" . $v. "</td>
            <td>Biology" .$v "</td>
            <td>Sport" . $v. "</td>
            <td>Sociology" . $v. "</td>
            <td>Chemist" . $v. "</td>
            <td>English" . $v. "</td>
        </tr>
</tbody>

Advertisement

Answer

Use str_split() and list() method for your use case, example below:

$row["first"]=756987;
list($math, $bio, $sport, $socio, $chemist, $english) = str_split($row["first"]);
echo "<table>
<tr><th colspan='2'>Score </th></tr>
<tbody class='tbody'>
        <tr>
            <td>Math " . $math. "</td>
            <td>Biology " .$bio. "</td>
            <td>Sport " . $sport. "</td>
            <td>Sociology " . $socio. "</td>
            <td>Chemist " . $chemist. "</td>
            <td>English " . $english. "</td>
        </tr>
</tbody>";

Docs

list() : https://www.php.net/manual/en/function.list.php

str_split() : https://www.php.net/manual/en/function.str-split

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement