How to clear this if / else statement? (refactoring)

I know there should be a better way to do this, but whenever I search for "& &" I don't get good enough results ...

<?php
if (empty($row[ContactName]) && empty($row[ContactEmail]) && empty($row[ContactPhone]) && empty($row[Website])){
echo "Not Provided";
}
else{
...do stuff...
}
?>

Thank!

+5
source share
6 answers

What is wrong with the source code?

<?php
if (empty($row[ContactName]) 
    && empty($row[ContactEmail]) 
    && empty($row[ContactPhone]) 
    && empty($row[Website]))
{
    echo "Not Provided";
} 
else{
 ...do stuff...
}
?>

It seems to me that I need a thin code ...

+5
source
<?php
$i=1;
$ar=array('ContactName','ContactEmail','ContactPhone','Website')

foreach($ar as $a)
  if (empty($row[$a]))
    {
     $i=0;
     break;                      //to make code fast
    }
  if($i)         //do stuff
  else echo 'not provided';
?>

or if you really want to make your code redundant, then change the column name in the database

From               To
ContactName        Col1
ContactEmail       Col2
ContactPhone       Col3
Website            Col4

and then do

 <?php
  $i=1;
  for($a=1;$a<5;$a++)
    if (empty($row['Col'.$a]))
    {
      $i=0;
      break;
    }
  if($i)//do stuff
  else echo 'Not Provided';
    ?>

However, it is not good to rename the column, as this will make your db somewhat less clear.

+5
source

, , , :)

$required = array('ContactName', 'ContactEmail', 'ContactPhone', 'Website');

$ra = array_filter(array_intersect_key($row,array_flip($required)));
if(empty($ra)){
    echo "Not Provided";
}
else{
    //....
}

array_filter empty() "Fatal error: Can not use function return value in write context"

1) array flip $
2) array_intersect_key $row , $required
3) array_filter

, . .

+1

php , , .

:

function empty()
{
    foreach(func_get_args() as $arg) if (empty($arg)) return false;
    return true;
}

:

if (empty($row[ContactName], $row[ContactEmail], $row[ContactPhone], $row[Website])) {
      echo "Not Provided";
}
+1
<?php
$flag = 1;
if (empty($row[ContactName])){
$flag = 0;
}
if (empty($row[ContactEmail])){
$flag = 0;
}
if (empty($row[ContactPhone])){
$flag = 0;
}
if (empty($row[Website])){
$flag = 0;
}

if($flag == 0){
echo "Not Provided";

}else{
//do stuff
}    


?>

$flag = 1;
$i=0;
$mustarray = array('ContactName','ContactName','ContactName','ContactName'); //or any number of fields that you want as mandatory
foreach($yourresult as $row){
   if(empty($row[$mustarray [$i]])){
    $flag = 0;
} 
$i++;
}
    if($flag == 0){
    echo "Not Provided";

    }else{
    //do stuff
    }  
0

, .

<?php
if (empty($row[ContactName]) 
    && empty($row[ContactEmail]) 
    && empty($row[ContactPhone]) 
    && empty($row[Website]))
{
    echo "Not Provided";
} 
else{
 ...do stuff...
}
?>

, , . ,

<?php
    $i=1;
    for($a=1;$a<5;$a++)
    if (empty($row['Col'.$a]))
    {
    $i=0;
    break;
    }
    if($i)//do stuff
    ?>

it may seem small, pleasant, and “professional,” but it has more operations to give the same results as a simple if statement. Without saying that it is impossible to do it faster, I do not know php well, just remember the speed of code execution.

0
source

All Articles