I want to write recursive code using loops instead of recursion function. because recursion take too much time to execute and even fails.This is counting user in both side of binary tree using recursion.i want to achieve this task using loops. please help me to achieve this task.thanks in advance.I will very thank full to you.
function countActiveMembers($mid){
if ($mid == null) {
return 0;
}
$c = 0;
include("conn.php");
$query = $conn->prepare('SELECT leftm, rightm, package_choose FROM member WHERE user_id = ?');
$query->bind_param('s', $mid);
$query->execute();
$query->bind_result($leftm, $rightm, $package_choosen);
if ($query->fetch()) {
$query->close();
$conn->close();
if ($package_choosen == 1) {
$c = 0;
} else {
$c = 1;
}
if (isset($leftm) == false && isset($rightm) == false) {
return $c;
}
if (isset($leftm)) {
$c = $c + countActiveMembers($leftm);
}
if (isset($rightm)) {
$c = $c + countActiveMembers($rightm);
}
} else {
$query->close();
$conn->close();
}
return $c;
}
function countLeftActive($mid){
if ($mid == null) {
return 0;
}
$c = 0;
include("conn.php");
$query = $conn->prepare('SELECT leftm, rightm, package_choose FROM member WHERE user_id = ?');
$query->bind_param('s', $mid);
$query->execute();
$query->bind_result($leftm, $rightm, $package_choosen);
if ($query->fetch()) {
$query->close();
$conn->close();
if (isset($leftm) == false && isset($rightm) == false) {
return 0;
}
if (isset($leftm)) {
$c = $c + countActiveMembers($leftm);
}
} else {
$query->close();
$conn->close();
}
return $c;
}
function countRightActive($mid){
if ($mid == null) {
return 0;
}
$c = 0;
include("conn.php");
$query = $conn->prepare('SELECT leftm, rightm, package_choose FROM member WHERE user_id = ?');
$query->bind_param('s', $mid);
$query->execute();
$query->bind_result($leftm, $rightm, $package_choosen);
if ($query->fetch()) {
$query->close();
$conn->close();
if (isset($leftm) == false && isset($rightm) == false) {
return 0;
}
if (isset($rightm)) {
$c = $c + countActiveMembers($rightm);
}
} else {
$query->close();
$conn->close();
}
return $c;
}
I hope you can appreciate that this is difficult to test, so hopefully you can understand it enough to help.
One of the big performance issues in any system is file/database access and opening and closing connections etc. is always a slow process. This routine loads all the members in the start and passes the data around rather than continually using the database...
function countActiveMembers( $members, $mid){
if ($mid == null) {
return 0;
}
$c = 0;
// Fetch the data from the $members list, using $mid as the index
$leftm = $members[$mid]['leftm'];
$rightm = $members[$mid]['rightm'];
$package_choosen = $members[$mid]['package_choose'];
if ($package_choosen == 1) {
$c = 0;
} else {
$c = 1;
}
if (isset($leftm) == false && isset($rightm) == false) {
return $c;
}
if (isset($leftm)) {
$c = $c + countActiveMembers($members, $leftm);
}
if (isset($rightm)) {
$c = $c + countActiveMembers($members, $rightm);
}
return $c;
}
function countLeftActive($members, $mid){
if ($mid == null) {
return 0;
}
$c = 0;
$leftm = $members[$mid]['leftm'];
$rightm = $members[$mid]['rightm'];
if (isset($leftm) == false && isset($rightm) == false) {
return 0;
}
if (isset($leftm)) {
$c = $c + countActiveMembers($members, $leftm);
}
return $c;
}
function countRightActive($members, $mid){
if ($mid == null) {
return 0;
}
$c = 0;
$leftm = $members[$mid]['leftm'];
$rightm = $members[$mid]['rightm'];
if (isset($leftm) == false && isset($rightm) == false) {
return 0;
}
if (isset($rightm)) {
$c = $c + countActiveMembers($members, $rightm);
}
return $c;
}
// Use your own database credentials
$conn = mysqli_connect("172.17.0.3", "root","a177fgvTRw", "test" );
$result = $conn->query('SELECT user_id, leftm, rightm, package_choose
FROM member');
$members = [];
// Read all the members in and index them by the user_id
while ($row = $result->fetch_assoc()) {
$members[$row["user_id"]] = $row;
}
// Not entirely sure how you use it,but this shows passing the members into the start function
echo countActiveMembers($members, 1);
Related
I need help to check if variables (from a database) are the same, like a test.
First I was using just "==", but I saw this: http://php.net/manual/en/language.operators.comparison.php , and now I'm using "===". But it still doesn't work.
My code:
$uids = array();
$trues = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($uids, $row['UID']);
} while ($row1 = mysqli_fetch_assoc($result1)) {
array_push($uids, $row1['UID']);
} while ($row2 = mysqli_fetch_assoc($result2)) {
array_push($uids, $row2['UID']);
}
for ($i = 0; $i < count($uids); $i++) {
$r = 0;
if ($row['question1'] === $row['uAnswer1']) {
$r++;
} else {
$r = $r;
} if ($row['question2'] === $row['uAnswer2']) {
$r++;
} else {
$r = $r;
} if ($row['question3'] === $row['uAnswer3']) {
$r++;
} else {
$r = $r;
} if ($row['question4'] === $row['uAnswer4']) {
$r++;
} else {
$r = $r;
} if ($row['question5'] === $row['uAnswer5']) {
$r++;
} else {
$r = $r;
} if ($row['question6'] === $row['uAnswer6']) {
$r++;
} else {
$r = $r;
} if ($row['question7'] === $row['uAnswer7']) {
$r++;
} else {
$r = $r;
} if ($row['question8'] === $row['uAnswer8']) {
$r++;
} else {
$r = $r;
} if ($row['question9'] === $row['uAnswer9']) {
$r++;
} else {
$r = $r;
} if ($row['question10'] === $row['uAnswer10']) {
$r++;
} else {
$r = $r;
}
array_push($trues, $r);
echo $uids[$i] . " [" . $r . "]<br>";
}
print_r($trues);
}
What the result is: https://hastebin.com/uxitoyoyib.php
So it actually says everything is right, but I know that it isn't. Can you help me with this?
Thanks!
Compare variables like
if ( strtolower(trim($row['question5'])) === strtolower(trim($row['uAnswer5'])) )
Remove spaces if added with strings, convert the string in lowercase to the exact match of both strings.
use
if ($row['question4'] == $row['uAnswer4']) {
//do something
}
and try to print your 2 variable.
You can use the strcmp() or strcasecmp() functions or the === operator.
I'm trying to figure out how to make this code work.
I input some text via variable into code:
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
You will see that for the Genome there is a Nn and for the Fenome there is a Ee
Upon this happening I need it to give a 50/50 chance for a compared result to be EITHER Ee OR Nn - The code I have right now can only check them individually (so sometimes I ger Ee AND Nn, other times I will get ee and nn) and I feel there could be a much easier method of achieving this than what I'm trying:
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
if ($gepieces[0] === $fepieces[0]) {
$ss = $gepieces[0];
} else {
$ss = rand(1,2);
if ($ss == 1) {
$ss = $gepieces[0];
} else {
$ss = $fepieces[0];
}
}
if ($gepieces[1] === $fepieces[1]) {
$ee = $gepieces[1];
} else {
$ee = rand(1,2);
if ($ee == 1) {
$ee = $gepieces[1];
} else {
$ee = $fepieces[1];
}
}
if ($gepieces[2] === $fepieces[2]) {
$ff = $gepieces[2];
} else {
$ff = rand(1,2);
if ($ff == 1) {
$ff = $gepieces[2];
} else {
$ff = $fepieces[2];
}
}
if ($gepieces[3] === $fepieces[3]) {
$nn = $gepieces[3];
} else {
$nn = rand(1,2);
if ($nn == 1) {
$nn = $gepieces[3];
} else {
$nn = $fepieces[3];
}
}
if ($gepieces[4] === $fepieces[4]) {
$oo = $gepieces[4];
} else {
$oo = rand(1,2);
if ($oo == 1) {
$oo = $gepieces[4];
} else {
$oo = $fepieces[4];
}
}
echo $cubgender." - ".$ss."/".$ee."/".$ff."/".$nn."/".$oo."<br/>";
}
I'm a colossal idiot!
Figured out my own issue
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
if ($genome === $fenome) {
$cubgeno = $genome;
} else {
$cubgeno = rand(1,2);
if ($cubgeno == 1) {
$cubgeno = $genome;
} else {
$cubgeno = $fenome;
}
}
echo $cubgender." - ".$cubgeno."<br/>";
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
$sequence = "";
for ($i = 0;$i < count($fepieces); $i++) {
rand(1,2) == 1 ? $sequence .= $gepieces[$i] : $sequence .= $fepieces[$i];
}
echo $sequence;
I implemented a vote system for my users on a forumbundle. It works fine but I would like to restrinct and just let vote 1 time for user. So I create a Boolean voted on my bdd.
But I don't know how should I can avoid that user's can vote again but at the same time I'd like to let them undo the vote.
So I made this function :
public function ScoreAction(Request $request){
$em = $this->getDoctrine()->getManager();
$idTopic = $request->request->get('id_topic');
$idPoster = $request->request->get('id_poster');
$positive= $request->request->get('positive');
$negative= $request->request->get('negatiu');
$user= $em->getRepository(User::class)->findOneById($idPoster);
$topic = $em->getRepository(Topic::class)->findOneById($idTopic);
$score= $user->getReputation();
$voted = $user->getVoted();
if ($positive!= null) {
$score= $score+ 1;
$voted = 1;
}
if($negative!= null){
$score= $score- 1;
$voted = 1;
}
$user->setReputation($score);
$user->setVoted($voted);
$em->persist($user);
$em->flush();
$redirect = $this->generateUrl('discutea_forum_post', array('slug' => $topic->getSlug()));
return $this->redirect($redirect);
}
Update with change boolean for int nullable on my Database and made this code:
$score = $user->getReputation();
$voted = $user->getVoted();
if ($voted == null) {
if ($positive != null) {
$score = $score + 1;
$voted = 1;
}
if($negative != null){
$score = $score - 1;
$voted = 0;
}
}else if($voted == 1){
if ($negative != null) {
$score = $score - 1;
$voted = 0;
}
}else if($voted == 0){
if ($positive != null) {
$score = $score + 1;
$voted = 1;
}
}
$user->setReputation($score);
$em->persist($user);
$em->flush();
$voted = $user->getVoted();
if ($voted) {
//just redirect somewhere else, without making an actual vote
} else {
//do your vote stuff
//persist and flush etc
//redirect somewhere else
}
something along those lines ?
are you trying to achieve the following ?:
$voted = $user->getVoted();
if ($voted) {
if ($positive!= null) {
$user->setReputation($user->setReputation()+2);
}
if ($negative!= null){
$user->setReputation($user->setReputation()-2);
}
} else {
if ($positive!= null) {
$user->setReputation($user->setReputation()+1);
}
if ($negative!= null){
$user->setReputation($user->setReputation()-1);
}
}
EDIT:
try this
if ($voted === null) {
if ($positive != null) {
$score = $score + 1;
}
if($negative != null){
$score = $score - 1;
}
}else if($voted === 1){
if ($negative != null) {
$score = $score - 1;
}
}else if($voted === 0){
if ($positive != null) {
$score = $score + 1;
}
}
function nonrecgen($min, $max, $amount) {
for($i=0;$i<$amount;$i++) {
$NrArray[$i] = rand($min,$max);
echo $NrArray[$i];
do {
for($j=0;$j<=$i;$j++) {
if ($NrArray[$j] == $NrArray[$i]) {
$NrArray[$i] = rand($min,$max); }
}
$Reccuring = false;
if ($i > 0) {
for($k=0;$k<=$i;$k++) {
if ($NrArray[$k] == $NrArray[$i]) {
$Reccuring = true; }
}
}
}
while ($Reccuring = true);
}
Return $NrArray;
}
$Test = nonrecgen(0,1,2);
print_r($Test);
I wanted to look into how to generate an array of nonreccuring numbers and while this is certainly not the most efficient way I believe, I can't seem to figure out why it loops endlessly on the first iteration. I tried logical analysis over and over, but there has to be something I'm missing.
do {
...
} while ($Reccuring = true);
Because your while statement sets $Reccuring to true, instead of evaluating it.
Try:
do {
...
} while ($Reccuring === true);
Other than the = to == you were also resetting the $Recurring in the wrong place:
<?
function nonrecgen($min, $max, $amount)
{
for($i=0;$i<$amount;$i++)
{
$NrArray[$i] = rand($min,$max);
do
{
for($j=0;$j<=$i;$j++)
{
if ($NrArray[$j] == $NrArray[$i])
{
$NrArray[$i] = rand($min,$max);
}
}
if ($i > 0)
{
for($k=0;$k<=$i;$k++)
{
if ($NrArray[$k] == $NrArray[$i])
{
$Reccuring = true;
}
}
}
$Reccuring = false;
}
while ($Reccuring == true);
}
return $NrArray;
}
$Test = nonrecgen(0,2,5);
echo "<pre>";
print_r($Test);
?>
You're currently assigning a value rather than checking (which will always be true).
Change it to: while ($Reccuring == true);
This is a custom encryption library. I do not know much about PHP's standard library of functions and was wondering if the following code can be improved in any way. The implementation should yield the same results, the API should remain as it is, but ways to make is more PHP-ish would be greatly appreciated.
Code
<?php
/***************************************
Create random major and minor SPICE key.
***************************************/
function crypt_major()
{
$all = range("\x00", "\xFF");
shuffle($all);
$major_key = implode("", $all);
return $major_key;
}
function crypt_minor()
{
$sample = array();
do
{
array_push($sample, 0, 1, 2, 3);
} while (count($sample) != 256);
shuffle($sample);
$list = array();
for ($index = 0; $index < 64; $index++)
{
$b12 = $sample[$index * 4] << 6;
$b34 = $sample[$index * 4 + 1] << 4;
$b56 = $sample[$index * 4 + 2] << 2;
$b78 = $sample[$index * 4 + 3];
array_push($list, $b12 + $b34 + $b56 + $b78);
}
$minor_key = implode("", array_map("chr", $list));
return $minor_key;
}
/***************************************
Create the SPICE key via the given name.
***************************************/
function named_major($name)
{
srand(crc32($name));
return crypt_major();
}
function named_minor($name)
{
srand(crc32($name));
return crypt_minor();
}
/***************************************
Check validity for major and minor keys.
***************************************/
function _check_major($key)
{
if (is_string($key) && strlen($key) == 256)
{
foreach (range("\x00", "\xFF") as $char)
{
if (substr_count($key, $char) == 0)
{
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
function _check_minor($key)
{
if (is_string($key) && strlen($key) == 64)
{
$indexs = array();
foreach (array_map("ord", str_split($key)) as $byte)
{
foreach (range(6, 0, 2) as $shift)
{
array_push($indexs, ($byte >> $shift) & 3);
}
}
$dict = array_count_values($indexs);
foreach (range(0, 3) as $index)
{
if ($dict[$index] != 64)
{
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
/***************************************
Create encode maps for encode functions.
***************************************/
function _encode_map_1($major)
{
return array_map("ord", str_split($major));
}
function _encode_map_2($minor)
{
$map_2 = array(array(), array(), array(), array());
$list = array();
foreach (array_map("ord", str_split($minor)) as $byte)
{
foreach (range(6, 0, 2) as $shift)
{
array_push($list, ($byte >> $shift) & 3);
}
}
for ($byte = 0; $byte < 256; $byte++)
{
array_push($map_2[$list[$byte]], chr($byte));
}
return $map_2;
}
/***************************************
Create decode maps for decode functions.
***************************************/
function _decode_map_1($minor)
{
$map_1 = array();
foreach (array_map("ord", str_split($minor)) as $byte)
{
foreach (range(6, 0, 2) as $shift)
{
array_push($map_1, ($byte >> $shift) & 3);
}
}
return $map_1;
}function _decode_map_2($major)
{
$map_2 = array();
$temp = array_map("ord", str_split($major));
for ($byte = 0; $byte < 256; $byte++)
{
$map_2[$temp[$byte]] = chr($byte);
}
return $map_2;
}
/***************************************
Encrypt or decrypt the string with maps.
***************************************/
function _encode($string, $map_1, $map_2)
{
$cache = "";
foreach (str_split($string) as $char)
{
$byte = $map_1[ord($char)];
foreach (range(6, 0, 2) as $shift)
{
$cache .= $map_2[($byte >> $shift) & 3][mt_rand(0, 63)];
}
}
return $cache;
}
function _decode($string, $map_1, $map_2)
{
$cache = "";
$temp = str_split($string);
for ($iter = 0; $iter < strlen($string) / 4; $iter++)
{
$b12 = $map_1[ord($temp[$iter * 4])] << 6;
$b34 = $map_1[ord($temp[$iter * 4 + 1])] << 4;
$b56 = $map_1[ord($temp[$iter * 4 + 2])] << 2;
$b78 = $map_1[ord($temp[$iter * 4 + 3])];
$cache .= $map_2[$b12 + $b34 + $b56 + $b78];
}
return $cache;
}
/***************************************
This is the public interface for coding.
***************************************/
function encode_string($string, $major, $minor)
{
if (is_string($string))
{
if (_check_major($major) && _check_minor($minor))
{
$map_1 = _encode_map_1($major);
$map_2 = _encode_map_2($minor);
return _encode($string, $map_1, $map_2);
}
}
return FALSE;
}
function decode_string($string, $major, $minor)
{
if (is_string($string) && strlen($string) % 4 == 0)
{
if (_check_major($major) && _check_minor($minor))
{
$map_1 = _decode_map_1($minor);
$map_2 = _decode_map_2($major);
return _decode($string, $map_1, $map_2);
}
}
return FALSE;
}
?>
This is a sample showing how the code is being used. Hex editors may be of help with the input / output.
Example
<?php
# get and process all of the form data
# $input = htmlspecialchars($_POST["input"]);
# $majorname = htmlspecialchars($_POST["majorname"]);
# $minorname = htmlspecialchars($_POST["minorname"]);
# $majorkey = htmlspecialchars($_POST["majorkey"]);
# $minorkey = htmlspecialchars($_POST["minorkey"]);
# $output = htmlspecialchars($_POST["output"]);
# process the submissions by operation
# CREATE
# $operation = $_POST["operation"];
if ($operation == "Create")
{
if (strlen($_POST["majorname"]) == 0)
{
$majorkey = bin2hex(crypt_major());
}
if (strlen($_POST["minorname"]) == 0)
{
$minorkey = bin2hex(crypt_minor());
}
if (strlen($_POST["majorname"]) != 0)
{
$majorkey = bin2hex(named_major($_POST["majorname"]));
}
if (strlen($_POST["minorname"]) != 0)
{
$minorkey = bin2hex(named_minor($_POST["minorname"]));
}
}
# ENCRYPT or DECRYPT
function is_hex($char)
{
if ($char == "0"):
return TRUE;
elseif ($char == "1"):
return TRUE;
elseif ($char == "2"):
return TRUE;
elseif ($char == "3"):
return TRUE;
elseif ($char == "4"):
return TRUE;
elseif ($char == "5"):
return TRUE;
elseif ($char == "6"):
return TRUE;
elseif ($char == "7"):
return TRUE;
elseif ($char == "8"):
return TRUE;
elseif ($char == "9"):
return TRUE;
elseif ($char == "a"):
return TRUE;
elseif ($char == "b"):
return TRUE;
elseif ($char == "c"):
return TRUE;
elseif ($char == "d"):
return TRUE;
elseif ($char == "e"):
return TRUE;
elseif ($char == "f"):
return TRUE;
else:
return FALSE;
endif;
}
function hex2bin($str)
{
if (strlen($str) % 2 == 0):
$string = strtolower($str);
else:
$string = strtolower("0" . $str);
endif;
$cache = "";
$temp = str_split($str);
for ($index = 0; $index < count($temp) / 2; $index++)
{
$h1 = $temp[$index * 2];
if (is_hex($h1))
{
$h2 = $temp[$index * 2 + 1];
if (is_hex($h2))
{
$cache .= chr(hexdec($h1 . $h2));
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
return $cache;
}
if ($operation == "Encrypt" || $operation == "Decrypt")
{
# CHECK FOR ANY ERROR
$errors = array();
if (strlen($_POST["input"]) == 0)
{
$output = "";
}
$binmajor = hex2bin($_POST["majorkey"]);
if (strlen($_POST["majorkey"]) == 0)
{
array_push($errors, "There must be a major key.");
}
elseif ($binmajor == FALSE)
{
array_push($errors, "The major key must be in hex.");
}
elseif (_check_major($binmajor) == FALSE)
{
array_push($errors, "The major key is corrupt.");
}
$binminor = hex2bin($_POST["minorkey"]);
if (strlen($_POST["minorkey"]) == 0)
{
array_push($errors, "There must be a minor key.");
}
elseif ($binminor == FALSE)
{
array_push($errors, "The minor key must be in hex.");
}
elseif (_check_minor($binminor) == FALSE)
{
array_push($errors, "The minor key is corrupt.");
}
if ($_POST["operation"] == "Decrypt")
{
$bininput = hex2bin(str_replace("\r", "", str_replace("\n", "", $_POST["input"])));
if ($bininput == FALSE)
{
if (strlen($_POST["input"]) != 0)
{
array_push($errors, "The input data must be in hex.");
}
}
elseif (strlen($bininput) % 4 != 0)
{
array_push($errors, "The input data is corrupt.");
}
}
if (count($errors) != 0)
{
# ERRORS ARE FOUND
$output = "ERROR:";
foreach ($errors as $error)
{
$output .= "\n" . $error;
}
}
elseif (strlen($_POST["input"]) != 0)
{
# CONTINUE WORKING
if ($_POST["operation"] == "Encrypt")
{
# ENCRYPT
$output = substr(chunk_split(bin2hex(encode_string($_POST["input"], $binmajor, $binminor)), 58), 0, -2);
}
else
{
# DECRYPT
$output = htmlspecialchars(decode_string($bininput, $binmajor, $binminor));
}
}
}
# echo the form with the values filled
echo "<P><TEXTAREA class=maintextarea name=input rows=25 cols=25>" . $input . "</TEXTAREA></P>\n";
echo "<P>Major Name:</P>\n";
echo "<P><INPUT id=textbox1 name=majorname value=\"" . $majorname . "\"></P>\n";
echo "<P>Minor Name:</P>\n";
echo "<P><INPUT id=textbox1 name=minorname value=\"" . $minorname . "\"></P>\n";
echo "<DIV style=\"TEXT-ALIGN: center\"><INPUT class=submit type=submit value=Create name=operation>\n";
echo "</DIV>\n";
echo "<P>Major Key:</P>\n";
echo "<P><INPUT id=textbox1 name=majorkey value=\"" . $majorkey . "\"></P>\n";
echo "<P>Minor Key:</P>\n";
echo "<P><INPUT id=textbox1 name=minorkey value=\"" . $minorkey . "\"></P>\n";
echo "<DIV style=\"TEXT-ALIGN: center\"><INPUT class=submit type=submit value=Encrypt name=operation> \n";
echo "<INPUT class=submit type=submit value=Decrypt name=operation> </DIV>\n";
echo "<P>Result:</P>\n";
echo "<P><TEXTAREA class=maintextarea name=output rows=25 readOnly cols=25>" . $output . "</TEXTAREA></P></DIV></FORM>\n";
?>
What should be editted for better memory efficiency or faster execution?
You could replace your isHex function with:
function isHex($char) {
return strpos("0123456789ABCDEF",strtoupper($char)) > -1;
}
And your hex2bin might be better as:
function hex2bin($h)
{
if (!is_string($h)) return null;
$r='';
for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
return $r;
}
You seem to have a lot of if..elseif...elseif...elseif which would be a lot cleaner in a switch or seperated into different methods.
However, I'm talking more from a maintainability and readability perspective - although the easier it is to read and understand, the easier it is to optimize. If it would help you if I waded through all the code and wrote it in a cleaner way, then I'll do so...