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.

1
  • What are you replacing the empty string with? Commented Nov 6, 2015 at 19:25

2 Answers 2

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>";

}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.