ERROR when bringing an image with PHP for a HERO of a web

0

I am getting the following error when I bring a URL with PHP in local to show an image of a "hero". It seems that I am using something obsolete. I've been googling but I do not really understand where the problem is. Can somebody help me?

[Deprecation] Resource requests whose URLs are both removed whitespace ( \n , \r , \t ) characters and less-than characters ( < ) are blocked. Please remove newlines and encode less-than characters from places like element attribute values in order to load these resources. See link for more details.

This would be the code I'm using for the hero (in a separate template):

<div class="container pt-4">
    <div class="row no-gutters">
        <div class="col-12 hero">
          <img src="img/<?php echo ($imagen) ? $imagen : $titulo . '.jpg';  ?>" class="img-fluid">
          <h2 class="text-uppercase d-none d-md-block"><?php echo $titulo; ?></h2>
        </div>
    </div>
</div>

And this is the beginning of the code of the page (We) where I get the error.

 <?php
    $titulo = 'nosotros';
    include 'templates/header.php';
    include 'templates/hero.php';
?>


<div class="container pt-4">
    <div class="row">
        <main class="col-lg-8 contenido-principal">
          <h2 class="d-block d-md-none text-uppercase text-center"><?php echo $titulo; ?></h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed convallis ac risus sit amet condimentum. Duis pellentesque vitae erat a varius. Donec tincidunt, risus sit amet varius tincidunt, turpis arcu ullamcorper ligula, at feugiat turpis massa vitae erat. Nam sit amet posuere urna. Mauris consequat elit in tellus fringilla, in mollis dolor feugiat. Vivamus fringilla eros sed leo maximus rhoncus. Phasellus sit amet vehicula diam. Pellentesque ut lorem ex. Donec sed accumsan velit.</p>

Comment that the name of the image that I want to show in .jpg is called the same as the title of the page to save in code.

    
asked by Edu 18.01.2018 в 17:59
source

1 answer

0

You need to check if there is a variable $imagen first of all, this line in particular:

<?php echo ($imagen) ? $imagen : $titulo . '.jpg';

The problem is precisely that while ($imagen) ? % checks if the variable is true or false no way of knowing before whether or not there. This technique works as long as your errors are suppressed, but due to the level of errors reported by PHP, the result is a Notice .

The solution is to use the function isset() to know if the variable exists before checking any other condition with it:

<?php echo isset($imagen) ? $imagen : $titulo . '.jpg';

The other option (which is not recommended during development) is to suppress the errors:

<?php
// Desactivar toda notificación de error
error_reporting(0);

// Notificar solamente errores de ejecución
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Notificar todos los errores excepto E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);
    
answered by 20.01.2018 в 22:11