I'm using the following code from PHPBuilder.com to handle user privileges on my site:
/**
* Correct the variables stored in array.
* #param integer $mask Integer of the bit
* #return array
*/
function bitMask($mask = 0) {
if(!is_numeric($mask)) {
return array();
}
$return = array();
while ($mask > 0) {
for($i = 0, $n = 0; $i <= $mask; $i = 1 * pow(2, $n), $n++) {
$end = $i;
}
$return[] = $end;
$mask = $mask - $end;
}
sort($return);
return $return;
}
and I'm a bit baffled by the "= 0" part of ($mask = 0) in the function parameter list. What does that do?
It means that if you call the function like this:
$a = bitMask();
Then $mask will be set to 0.
It is how you set default values to parameter in functions.
Example:
function example($a=0){
echo "a = $a";
}
example(10);
example();
Output:
a = 10
a = 0
If $a did not have a default value set, then calling the function like example() would give a warning.
reference: http://php.net/manual/en/functions.arguments.php (Default argument values)
That's the default value of $mask if no arguments are passed. This also prevents a warning from being generated when the parameter is omitted.
Michael's response is correct. To add to it, take note that the assignment will not have an affect on the original variable modified. Here is his code with a few more assignments / echos to illustrate this:
function example($a=0){
echo "Entering function: a = $a\n";
$a = 3;
echo "End of function: a = $a\n";
}
$a = 7;
example(10);
echo "Outside of Function: a = $a\n";
Outputs
Entering function: a = 10
End of function: a = 3
Outside of Function: a = 7
Related
this is my code:
$arry = (10,20,30,40,50)
function pairwiseDifference($arry, $n)
{
$diff = 0;
for ($i = 0; $i < $n - 1; $i++)
{
// absolute difference between
// consecutive numbers
$diff = abs($arry[$i] - $arry[$i + 1]);
echo $diff." ";
}
}
// Driver Code
$n = sizeof($arry);
echo "<br> Percent of commisions are: "; pairwiseDifference($arry,$n); // this echo 10,10,10,10
So now i wanna make the results of pairwise funcition an array to use in another code, i tried to put:
$arru = pairwiseDifference($arry,$n)[1];
$arru1 = pairwiseDifference($arry,$n)[2];
$arru2 = pairwiseDifference($arry,$n)[3];
$arru3 = pairwiseDifference($arry,$n)[4];
echo $arru;
echo $arru1;
echo $arru2;
echo $arru3;
But are not working.
What im wrong?
Thanks in advance!
An array declaration in PHP needs to either have the array keyword or it can just be declared with square brackets []:
$arry = array(10,20,30,40,50);
or
$arry = [10,20,30,40,50];
You can get the size of an array with the count() method, and there is no need to pass it as a parameter to your function:
function pairwiseDifference($arry)
{
$n = count($arry);
// ...
}
Your function doesn't return anything, so the variables $arru, $arru1, ... won't get anything back from the function and will remain empty. In order to return an array of differences and solve your problem you can do this:
function pairwiseDifference($arry)
{
$n = count($arry) - 1; // you can add the -1 here
$diff = 0;
$result = array();
for ($i = 0; $i < $n; $i++)
{
// absolute difference between
// consecutive numbers
$diff = abs($arry[$i] - $arry[$i + 1]);
echo $diff." ";
array_push($result, $diff); // use array_push() to add new items to an array
}
return $result; // return the result array
}
$arry = array(10,20,30,40,50);
echo "<br> Percent of commisions are: ";
// this echos 10,10,10,10 and assigns whatever your function returns
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);
$arru = $diffArray[0]; // array elements start at index 0
$arru1 = $diffArray[1];
$arru2 = $diffArray[2];
$arru3 = $diffArray[3];
echo $arru; // prints 10
echo $arru1; // prints 10
echo $arru2; // prints 10
echo $arru3; // prints 10
How to find common character matches in two strings having same length using PHP?
For example,
$s1 = "ashyjUTY#rj[jkIIO}[hh{FIL]Ojk89y]";
$s2 = "pshyjUTY#r7[jk8rO}[hh{DrL]OjkB7y]";
cMatch($s1, $s2);
Output:
-shyjUTY#r-[jk--O}[hh{--L]Ojk--y]
The cMatch function will predict the common character matches like above.
CODE
<?php
function cMatch($s1, $s2)
{
$p = $s1;
$r = $s2;
$m = str_split($p, 1);
$n = str_split($r, 1);
$a = count($m);
$b = count($n);
if ($a == $b) {
for ($i = 0; $i < $a; $i++) {
if ($m[$i] == $n[$i]) {
print $m[$i];
} else {
print "-";
}
}
} else {
print "Length of both strings are different!"; }
}
$x = "ashyjUTY#rj[jkIIO}[hh{FIL]Ojk89y]";
$y = "pshyjUTY#r7[jk8rO}[hh{DrL]OjkB7y]";
cMatch($x, $y);
?>
Take a look to the below solution:-
<?php
$s1="ashyjUTY#rj[jkIIO}[hh{FIL]Ojk89y]";
$s2="pshyjUTY#r7[jk8rO}[hh{DrL]OjkB7y]";
$already=""; // create an empty string
for($i=0;$i<strlen($s2);$i++) // start loop
{
if ($s1[$i] == $s2[$i]) // done match of character at exact position in both string
{
$already .=$s2[$i]; // if match found then assign the character to the newly created variable
}else{
$already .='-'; // if not then add - to the variable
}
}
echo $already; // print the variable and get the common characters along with - included too.
?>
Output:- https://eval.in/520701
Note:-
This code will match characters at same position in both string.
Also i believe that you can change it into function very easily.Thanks.
Try this, it might help you.
<?php
$s1 = "ashyjUTY#rj[jkIIO}[hh{FIL]Ojk89y]";
$s2 = "pshyjUTY#r7[jk8rO}[hh{DrL]OjkB7y]";
$S1_arr = str_split($s1);
$S2_arr = str_split($s2);
$common = implode(array_unique(array_intersect($S1_arr, $S2_arr)));
echo "'$common'";
?>
I was given a quiz by an employer to determine my ability as a programmer and the test was more or less "Write a function that counts the length of this linked list". I failed the quiz because for whatever reason my function didn't return anything (It was a timed quiz). This is my code.
class IntList{
var $value = 1;
var $next = null;
}
$A = new IntList();
$B = new IntList();
$C = new IntList();
$D = new IntList();
$A->next = $B;
$B->next = $C;
$C->next = $D;
main($A);
$count = 0;
function main($L)
{
global $count;
$final = getListLength($L, $count);
print $final;
}
function getListLength($L, $count)
{
if (isset($L->next))
{
$count++;
getListLength($L->next, $count);
} else
{
print $count;
return $count;
}
}
in getListLength im getting 3 when i print count before the return statement. But after the function returns I'm left with no output. I feel really stupid right now. Any thoughts?
Assuming this is the code from the quiz (argh, PHP4 --'):
class IntList{
var $value = 1;
var $next = null;
}
$A = new IntList();
$B = new IntList();
$C = new IntList();
$D = new IntList();
$A->next = $B;
$B->next = $C;
$C->next = $D;
I don't think you need recursion to solve that. You could just:
function getListLength($list) {
$count = 0;
$item = $list;
while($item instanceof IntList) {
$count++;
$item = $item->next;
}
return $count;
}
You just forgot to put global $count; in the second function.
Also, if you want to count the last one, you should move the $count++ outside of the conditional.
Here's a fiddle.
Alternatively, you can pass the $count variable by reference
function getListLength($L, &$count){...}
Another fiddle..
Since you are trying to use recursion here, I think the only thing that is missing is that your recursive case is not returning. You really should not need global. If you need to start at zero, you can give your getListLength a default count, or explicitly call it with zero in main.
function main($L) {
$final = getListLength($L);
print $final;
}
function getListLength($L, $count = 0) {
if (isset($L->next)) {
$count++;
// this case should return
return getListLength($L->next, $count);
} else {
return $count;
}
}
I'd like to store a function in a variable, is that possible?
Let's see this example
<?php
$tot_prezzo = 200;
$x = 75;
$y = 25;
function addition() {
echo $GLOBALS['imponibile'] = $GLOBALS['tot_prezzo'] + $GLOBALS['x'];
}
$func = 'addition';
$func();
?>
How can I store "$func()" in a variable?
I've tried with -> $func_var = $func(), but it doesn't work.
What am I missing here?
Thank you!
This is called "Lambda Functions" and can be used like this:
$addition = function($a, $b) {
return $a+$b;
};
echo $addition(1,2); //3
In order to use a value from the function you simply have to return it.
$tot_prezzo = 200;
$x = 75;
$y = 25;
function addition($tot_prezzo,$x,$y) {
$imponibile = $tot_prezzo + $x;
return $imponibile;
}
$result = addition($tot_prezzo,$x,$y);
print $result;
Note the use of arguments instead of $GLOBALS also you are not using the $y variable.
Is it possible to execute a function, then return a new value for Var and therefore when it tries to loop again it checks the condition and detects the changes?
Let's say I have this function:
function changeZ(){
$z=1
return $z;
}
and this is my while loop:
$a = 0;
$z = 10;
while($a<$z){
changeZ();
$a++;
}
How should I modify the codes such that
the function changeZ() returns a new value for the variable $z
and therefore when the loop checks for condition (1<1), it returns a false and stop looping.
You can pass by reference or return the value
$z = 100;
change($z);
function change(&$var) {
$var = 1;
}
$z = 100;
$z = change($z);
function change($var) {
return $var * 100 - 50;// Logic with $z obviously can be whatever
}
The $z in your function and the $z you use in the loop are not the same guys. So you have to set the value of the loop-z to the return value of your function...
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ();
$a++;
}
$z = changeZ();
but as commented, you appear to definitely going about this wrong.
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ();
$a++;
}
Since your function returns a value you should set your variable to contain the returned value. This will do what you asked resulting in the loop running only once.
Another thing you can do is pass the variable into a function like so
function changeZ($in){
$out = $in-1;
return $out;
}
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ($z);
$a++;
}
This will result in the loop running 5 times as one number goes up the other goes down
0<10
1<9
2<8
3<7
4<6
you can use this for your purpose for storing the returned data from function into an array.
function changeZ(){
$z = $row['result']; //your data from database
return $z;
}
$a = 0;
$z = 10;
$returned_value=new array();
while($a<$z){
$returned_value[] = changeZ();
$a++;
}
You have to put this
$a = 0;
$z = 10;
while($a<$z) {
$z = changeZ();
$a++;
}