IF with 2 distinct variables for various scenarios

Asked

Viewed 189 times

0

I’m trying to make a condition in php this way:

$Telefone = "";
$Celular = "";

Where:

  • If both empty shows echo sem Telefone e nem Celular
  • If variable Telephone = 222-2222 echo Telefone: 2222-2222
  • If on the contrary echo Celular: 9999-99999
  • If both are not empty it will be echo Telefone: 2222-2222 e Celular: 9999-9999

I couldn’t ride.

<?php   

$Telefone= "Telefone";
$Celular= "";

if (empty($Telefome) and empty($Celular)) {
    echo "Tel=Vazio e Cel=Vazio";
}
else
{
    if (empty($Telefome) and empty($Celular)) {
        echo "so". " " . $Telefone;
    }
    else
    {
        echo $Celular. "e". $Telefone;
    }
}
?>

2 answers

0

A way using if, else if and else:

<?
if(empty($Telefone) && empty($Celular)){

   echo "sem Telefone e nem Celular";

}else if(!empty($Telefone) && empty($Celular)){

   echo "Telefone: ". $Telefone;

}else if(empty($Telefone) && !empty($Celular)){

   echo "Celular: ". $Celular;

}else{

   echo "Telefone: ". $Telefone . " e Celular: ". $Celular;

}
?> 
  • It was very good thank you

  • You are welcome. Just choose one of the answers and mark , you can also vote on the answers you found useful. Abs!

0

<?php
if($Telefone == "" && $Celular == ""):
  echo "Sem contato";
elseif($Telefone != "" && $Celular != ""):
    echo "Telefone:".$Telefone."Celular: ".$Celular;
elseif($Telefone == ""):
    echo "Celular: ".$Celular;
else:
    echo "Telefone: ".$Telefone;
endif;
?>

Browser other questions tagged

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