0

I'm trying to fill in a string in an array value that has a empty '' do I need to use isset or empty to check it first?

Here is my code

echo table();

function table() {
    $a = array  ('0' => array('Jan de Boer', '213','440'),
                 '1' => array('Gerda Severin','214','442'),
                 '2' => array('Jean Dubois','215',''),
                 '3' => array('Peter Geringh','221','449'),
                 '4' => array('ricardo','666','666'));

    echo "<table border='6px'>
    <tr><th colspan='3'>Alle werknemers</th></tr>
    <tr><th>Naam</th>
    <th>kamer</th>
    <th >Toestelnummer</th></tr>";

    for ($x=0;$x<5;$x++){
        echo "<tr>";
        for($y=0;$y<3;$y++){
            echo "<td>",$a[$x][$y].'</td>';
        }
        echo "</tr>";
    }
    echo "</table>";
}

It needs to fill in the blank '' like a string unknown.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
the R
  • 49
  • 8

2 Answers2

1

Perform a check if the string is blank and replace with a value, in my example 'UNKNOWN'

    echo table();
function table()

{    
$a = array  ('0' => array('Jan de Boer', '213','440'),
             '1' => array('Gerda Severin','214','442'),
             '2' => array('Jean Dubois','215',''),
             '3' => array('Peter Geringh','221','449'),
             '4' => array('ricardo','666','666'));

echo "<table border='6px'>
<tr><th colspan='3'>Alle werknemers</th></tr>
<tr><th>Naam</th>
<th>kamer</th>
<th >Toestelnummer</th></tr>";

    for ($x=0;$x<5;$x++){
    echo "<tr>";
    for($y=0;$y<3;$y++){
    if($a[$x][$y] == "") $a[$x][$y] = 'UNKNOWN';
        echo "<td>",$a[$x][$y].'</td>';
    }
    echo "</tr>";
}

echo "</table>";

}
Lucky Chingi
  • 2,248
  • 1
  • 10
  • 15
0

It depends on what you are checking for. empty will return true if the string is set and has no characters in it while !isset will only return true if the string does not exist at all.

If you are just trying to avoid errors and you are ok with a '' string use !isset. If you are looking to make sure the string actually has anything in it use empty.

SunKnight0
  • 3,331
  • 1
  • 10
  • 8