If variable is blank replace

Asked

Viewed 26 times

1

I have a $name variable

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

And I call her as follows:

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

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

I would like to include the direct () in $name and if it does not return value the not appear anything.

Can someone help me?

2 answers

1


Use a ternary to check for character in $arrayReturn['nome']. If there is, you concatenate the tag with the name, if not, returns nothing '':

<?php
$nome = $arrayReturn['nome'] ? "<p>Nome:  (".str_replace("-"," ",strtolower($arrayReturn['nome'])).")</p>" : '';
?>
<?php echo $nome; ?>
  • Perfect dvd! Thank you!

0

Good night,

Before calling the paragraph add an if

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.