PHP Пустая строка длиной 32

EDIT: строка выводится и интерпретируется браузером. Глупая ошибка.

В моем проекте я создал класс для генерации HTML-тегов, в которых я нуждаюсь, а скорее отдал их всем. У меня есть функция, называемая generateTag($control, $isCardValue = true) в классе php, называемом Card . Эта функция генерирует тег HTML, основанный на свойствах, переданных через параметр $control array. Вот как выглядит функция:

 public function generateTag($control, $isCardValue = true) { if ($isCardValue) { // First we convert the 'class' element to an array if (isset($control['class']) && gettype($control['class']) !== 'array') { $control['class'] = array($control['class']); } // Then we add the 'card-value' class to that array. $control['class'][] = 'card-value'; } // The tag key is mandatory $tag = '<' . $control['tag']; // All keys other than 'tag' & 'content' are considered attributes for the HTML tag. foreach ($control as $key => $value) { switch ($key) { case 'tag': break; case 'content': break; default: if (gettype($value) === 'array') { $tag .= ' ' . $key . '="' . implode(' ', $value) . '"'; } elseif (gettype($value) === 'NULL') { $tag .= ' ' . $key; } else { $tag .= ' ' . $key . '="' . $value . '"'; } break; } } $tag .= '>'; // If the 'content' key is not passed through $control, we assume that the tag // doesn't need to be closed (eg <input> doesn't need a closing tag) if (isset($control['content'])) { if (gettype($control['content']) === 'array') { foreach ($control['content'] as $child) { $tag .= $this->generateTag($child); } } else { $tag .= $control['content']; } $tag .= '</' . $control['tag'] . '>'; } return $tag; } 

Я использую эту функцию для создания всех тегов <option> для окна <select> . Я просто перебираю массив для генерации тегов:

 foreach ($lists['tags'] as $key => $tag) { $tag_options[$key] = array( 'tag' => 'option', 'value' => $tag['tag_id'], 'content' => $tag['tag_name_en'], ); var_dump($card->generateTag($tag_options[$key], false)); } 

Здесь все странно. Я вызываю var_dump в сгенерированной строке, и я получаю следующий вывод:

 string(32) "" string(35) "" string(33) "" string(33) "" string(38) "" string(32) "" string(42) "" string(30) "" string(41) "" string(34) "" string(35) "" string(34) "" string(29) "" string(36) "" string(37) "" string(31) "" string(36) "" string(67) "" string(36) "" string(33) "" string(36) "" string(36) "" 

Похоже, что он создает пустую строку длиной ~ 35? Самое странное, что когда я вызываю substr($tag_options[$key], 0, 1) , он дает мне < как следует. Но когда я вызываю substr($tag_options[$key], 0, 2) , он дает мне «пустую» строку длины 2. Любое понимание того, что происходит?

Поскольку вы просматриваете вывод в браузере, он по-прежнему анализирует HTML в каждой строке как HTML, и вы не видите его на отображаемой странице. var_dump не делает HTML-кодирование.

Как вы узнали, он работает в источнике вашей страницы. 🙂