I have two bit strings, each four bits long.
e.g.,
$role1BitString = '1010';
$role2BitString = '1001';
I want to OR them, so that in this case the result would be:
$bitString = '1011';
Here is the code that I have, but it calls substr eight times.
$selectBit = (substr($role1BitString, RBAC::SELECT_BIT, 1) == '1' || substr($role2BitString, RBAC::SELECT_BIT, 1) == '1') ? '1' : '0' ;
$insertBit = (substr($role1BitString, RBAC::INSERT_BIT, 1) == '1' || substr($role2BitString, RBAC::INSERT_BIT, 1) == '1') ? '1' : '0' ;
$updateBit = (substr($role1BitString, RBAC::UPDATE_BIT, 1) == '1' || substr($role2BitString, RBAC::UPDATE_BIT, 1) == '1') ? '1' : '0' ;
$deleteBit = (substr($role1BitString, RBAC::DELETE_BIT, 1) == '1' || substr($role2BitString, RBAC::DELETE_BIT, 1) == '1') ? '1' : '0' ;
$bitString = $selectBit . $insertBit . $updateBit . $deleteBit;
This code will be executed dozens of times with every user operation. What is the most performant way of doing this?
using an actual int with bitshifting and using the bitwise or operation
use intval($string, 2) to convert your binary string to an int and then use | to do the operation
Just use PHP's bitwise operator |:
$bitString = $role1BitString | $role2BitString;
Related
I am trying to consolidate a few different if statements. What I am trying to accomplish would read something like this:
If (this is true and this is true) OR (this is true and this is true) AND (This is true)
So, one at least one of the first two sets of parentheses would need to be true, and if one of those is true, then also the last set of parentheses would need to be true, in order for the code inside to be executed.
Here is the specific code I am (unsuccessfully) trying to make work:
if(($calc->number % 2 == 1 && $calc->doubleColor == 'b2' | $calc->number % 2 == 0 && $calc->doubleColor = 'r2') && in_array($calc->number, $backToBet)){
}
Is there a way to do this? A possibility? Is there any drawback to getting a lot into a single if statement?
EDIT
$blackMatch = $calc->number % 2 == 1 && $calc->doubleColor == 'b2';
$redMatch = $calc->number % 2 == 0 && $calc->doubleColor = 'r2';
$numberMatch = in_array($calc->number, $backToBet);
if(($blackMatch || $redMatch) && $numberMatch){
}
/ ** Calc->number = 2, $blackMatch = false, $redMatch = false,
$numberMatch array contains 2 **/
Basically what I end with is a 'true' result, even though neither of the conditions within the inner parentheses are satisfied.
to make code easier to read, I'd suggest to use separate variables, like this:
$condition1 = ($calc->number % 2 == 1) && ($calc->doubleColor == 'b2');
$condition2 = ($calc->number % 2 == 0) && ($calc->doubleColor == 'r2');
$condition3 = in_array($calc->number, $backToBet);
if (($condition1 || $condition2) && $condition3) {
}
two things to note:
|| is logical OR, | is bitwise OR
== is comparison, = is assignment
I need a way to be able to automatically add a '0' in front of any number if the number a user enters is a number between 1-9 in PHP. I need this because I am trying to make a 10 character date so that I can search for exercises added to my website between the dates.
The easiest way to do this I thought was to start with a year, then the month, then the day so I get something like this: 2001/04/15. I can just have the user manually add a '0', but I was hoping there was a way to do that automatically.
So far, I have this, but it doesn't work quite right. Can someone help me out here?
Thanks in advance.
//Checks to see if start day is a number 1-9
//If so, adds a '0' at the beginning of the start day variable
if($_POST['eday'] == 1 || $_POST['eday'] == 2 || $_POST['eday'] == 3 || $_POST['eday'] == 4 ||
$_POST['eday'] == 5 || $_POST['eday'] == 6 || $_POST['eday'] == 7 || $_POST['eday'] == 8 ||
$_POST['eday'] == 9) {
$_POST['eday'] = 0 . $_POST['eday'];
echo $_POST['eday'] . '<br>';
}
Use printf formatting:
printf("%02d<br>", $_POST['eday']);
You are complicating yourself for nothing dude, you could do it like that :
if($_POST['eday'] < 10) $number = '0'.$_POST['eday'];
else $number = $_POST['eday'];
Here is another thing: http://www.w3schools.com/php/func_string_str_pad.asp
pad_str($str,20,".");
will pad out to 20 characters of "."
There is and optional 4th parameter.
STR_PAD_BOTH
STR_PAD_RIGHT (Default if not supplied)
STR_PAD_LEFT
so for you:
$str = $_POST['eday'];
pad_str($str,2,"0", STR_PAD_LEFT);
Now $str is set and you can use it where ever you need.
I am working on a project and then I got this problem. Here is the scenario, when I insert in database, it must not insert if the value is zero ,,but then when I check my database ,there are zeros being inserted, I don't know where I go wrong or I just missed the trapping that if the value is equal zero then it must not insert.
Here is the code :
$ref_array = explode(',' , $ref_number);
$po_array = explode(',' , $po_number);
$inv_array = explode(',' , $inv_number);
$asn_array = explode(',' , $asn_number);
$adj_array = explode(',' , $adj_number);
$amount_array = explode(',' , $amount);
// count the number of po,invoice,asn and adj
if(count($po_array) != count($ref_array) || count($inv_array) != count($ref_array) || count($asn_array) != count($ref_array) || count($adj_array) != count($ref_array) || count($ref_array) != count($amount_array)){
foreach ($ref_array as $i => $ref_num){
$po_num = isset($po_array[$i]) ? $po_array[$i] : '' ; //leave blank there is no $po_array[$i]
$inv_num = isset($inv_array[$i]) ? $inv_array[$i] : '';
$asn_num = isset($asn_array[$i]) ? $asn_array[$i] : '' ;
$adj_num = isset($adj_array[$i]) ? $adj_array[$i] : '' ;
$amount_num = isset($amount_array[$i])? $amount_array[$i] : '';
if(intval($ref_num) != 0 ){
$conn->query ("INSERT INTO transaction_detail (`transaction_id`,`ref_number`,`po_number`,`inv_number`,`asn_number`,`adj_number`,`amount`)
VALUES ('$transaction_id','$ref_num','$po_num','$inv_num','$asn_num','$adj_num','$amount_num') " );
}
}
}
Can somebody help me?
You just remove intval form your condition because it is use to Get the integer value of a variable.
Second check $ref_num variable is greater then zero
you condition would be
if(($ref_num) >0 ){
Also read intval()
Check the transaction_detail table whether you have put default value as 0 for po_number field.
I am trying to do:
if(x != 1 || 2) echo 'okay';
With my code here:
if($_POST["timezone"] != ("Pacific/Midway" || "America/Adak" || "Etc/GMT+10" || "Pacific/Marquesas")) {
$timezone_error = 'Invalid timezone';
}
Whereas I put in information that did not equal, and $timezone_error was still not set, what is the proper OR operator that I should be using, or is this possible at all? I would rather not write $_POST['x'] != 1, $_POST['x'] != 2 all out separately as this is quite a long list.
what you want is something like this
$array = array("Pacific/Midway" , "America/Adak" , "Etc/GMT+10" , "Pacific/Marquesas");
if (!in_array($_POST["timezone"], $array){
$timezone_error = 'Invalid timezone';
}
Correct format would be:
if(x!= 1 || x!=2) echo 'okay';
I am trying to convert the following code into a Ternary Operator, but it is not working and I am unsure why. I think my problem is that I do not know how to express the elseif operation in ternary format. From my understanding and elseif is performed the same way as an if operation by using the format : (condition) ? 'result'.
if ($i == 0) {
$top = '<div class="active item">';
} elseif ($i % 5 == 0) {
$top = '<div class="item">';
} else {
$top = '';
}
$top = ($i == 0) ? '<div class="active item">' : ($i % 5 == 0) ? '<div class="item">' : '';
$top = ($i == 0) ? '<div class="active item">' : (($i % 5 == 0) ? '<div class="item">' : '');
you need to add parenthesis' around the entire else block
The Ternary Operator doesn't support a true if... else if... else... operation; however, you can simulate the behavior by using the following technique
var name = (variable === 1) ? 'foo' : ((variable === 2) ? 'bar' : 'baz');
I personally don't care for this as I don't find it more readable or elegant. I typically prefer the switch statement.
switch (variable) {
case 1 : name = 'foo'; break;
case 2 : name = 'bar'; break;
default : name = 'bas'; break;
}
Too late probably to share some views, but nevertheless :)
Use if - else if - else for a limited number of evaluations. Personally I prefer to use if - else if - else when number of comparisons are less than 5.
Use switch-case where number of evaluations are more. Personally I prefer switch-case where cases are more than 5.
Use ternary where a single comparison is under consideration (or a single comparison when looping), or when a if-else compare is needed inside the "case" clause of a switch structure.
Using ternary is faster when comparing while looping over a very large data set.
IMHO Its finally the developer who decides the trade off equation between code readability and performance and that in turn decides what out of, ternary vs. if else-if else vs. switch-case, can be used in any particular situation.
//Use this format before reducing the expression to one liner
$var=4; //Change value to test
echo "Format result: ";
echo($var === 1) ? 'one' : //if NB.=> $varname = || echo || print || var_dump(ternary statement inside); can only be (placed at the start/wrapping) of the statement.
(($var === 2) ? 'two' : //elseif
(($var === 3) ? 'three' : //elseif
(($var === 4) ? 'four' : //elseif
'false' //else
))); //extra tip: closing brackets = totalnumber of conditions - 1
// Then echo($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false')));
echo "<br/>";
var_dump("Short result: ", ($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false'))) );