PHP කාපරයෙක් වෙමුද?

Dead Island

Member
Mar 22, 2012
5,350
681
0
hey do u kow what the result wud be for this code below

<?php
function swap($x, $y) {
$x = $x + 1;
$y = $y + 2;
return $x * $y;
}
$a = 3;
$b = swap($a, $a);
print "$a, $b";
$b = swap(&$a, &$a);

print "$a, $b";
?>


This is depreciated in PHP 5.3.0 and newer versions. so DO NOT
use call time pass by reference. but it still shows the results.
I suggest you avoid using this.
 

||~R_girl~||

Member
Mar 21, 2008
34,536
460
0
What value of the variable Answer will be printed when this is executed?

var Max =4, Answer = 10;
var Value = new Array(Max);
for (i=0;i<=Max;i++)
Value=i+1;
for (i=0;i<=Max;i++)
Answer +=Value;
alert (Answer);
 

Dead Island

Member
Mar 22, 2012
5,350
681
0
What value of the variable Answer will be printed when this is executed?

var Max =4, Answer = 10;
var Value = new Array(Max);
for (i=0;i<=Max;i++)
Value=i+1;
for (i=0;i<=Max;i++)
Answer +=Value;
alert (Answer);


It's JavaScript if you've ever tried you can obviously get 25 as the answer.
I suggest you get some basic idea then you can do anything.
 

Dead Island

Member
Mar 22, 2012
5,350
681
0
iv been trying to get a basic idea for the past coupla years now..
could you explain how you got 25?

Code:
var Max =4, Answer = 10;
var Value = new Array(Max);
for (i=0;i<=Max;i++)
Value[i]=i+1;
for (i=0;i<=Max;i++)
Answer +=Value[i];
alert (Answer);
It isn't that much hard. first you need to know what is a variable.
variable is a container which lays in the system memory (RAM) and store values in binary form. it has an unique address which is used by the application to identify that particular variable.

so in this phase what you do is
vQNdy.png


Code:
var Max =4
declare a variable which means actually make a variable in the system memory and its name is "MAX" though its name is max actually it doesn't have a name(the name is for the programmer so programmer can give it a meaningful name rather than numbers(address) so programers can easily program) it has an address in the memory and value 4 is stored by the application in that memory slot.




THIS IS THE BASIC. if you wanna know more advance the picture might be a bit different. cause there are datatypes though JavaScript doesn't have datatypes when you declare a variable the application defines the datatype of that variable depend in the value passes(assign) to that variable.

so if you pass 3 to any variable the application defines that variable's datatype as integer. integer is normally 32bit.
(1 Byte is equal to = 8 bits) so in the memory 32/8 = 4 bytes of values it can hold.

now take this example you pass 4 value to the MAX variable.
so the application identifies automatically the datatype of the MAX variable and stores the value 4 inside the MAX variable.
the numbers or everything represents in the system memory as BINARY.
so ONE character takes 1 byte (if it's ASCII if it's UNICODE 2 bytes) (this is for characters NOT numbers) when it comes to numbers the picture is different.
convert 4 to binary then u can get this 0100.
in 32bit you can go to MAX

0000 0000 0000 0000 0000 0000 0000 0000

this is minimum value that you can store in a 32bit (integer) variable in this case MAX variable. the MAX value is

0111 1111 1111 1111 1111 1111 1111 1111 (BINARY)
which is equal to 2147483647 (in DECIMAL)
and this the Maximum VALUE you can hold in the MAX variable.

the reason for keeping the first value as 0 cause it's for identifying whether this number is a POSITIVE or a NEGATIVE VALUE.
if it's negative first 0 is 1, if it's a positive value the first value is 0.


Now tell me do u understand so far? if u have any issue then let me know.
 
Last edited:

Dead Island

Member
Mar 22, 2012
5,350
681
0
this is not at all related to my question ne
anyway yeah i understand what you explained here

yeah I know it's not directly related to your question. but indirectly.
you know Mao Zedong said once.if someone asks you for a fish.
it's better teach him how to catch fish rather than giving him a fish.
cause then you know how to stand by your own foot. so that's what I am trying to do. teach
you how to stand by your own foot. I've never asked programming questions cause I know How to solve them by myself.
that's what people need to learn.
 

Dead Island

Member
Mar 22, 2012
5,350
681
0
hey thanks for that but iv got no time to stand by my foot now
im gonna have to get ready
*sigh*

var Max =4, Answer = 10;
var Value = new Array(Max);
for (i=0;i<=Max;i++)
Value=i+1;
for (i=0;i<=Max;i++)
Answer +=Value;
alert (Answer);
okay fine but keep in your mind you will never be productive if you go like this way forever. if you have spare time try to learn programming properly. if you have any questions make them on Yahoo Answers or on here.

I will then boost the tutorial.

Code:
var Max =4, Answer = 10;
declare 2 variables and assign 4 to MAX variable
and assign 10 to Answer variable.

Code:
var Value = new Array(Max);
declare an array and specify its size. so in this case the array's size is
4 (cause it uses variable MAX and variable max is 4).
then store the newly made array inside the value array variable.

Code:
for (i=0;i<=Max;i++)
if the FOR clause has just one line you DO NOT need to use
{} curly brackets. what you here do
declare and initialize the i variable and assign 0 to it.
then specify the RANGE. so in this case the RANGE is
i <= MAX. since the MAX variable is 4
it means.
i <= 4
then specify the increment or decrement value. in this case
i++ means i increase by 1 itself. so if the i value is 0 in the next iteration
i is 1 cause it increases itself.


Code:
Value[i]=i+1;
in the first iteration i is 0 so
so i+ 1 means 0+1 = 1
so answer is 1 which is then stored in the Value Array's 0 POSITION.
(remember still 1 is 0)

so it goes like this way for 5 iterations.

1 iteration
Value[0]= 0 +1;

2 iteration
Value[1]= 1 +1;

3 iteration

Value[2]= 2 +1;

4 iteration

Value[3]= 3 +1;

5 iteration
Value[4]= 4 +1;

4<=4
(TRUE)

6 Iteration

=> in this phase the FOR loop stops looping itself since the range is fulfilled.
you see if the i is 5
5<=4
this statement returns false. when the condition is false FOR loop doesn't execute the codes within its brackets {}.

SO THE FOR LOOP executes as long as the Condition (Range) returns TRUE.

Code:
for (i=0;i<=Max;i++)
this is same as the above example.
so no need for additional explanation.

Code:
Answer +=Value[i];
in this case, += means
ANSWER = ANSWER + Value so when it makes short
you can write like this way
Answer +=Value;

in the first iteration.
ANSWER variable is 10, i is 0
the VALUE array holds 4 VALUES
those are 1,2,3,4,5

VALUE[0] is 1
VALUE[1] is 2
VALUE[2] is 3
VALUE[3] is 4

VALUE[4] is 5

ANSWER = ANSWER + Value

ANSWER = 10 + 1

ANSWER = 11 + 2

ANSWER = 13 + 3

ANSWER = 16 + 4

ANSWER = 20 + 5



Code:
alert (Answer);
what alert function does. display a dialog box. and it takes 1 argument.
it's string typed used to specify the BODY text of its DIALOG BOX.
so in this case the argument of alert function is ANSWER variable.
the ALERT will display the VALUE inside the ANSWER variable which is 25.