php - mt_rand() returns unexpected output -


i working ad system of site , having trouble this.

<?php  function bdads($size, $company) { if($company == 'nufa') {     if ($size == '300'){         echo 'n300';     }      if ($size == '160'){         echo 'n160';     }     if ($size == '728'){         echo 'n728';     }      if ($size == '700'){         echo 'n700';     } }  if($company == 'gnr') {     if ($size == '300'){         echo 'g300';     }      if ($size == '160'){         echo 'g160';     }      if ($size == '728'){         echo 'g728';     }      if ($size == '700'){         echo 'g700';     } }  }  function bdad($size, $company){ $zsize = $size; if($company == 'nufa'){     echo bdads($zsize, 'nufa'); } if($company == 'gnr'){     echo bdads($zsize, 'gnr'); } if($company == 'both'){     $randomlist = [ bdads($zsize, 'gnr'),  bdads($zsize, 'nufa')];     echo $randomlist[mt_rand(0, count($randomlist) - 1)]; } } ?> 

now, seems fine.. example,

<?php echo bdad(728, 'gnr'); ?> 

returning g728 (as expected)

<?php echo bdad(300, 'nufa'); ?> 

returning n300 (as expected)

but trouble in generating random content.

<?php echo bdad(300, 'both'); ?> 

returning g300n300 want choose either g300 or n300 randomly.

edit:

  • changed $a $randomlist, still same result

i tried simplify functions bit:

you have return values, otherwise functions return null default. can access string array, used $company[0] first letter of company, can concatenate size.

<?php      function bdads($size, $company) {            return $company[0] . $size;     }      function bdad($size, $company){          if($company == "both") {             $randomlist = [bdads($size, "gnr"),  bdads($size, "nufa")];             return $randomlist[mt_rand(0, count($randomlist) - 1)];         } else {             return dads($size, $company);         }      }      echo bdad(300, "both");  ?> 

output:

n300 //or g300 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -