can any1 plz help me write this code…

i want to print the valu of variable z.
there are three variables x, y and z

x is initialized some where else…
y=x z=z+y

first call of function x=10 y=10 z=10
second call x=20 y =20 z=20+10
third call x=30 y= 30 z=30+30
fourth call x=40 y=40 z=90+40

and so on

Welcome to SitePoint.

That makes no sense. I think.

Where did 90 come from in the fourth call, and why add 30 to $z in the the third?

nishafav:

```
y=x z=z+y
```

```
<?php
$x = $y = $x = 0;
function process(&$x, &$y, &$z){
$z = $z + ($y = $x += 10);
}
foreach(range(1, 5) as $iteration){
process($x, $y, $z);
printf(
'$x = %d, $y = %d, $z = %d' . "\
",
$x,
$y,
$z
);
}
/*
$x = 10, $y = 10, $z = 10
$x = 20, $y = 20, $z = 30
$x = 30, $y = 30, $z = 60
$x = 40, $y = 40, $z = 100
$x = 50, $y = 50, $z = 150
*/
```

u are a darling… thanks a tone… and sorry for that 90 mistake… i was so damn confused that i wrote 90 instead of 60…

one query…what if the value of x changes randomly…?? like
first it is 39
second it is 50
third its 55
and so on…

One other tip is to nor write help urgent ad the post title because that can cause many proletariat to ignore It. Help with php variable would have been a fine title

Like this?

```
<?php
function process($x){
static $previous = 0;
printf(
'$x = %d, $y = %d, $z = %d' . "\
",
$x,
$y = $x,
$z = $previous + $y
);
$previous = $z;
}
foreach(range(1, 10) as $x){
process($x);
}
/*
$x = 1, $y = 1, $z = 1
$x = 2, $y = 2, $z = 3
$x = 3, $y = 3, $z = 6
$x = 4, $y = 4, $z = 10
$x = 5, $y = 5, $z = 15
$x = 6, $y = 6, $z = 21
$x = 7, $y = 7, $z = 28
$x = 8, $y = 8, $z = 36
$x = 9, $y = 9, $z = 45
$x = 10, $y = 10, $z = 55
*/
```

fine… thanks for the suggestion…

For giggles, here’s some more awful code.

```
<?php
function update($x, &$y, &$z){
static $previous = 0;
$previous = $z = $previous + $y = $x;
}
foreach(range(2, 20, 2) as $x){
$y = $z = 0;
update($x, $y, $z);
printf(
'$x = %d, $y = %d, $z = %d' . "\
",
$x,
$y,
$z
);
}
/*
$x = 2, $y = 2, $z = 2
$x = 4, $y = 4, $z = 6
$x = 6, $y = 6, $z = 12
$x = 8, $y = 8, $z = 20
$x = 10, $y = 10, $z = 30
$x = 12, $y = 12, $z = 42
$x = 14, $y = 14, $z = 56
$x = 16, $y = 16, $z = 72
$x = 18, $y = 18, $z = 90
$x = 20, $y = 20, $z = 110
*/
```

AnthonySterling:

Like this?

```
<?php
function process($x){
static $previous = 0;
printf(
'$x = %d, $y = %d, $z = %d' . "\
",
$x,
$y = $x,
$z = $previous + $y
);
$previous = $z;
}
foreach(range(1, 10) as $x){
process($x);
}
/*
$x = 1, $y = 1, $z = 1
$x = 2, $y = 2, $z = 3
$x = 3, $y = 3, $z = 6
$x = 4, $y = 4, $z = 10
$x = 5, $y = 5, $z = 15
$x = 6, $y = 6, $z = 21
$x = 7, $y = 7, $z = 28
$x = 8, $y = 8, $z = 36
$x = 9, $y = 9, $z = 45
$x = 10, $y = 10, $z = 55
*/
```

please dont mind my so many questions AnthonySterling… but my query is that what to do in case of random values of x… how to approach for that thing…

It handles a random figure for $x just fine.

```
<?php
function update($x, &$y, &$z){
static $previous = 0;
$previous = $z = $previous + $y = $x;
}
foreach(range(1, 10) as $i){
#assign $x a random number
$x = rand(9, 99);
$y = $z = 0;
update($x, $y, $z);
printf(
'$x = %d, $y = %d, $z = %d' . "\
",
$x,
$y,
$z
);
}
/*
$x = 51, $y = 51, $z = 51
$x = 58, $y = 58, $z = 109
$x = 66, $y = 66, $z = 175
$x = 20, $y = 20, $z = 195
$x = 64, $y = 64, $z = 259
$x = 64, $y = 64, $z = 323
$x = 78, $y = 78, $z = 401
$x = 22, $y = 22, $z = 423
$x = 74, $y = 74, $z = 497
$x = 76, $y = 76, $z = 573
*/
```

Just change

```
process($x);
```

in

```
process(rand(1, 90));
```

Change the min and max values of the rand function to your own need.

By the way, are we talking homework here?

thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…thanx thanx…

I cant thank you enough… u r God send… thanx Anthony…

Zurev
December 30, 2010, 2:22pm
14

guido2004:

Just change

```
process($x);
```

in

```
process(rand(1, 90));
```

Change the min and max values of the rand function to your own need.

By the way, are we talking homework here?

heh, was gonna say. Better to scan the homework assignment right in here

and what does that mean???

Zurev
December 30, 2010, 2:45pm
16
Didn’t mean to offend, it’s just that it’s a common homework assignment for programming/logic 101.

well… in that case i wanna share that m not doing this as a homework… but i am gonna use this logic in a table where the td is dynamically generated for a table. there is a query which is in a loop and its at run time… this small function will help me complete my ERP…