Truth Table Output in PHP

I came across this truth table generation site and tried to simulate it in PHP (I understand that the source code is available, but I know 0 perl).

Now my question is not in evaluating the expression, but in how to display the table so that each combination of T and F for the variables is displayed

For example, with three variables, the table would look like this:

a | b | c ----------- T | T | TT | T | FT | F | TT | F | FF | T | TF | T | FF | F | TF | F | F 

and with 4 variables.

 a | b | c | d ------------- T | T | T | T T | T | T | F T | T | F | T T | T | F | F T | F | T | T T | F | T | F T | F | F | T T | F | F | F F | T | T | T F | T | T | F F | T | F | T F | T | F | F F | F | T | T F | F | T | F F | F | F | T F | F | F | F 

What is the logic / pattern for creating it?

+6
source share
7 answers

What about this recursive function? It returns a 2-dimensional array in which each row has $count elements. You can use this to create your table.

 function getTruthValues($count) { if (1 === $count) { // true and false for the first variable return array(array('T'), array('F')); } // get 2 copies of the output for 1 less variable $trues = $falses = getTruthValues(--$count); for ($i = 0, $total = count($trues); $i < $total; $i++) { // the true copy gets a T added to each row array_unshift($trues[$i], 'T'); // and the false copy gets an F array_unshift($falses[$i], 'F'); } // combine the T and F copies to give this variable output return array_merge($trues, $falses); } function toTable(array $rows) { $return = "<table>\n"; $headers = range('A', chr(64 + count($rows[0]))); $return .= '<tr><th>' . implode('</th><th>', $headers) . "</th></tr>\n"; foreach ($rows as $row) { $return .= '<tr><td>' . implode('</td><td>', $row) . "</td></tr>\n"; } return $return . '</table>'; } echo toTable(getTruthValues(3)); 

EDIT : Codepad with added function to convert an array to a table.

+4
source
 $nbBooleans = 5; // change to whatever you want // show header for($i = 0; $i < $nbBooleans ; $i++) { if ($i > 0) echo " | "; echo chr($i + 65); // 1 => A, 2 => B etc. } // separator line (dynamic size) echo "\n".str_repeat("-", ($nbBooleans - 1) * 3 + $nbBooleans)."\n"; // show combinations $nbInterations = pow(2, $nbBooleans); for($i = 0; $i < $nbInterations; $i++) { for ($iBit = 0; $iBit < $nbBooleans; $iBit++) { if ($iBit > 0) echo " | "; echo (($i & pow(2, $iBit)) != 0 ? 'Y' : 'N'); } echo "\n"; } 
+2
source

You can do something like this:

 echo "<table>"; for($a=0; $a<=1; $a++){ for($b=0; $b<=1; $b++){ for($c=0; $c<=1; $c++){ for($d=0; $d<=1; $d++){ echo "<tr><td>$a</td><td>$b</td><td>$c</td><td>$d</td></tr>"; } } } } echo "</table>"; 

it might be a bit overkill, but it works ...

+1
source

First you must calculate the number of combinations, for example, if you have 4 variables, then you will have 16 combinations. For each variable, take your index, name it i. In this column of the variable you will have (n / (2 ^ i)) the switching time of the groups T and F, where n are the total combinations of the possible values โ€‹โ€‹of the variables.

  <?php $numberOfVariables = 5; $totalCombinations = pow(2, $numberOfVariables); for ($i = 0; $i < $numberOfVariables; $i++) { $subGroupCount = $totalCombinations / pow(2, $i); $lettersPerGroup = $totalCombinations / $subGroupCount; $toggler = true; for($j=0; $j<$subGroupCount; $j++) { for($k=0; $k < $lettersPerGroup; $k++) $array[$i][$j*$lettersPerGroup + $k] = ($toggler ? "T" : "F"); $toggler = !$toggler; } } echo("<table border='1' bgcolor='yellow'>"); for ($k=0;$k<$totalCombinations;$k++) { echo("<tr>"); for ($j=$numberOfVariables-1;$j>=0;$j--) echo("<td>".$array[$j][$k]."</td>"); echo("</tr>"); } echo("</table>"); ?> 
+1
source

Made a small function for this:

 function printTruth($vars) { $rows = array(); $max = pow(2, $vars); // Lines and Letters $arr = array(); for($i=97;$i<(97+$vars);$i++) { $arr[] = chr($i); } $rows[] = implode(' | ', $arr); $rows[] = str_repeat('-', $vars*3); // Variables for($i=0;$i<$max;$i++) { $oneRow = ''; for ($j=0;$j<$vars;$j++) { if($j>0) $oneRow .= " | "; $oneRow .= (($i & pow(2,$j)) != 0 ? 'T' : 'F'); } $rows[] = strrev($oneRow); } return implode("<br>", $rows); } echo printTruth(3); 
+1
source

Try this code. generate ($ numberOfVariables) returns an array with a truth table. Each item is a string that you can iterate through

 <? function generate($var=3){ $number= pow(2,$var)-1; $array=array(); while($number>=0){ $str=decbin($number); $number--; while(strlen($str)<$var) { $str="0".$str; } array_push($array,$str); } return $array; }?> <pre> <?print_r(array_reverse(generate(3))); ?> </pre> 
+1
source

this approach may be ugly, but it seems universal :)

 $length = 3; for($i=0;$i<pow(2,$length);$i++){ $bin = decbin($i); _add($bin, $length); _out($bin, $length); } function _out($str, $length){ for ($i=0; $i<$length; $i++) echo ($str[$i] == 0 ? 'F' : 'T')."\t"; echo "\n"; } function _add(&$bin, $length){ $add = ''; if (strlen($bin) < $length){ for($j=0;$j<($length - strlen($bin));$j++){ $add.='0'; } $bin = $add.$bin; } } 

the output is as follows

 FFFFFTFTFFTTTFFTFTTTF TTT 
+1
source

Source: https://habr.com/ru/post/908498/


All Articles