Search code examples
phpif-statementtextareais-empty

Using empty() to adjust output with multiple variables


I have two primary variables that are composed of strings and other variables. I want the two primary variables only to be echo'ed if all the variables that they are comprised of have data.

The two primary variables are $introduction and colortxt.

$introduction is comprised of $finalvehicle3, $bodystyle, $mileage, and $hi.

$colortxt is comprised of $model, $exterior, and $interiorspec.

If any of the secondary variables are empty, I don't want the primary variable to be displayed.

Below is the code I have created that doesn't seem to be working. I have been using empty().

My PHP:

<?php

 $finalvehicle3 = "Toyota Camry";
 $bodystyle = "sedan";
 $mileage = "30,000";
 $hi = null;
 $model = "Camry";
 $exterior = "red";
 $interiorspec = "black cloth";

 if (empty([$finalvehicle3, $bodystyle, $mileage, $hi]) == true){
  $introduction = "";
  }
 else {
   $introduction = "I am pleased to present this ".$finalvehicle3." ".$bodystyle." with ".$mileage." miles.";
  }

  if (empty([$model, $exterior, $interiorspec]) == true){
   $colortxt = "";
  }
  else {
  $colortxt = "This ".$model." is finished in ".$exterior." with a ".$interiorspec. " interior.";
  }

  echo "<textarea name=''  id='' style='width: 565px;' rows='8' cols='60'>";

    echo  $introduction." ".$colortxt;

  echo "</textarea>";
  echo "<br><br>";



 ?>

In this case $introduction should not be displayed as $hi = null


Solution

  • I can't get empty([$finalvehicle3, $bodystyle, $mileage, $hi]) to work.

    I was able to use:

    if (empty($hi)
         || empty($finalvehicle3)
         || empty($bodystyle) 
         || empty($mileage)){
      $introduction = "";
     }
    else {
       $introduction = "I am pleased to present this ".$finalvehicle3." ".$bodystyle." 
                        with ".$mileage." miles.";
    }
    

    Will that not work?