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.
Related
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;
}
}
My code:
$a['page'] = 1;
function change($a) {
$a['page'] = 2;
}
My output:
$a['page'] = 1;
$a['page'] = 2;
Why am I get two keys 'page'?
I was expecting the function changed the value.
you could pass $a by reference instead and it would work as expected. It would be slower but not significantly so, given the function.
$a['page'] = 1;
function change(&$a) {
$a['page'] = 2;
}
change($a);
echo "<pre>";
print_r($a);
$a['page'] = 1;
function change($a) {
return $a['page'] = 2;
}
echo change($a);
I wanted to have a result like this
50+2+2+2+2 = 58
But I'm getting these kind of result
50
2
2
2
2
these are my codes.
<?php
$height = 50;
echo $height;
function hey($x)
{
return $height += $x;
}
$i = 4;
while($i != 0)
{
echo "<br>".hey(2);
$i--;
}
?>
Please take note that the location of my variable and loop must be and really meant at that position.
what do i need to change on my code. I am new in using php functions.
thanks for the help.
You can use a global variable like this try:
function hey($x)
{
global $height;
return $height += $x;
}
And print the variable height only after the called function.
If you don't put global before the variable inside the function it the seems that you create a new variable inside your function. With global you tell to the server to take the variable that you have created outside the function
In this function:
function hey($x)
{
return $height += $x;
}
$height is not in scope, so it is undefined. You should pass it in:
function hey($x, $height)
{
return $height += $x;
}
Then call it like this:
hey(2, $height);
this is scope problem :
function hey($x)
{
global $height;
return $height += $x;
}
Change to:
global $height;
then
while($i != 0)
{
echo "+".hey(2);
$i--;
}
echo "=" . $height;
I don't understand that you want, but if you need output like that, please try this code..
br mean go to bottom, so I delete it.
<?php
$height = 50;
echo $height;
function hey($x)
{
echo " + $x";
return $x;
}
$i = 4;
while($i != 0)
{
$height += hey(2);
$i--;
}
echo " = $height";
?>
This is online demo: http://phpfiddle.org/main/code/6h1-x5z
<?php
function getResult($height = 50, $increment = 2, $times = 4){
echo $height."+";
$total = 0;
for ($i = 0; $i < $times; $i++){
$total += $increment;
if ($i != ($times-1)){
echo $increment."+";
}
else{
echo $increment." = ".($height+$total);
}
}
}
//usage
getResult(50,2,4);
//The print out: 50+2+2+2+2 = 58
?>
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++;
}
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