switch(n){
case "badge01":
case "badge02":
case "badge03":
case "badge04":
case "badge05":
//dosomething
break;
}
Hi above's switch case statement, I would like to use a function to run the multiple loop to generate the case's name, so can i know how to generate with function on a switch case statement like this?
switch(n){
case badgenameloop():
//dosomething
break;
}
And is it possible to do that?
Thanks and sorry for my bad English.
According to PHP Manual
The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type.
I'm afraid you can Not have a Loop for a case statement, hence a waste of time trying.
You can do this for example:
function badgenameloop($key)
{
switch($key){
case "badge01":
case "badge02":
case "badge03":
case "badge04":
case "badge05":
echo "badge 1";
break;
}
}
function badgename2loop($key)
{
switch($key){
case "badge_2_01":
case "badge_2_02":
case "badge_2_03":
case "badge_2_04":
case "badge_2_05":
echo "badge 2";
break;
}
}
$key = "badge_2_01";
switch($key){
case badgenameloop($key): break;
case badgename2loop($key): break;
}
Related
I am trying to bind an unknown number of parameters to a prepared PHP/MySQL statement. I tried the code below. However, it only works with queries with a set number of parameters and I know that there has to be a more efficient way to do so.
switch($numparams){
case 0:
#$stmt->bind_param("");
break;
case 1:
#$stmt->bind_param($paramtypes, $param_arr0);
break;
case 2:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1);
break;
case 3:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2);
break;
case 4:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3);
break;
case 5:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4);
break;
case 6:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5);
break;
case 7:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6);
break;
case 8:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7);
break;
case 9:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7,$param_arr8);
break;
case 10:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7,$param_arr8,$param9);
break;
}
As you can see, it's completely inefficient to code and isn't scalable.
I tried dynamically naming variables in a for-loop based on the number of parameters, but I couldn't figure out how to then insert them into the bind_param without having a separate bind_param statement for each in a switch-case loop (as above). It might just be that there is no way to do this, but I feel there should be one. If either are the case, I appreciate any help.
I think your solution lies in call_user_func_array() array.
First you need a string similar to "ssi...." that is the types of parameters in order.
Code
$types = '';
foreach($param_arr as $param) {
$types.= substr(strtolower(gettype($param)), 0, 1);
}
call_user_func_array(array($stmt, 'bind_param'), array_merge(array($types), $param_arr)));
Reference
http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli
http://php.net/manual/en/function.call-user-func-array.php
Hope this helps you through ;)
I have the following if statement (except the final one would be much longer with more values) that I would like to condense.
if ($row['titleId'] == '123' || $row['titleId'] == '456' || $row[){
I would imagine it would end up something like:
if ($row['titleId'] == ('123'||'456'){}
Or would I be better off like this:
$array = ('123','456')
if (in_array($row['titleId'], $array){}
You can use switch for this:
switch ($row['titleId']) {
case '123': case '456': case '789': case '314': case '271':
doSomething();
}
I'd probably still prefer to have each case on a separate line but, if your goal is to reduce the "height" of your code, you can do it as above.
In terms of shortening the code in your comment, which apparently looks like this:
switch($row['titleId']){
case '8216': case '8678': case '8705': case '8216': case '8707':
$rows[$row['titleId']]=array();
break;
case '8214':
$rows['8216'][]=$row['titleId'];
break;
case '8791':
$rows['8678'][]=$row['titleId'];
break;
case '8643':
$rows['8705'][]=$row['titleId'];
break;
case '8666':
$rows['8707'][]=$row['titleId'];
break;
}
you could opt for something like:
$xlat = array('8214'=>'8216', '8791'=>'8678', '8643'=>'8705', '8666'=>'8707');
switch($row['titleId']){
case '8216': case '8678': case '8705': case '8216': case '8707':
$rows[$row['titleId']]=array(); break;
default:
if (array_key_exists($row['titleId'],$xlat)) {
$rows[$xlat[$row['titleId']]][]=$row['titleId'];
}
}
This compresses the common cases by putting them under the control of an associative array. Basically, if the title ID is in the array as a key, its lookup value will be used to affect the correct $rows entry.
I'm using a switch statement with several cases.
When one of the cases is chosen, how can I randomly choose a block of code within that case, but without using a nested switch statement?
For example:
switch ($choose_case){
case 1:
//some code
break;
case 2:
//some code
break;
case 3:
(random block of code #1)
(random block of code #2)
=randomly choose #1 or #2
break;
}
In don't know about you but i will do it like this.
switch ($choose_case){
case 1:
//some code
break;
case 2:
//some code
break;
case 3:
$var = rand(1,2);
if($var == 1)
{
//case 1
}
else
{
//case 2
}
break;
}
The best way to do that is by attributing $choose_value a random number between 1 and 2 and calling the function that contains the switch again with the new value. So, in your case:
function do_something($choose_case){
switch ($choose_case){
case 1:
//some code
break;
case 2:
//some code
break;
case 3:
$choose_case = floor(rand(1.5,2.5));
do_something($choose_case);
break;
}
Does POST or GET data need to be validated if being thrown directly into a switch() statement? For example:
switch($_GET['input']) {
//...
}
???
If your switch contains real cases no, but if you do default you need to validate the $_GET if you use it in that case.
eg:
switch ($_GET['input']) {
case 'one':
echo "input is one";
break;
case 'two':
echo "input is two";
break;
default:
// here `$_GET['input']` need to be validated, escaped, checked if you use it to avoid XSS or SQL injections
echo $_GET['input'];
}
No, your case is basically the validation.
The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.
Note that switch/case does loose comparision.
<?php
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
default:
echo "";
break;
}
?>
So, the short answer is "NO".
This is something that I haven't seen in the PHPdoc for switch() so I'm not sure if it's possible, but I'd like to have a case which is multi-conditional, such as:
switch($this) {
case "yes" || "maybe":
include "filename.php";
break;
...
}
Is this valid syntax/is this even possible with a switch() statement?
Usually you'd just use case fall-through.
switch($this) {
case "yes":
case "maybe":
include "filename.php";
break;
...
}
Is this valid syntax/is this even possible with a switch() statement?
No and no. The expression will be evaluated as ("yes" or "maybe"), which will result in true. switch will then test against that result.
You want to use
case "yes":
case "maybe":
// some code
break;
Should be
switch($this) {
case "yes":
case "maybe":
include "filename.php";
break;
...
}
You can do this with fall-through:
switch ($this) {
case "yes":
case "no":
include "filename.php";
break;
}
Sure, just specify two cases without breaking the first one, like so:
switch($this) {
case "yes":
case "maybe":
include "filename.php";
break;
...
}
If you don't break a case, then any code for that case is run and continues on to execute additional code until the execution is broken. It will continue through all cases below it until it sees a break.