Displaying text extracted from XML using PHP simplexml
My preference is to leave the html escaping until the last minute.

Here are some changes you could make to simplify your code.

Code should work but there is no link between $xml and $items, so unable to test.

&lt;?php

$file = 'xml/noticias.xml';

// guard clause exit early, instead of last
if (!file_exists($file)) {
    // do something... throw error message
    throw new \Exception('File does not exist');
}

$xml = simplexml_load_file($file);

foreach ($items as $item) {
    $titulo = $item-&gt;titulo;
    $desc = $item-&gt;desc;
    $url = $item['url'];

    // use mb_ function as you are dealing with unicode text, strlen will report incorrect length
    if (mb_strlen($titulo) &lt; 3) {
        $titulo = 'Título em falta';
    }
    // no need to reassign value to itself
    //else {
    //    $titulo = $titulo;
    //}

    if (mb_strlen($desc) &lt; 3) {
        $desc = 'Descrição em falta';
    }
//    else {
//        $desc = $desc;
//    }

    // not expecting url to be unicode
    if (strlen($url) &gt;= 3) {
        $url = "noticia/{$url}";
    } else {
        $url = null;
    }

    $h3 = $titulo;

//        $h3 = '&lt;h3&gt;' . $titulo . '&lt;/h3&gt;';
//    } else {
//        $h3 = '&lt;a href="noticia/' . $url . '"&gt;&lt;h3&gt;' . $titulo . '&lt;/h3&gt;&lt;/a&gt;';
//    }
    ?&gt;

    &lt;div class="col-lg-3 col-md-6"&gt;
        &lt;div class="item"&gt;
            &lt;div class="content"&gt;
                &lt;?php if ($url): ?&gt;
                    &lt;a href="&lt;?php echo $url; ?&gt;"&gt;
                &lt;?php endif; ?&gt;

                &lt;h3&gt;&lt;?php echo htmlentities($h3); ?&gt;&lt;/h3&gt;

                &lt;?php if ($url): ?&gt;
                    &lt;/a&gt;
                &lt;?php endif; ?&gt;

                &lt;p&gt;&lt;?php echo htmlentities($desc); ?&gt;&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;?php
}