My PHP code is not working properly - php
I´m trying to do some program which would transfer hexa do binary. Problem is in changing of A,B,C,..,F to 10,11,12,...,15 so i can work with them as with numbers. I made this function:
function odstran_pismena($pole)
{
$dlzka = count($pole);
for ($i = 0; $i< $dlzka; $i++)
switch ($pole[$i])
{
case 0: break;
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 5: break;
case 6: break;
case 7: break;
case 8: break;
case 9: break;
case ("A" || "a"): $pole[$i] = 10;
break;
case ("B" || "b"): $pole[$i] = 11;
break;
case ("C" || "c"): $pole[$i] = 12;
break;
case ("D" || "d"): $pole[$i] = 13;
break;
case ("E" || "e"): $pole[$i] = 14;
break;
case ("F" || "f"): $pole[$i] = 15;
break;
default: $pole[$i] = "ERROR";
break;
}
return $pole;
}
First i made array from string, and now i want to change letters to numbers.
I´m testing it with this string: $test = "AbCdEf2345";
I was expecting result 10 11 12 13 14 15 2 3 4 5 but all i have is 10 10 10 10 10 10 2 3 4 5
Am I doing some mystake?(Of course I am, but where?)
("A" || "a") evaluates to boolean value 'true', so all a to f will get caught by the case ("A" || "a" ) and result in 10.
Without using the hexdec() and with minimal change to your code:
function odstran_pismena($pole)
{
$dlzka = count($pole);
for ($i = 0; $i< $dlzka; $i++)
switch ($pole[$i])
{
case 0: break;
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 5: break;
case 6: break;
case 7: break;
case 8: break;
case 9: break;
case "A":
case "a": $pole[$i] = 10;
break;
case "B":
case "b": $pole[$i] = 11;
break;
case "C":
case "c": $pole[$i] = 12;
break;
case "D":
case "d": $pole[$i] = 13;
break;
case "E":
case "e": $pole[$i] = 14;
break;
case "F":
case "f": $pole[$i] = 15;
break;
default: $pole[$i] = "ERROR";
break;
}
return $pole;
}
When you have specified case ("A" || "a"): $pole[$i] = 10; it evaluates to true. So all your chars matched against true returns true. for 'case' conditions avoid using expressions. Use the static values your comparing such as
case "A":
case "a":
$pole[$i] = 10;
break;
An easy way to help you with that would be to use a strtolower:
switch(strtolower($pole[$i])) {
case "a" :...
break;
case "b": ...
break;
}
or simply, as the first line of your function:
$pole = strtolower($pole);
This way you won't have to bother with upper/lower casing.
You should use hexdec() and replace you whole for loop with:
for ($i = 0; $i< $dlzka; $i++)
{
$pole[$i] = hexdec($pole[$i]);
}
Note that you will receive a 0 for non-valid values so you might have to check for that separately if it can happen, using for example is_numeric on the original value.
Related
PHP switch strange behavoiur with zero value and strings
Going immediately to the point: in this code I expect the default case: <?php $a = 0; switch ($a) { case "one": echo "one"; break; case "two": echo "two"; break; default: echo "default"; break; } ?> I get one instead. here expect the zero case ("two"): <?php $a = 0; switch ($a) { case "one": echo "one"; break; case 0: echo "two"; break; default: echo "default"; break; } ?> I get one instead. That appens only with zero value because here I get correctly default: <?php $a = 1; switch ($a) { case "one": echo "one"; break; case "two": echo "two"; break; default: echo "default"; break; } ?> but here I get correctly zero: <?php $a = 0; switch ($a) { case 1: echo "one"; break; case 0: echo "zero"; break; default: echo "default"; break; } ?> Why?
How to use Comma Separated String with switch statement
I have a string $str="1,2,4"; Also I have a switch statement switch ($str_value) { case '0': $finalday='Sunday'; break; case '1': $finalday='Monday'; break; case '2': $finalday='Tuesday'; break; case '3': $finalday='Wednesday'; break; case '4': $finalday='Thursday'; break; case '5': $finalday='Friday'; break; case '6': $finalday='Saturday'; break; default: $finalday=''; } Using this statement from given string I want to show result as Day is : Monday,Tuesday,Thursday how can I do that?
Instead of switch case make an array of days $days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Saturday']; $str="1,2,4"; $val = []; foreach(explode(',',$str) as $value){ $val[] = $days[$value]; } echo implode(',',$val);//Monday,Tuesday,Thursday
Don't use a switch statement. Use preg_replace: $final_day = $str_value; $final_day = preg_replace('/0/', 'Sunday', $final_day); // ... $final_day = preg_replace('/6/', 'Saturday', $final_day); And you have it.
If you really want to use the switch statement, explode the string into an array: myDays = explode(",", $str); Then loop over myDays with the switch statement in the loop.
You can use preg_replace_callback and pass a $days array to it. That'll get the required digit(using the regex) and return day from the $days array. Something like this - $days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday"] preg_replace_callback('/\d+/',function($m)use($days){return $days[$m[0]];}, $str) #OUTPUTS - "Monday,Tuesday,Thursday"
Hope this will help: <?php $str='1,2,3'; $array=explode(',',$str); $finalday=''; foreach($array as $a){ switch ($a) { case '0': $finalday.=',Sunday'; break; case '1': $finalday.=',Monday'; break; case '2': $finalday.=',Tuesday'; break; case '3': $finalday.=',Wednesday'; break; case '4': $finalday.=',Thursday'; break; case '5': $finalday.=',Friday'; break; case '6': $finalday.=',Saturday'; break; default: $finalday=''; } } echo substr($finalday,1);
Update PHP variable in switch statement (simple)
In PHP, I have a value outside of a switch called $chickens, equal to 1. After going through a switch statement, I want the value to change to b. //original value of var $chickens = 1; switch ($chickens) { case "0": $chickens === "a"; break; case "1": $chickens === "b"; break; case "2": $chickens === "c"; break; case "3": $chickens === "b"; break; } //want this to be "b" defined in switch echo $chickens; For some reason the value does not update :(. Any Ideas
You're using the wrong operator. = is the assignment operator. === is a comparison operator: //original value of var $chickens = 1; switch ($chickens) { case "0": $chickens = "a"; break; case "1": $chickens = "b"; break; case "2": $chickens = "c"; break; case "3": $chickens = "d"; break; } //want this to be "b" defined in switch echo $chickens;
PHP Switch with 2 values
is there a way to the php's SWITCH but with 2 values? Here's what I'm looking for switch(a, b){ case 1,2: some code... ; break; case 3,4: some code... ; break; case 3,6: some code... ; break; case 5,2: some code... ; break; case 1,3: some code... ; break; case 8,5: some code... ; break; } I know this won't work, so how would i do something along these lines?
You could use an array with 2 elements since == comparison checks the array values: $a = 3; $b = 6; switch([$a, $b]){ case [1, 2]: echo '1'; break; case [3, 4]: echo '2'; break; case [3, 6]: echo '3'; break; case [5, 2]: echo '4'; break; case [1, 3]: echo '5'; break; case [8, 5]: echo '6'; break; } Outputs 3.
You could use some string instead if it isn't heavy processing: $variable= "1,2"; switch($variable){ case "1,2": some code... ; break; case "3,4": some code... ; break; case "3,6": some code... ; break; case "5,2": some code... ; break; case "1,3": some code... ; break; case "8,5": some code... ; break; }
Switch, same value for multiple case
switch ($i) { case A: $letter = 'first'; break; case B: $letter = 'first'; break; case C: $letter = 'first'; break; case D: $letter = 'second'; break; default: $letter = 'third'; } Is there any way to shorten first three cases? They have the same values inside.
switch ($i) { case A: case B: case C: $letter = 'first'; break; case D: $letter = 'second'; break; default: $letter = 'third'; } Yep there is. If there's no break after a case, the code below the next case is executed too.
switch ($i) { case A: case B: case C: $letter = 'first'; break; case D: $letter = 'second'; break; default: $letter = 'third'; }