If variable is blank replace

1

I have a variable $ name

$nome = str_replace("-"," ",strtolower($arrayReturn['nome']));

And I call it like this:

<p>Nome: (<?php echo $nome; ?>) </p>

It turns out that sometimes this variable does not return value, getting only ().

I would like to include the direct () in $ name and if no value returns the nothing appears.

Can anyone help me?

    
asked by anonymous 27.06.2018 / 03:15

2 answers

1

Use a ternary checking for a character in $arrayReturn['nome'] . If there is, you concatenate the tag with the name, otherwise, return anything '' :

<?php
$nome = $arrayReturn['nome'] ? "<p>Nome:  (".str_replace("-"," ",strtolower($arrayReturn['nome'])).")</p>" : '';
?>
<?php echo $nome; ?>
    
27.06.2018 / 04:16
0

Good evening,

Before calling the paragraph add an if

<?php
$nome = str_replace("-"," ",strtolower($arrayReturn['nome']));

 if ($nome == null){
 $nome = "()";
}
?>

<p>Nome: <?php echo $nome; ?> </p>
    
27.06.2018 / 03:19