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

Dead Island

Member
Mar 22, 2012
5,350
681
0
no i understand it!

Good then if you have more questions follow the same method.
always remember to test what you write on your computer.
then use your logic and try to understand the logic behind the codes.
programming codes have a pattern and a logic if you can catch it properly.
you can predict the answer so easily. you can also follow this method.
first write the code then test it then get the final answer then try to go from the beginning and try to understand the logic and the pattern then solve it on a paper. if you can get the correct answer that's your achievement.

remember if you can solve an issue by yourself at the end you feel very confident. that's more than enough to reach your destiny.
 

sahanlak

Member
Nov 7, 2008
1,369
73
0
34
Badulla
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);


in first "for" it assign values to "Value" array, loop is running 5 times [i<=Max, Max is = 4], in each turn "i" is incremented by 1 (i++).

//1st time
Value[0] = 0 + 1; i = 0

//2nd time
Value[1] = 1+1; i = 1

//3rd time
Value[2] = 2+1; i = 2

//4th time
Value[3] = 3+1; i = 3

//5th time
Value[4] = 4+1; i = 4



at the end the values in the array are 1, 2, 3, 4, 5

next loop again its looping through 5 times, adding values to the "Answer" variable.

//1st time
Answer = 10 + 1; Answer has 10 assigned already += will add 1 to it, so Answer = 11

//2nd time
Answer = 11 + 2;

//3rd time
Answer = 13 + 3;

//4th time
Answer = 16 + 4;

//5th time
Answer = 20 + 5;

at the end Answer is 25,

Hope it make sense.
 

Red Soda

Well-known member
  • Mar 15, 2012
    3,013
    460
    113
    Kalutara
    මචන් php ඉගෙනගන්න හොද සින්හල ටියුට්ස් ටිකක් තියෙන sites ටිකක් කියපල්ලා.ලොකු උදව්වක්.
    :yes::yes::yes:
     

    sahanlak

    Member
    Nov 7, 2008
    1,369
    73
    0
    34
    Badulla
    මචන් php ඉගෙනගන්න හොද් සින්හල ටියුට්ස් ටිකක් තියෙන sites ටිකක් කියපල්ලා.ලොකු උදව්වක්.
    :yes::yes::yes:

    kuppiada mokadda kiala site ekak tibba mcn, a unata godak madi resources sinhalen igena gannawanan.
     

    P_Namal

    Active member
  • Oct 19, 2007
    862
    224
    43
    මචන් php ඉගෙනගන්න හොද සින්හල ටියුට්ස් ටිකක් තියෙන sites ටිකක් කියපල්ලා.ලොකු උදව්වක්.
    :yes::yes::yes:

    මචන් මම නම් කියන්නේ බැරි මරගාතේ හරි කමක් නෑ කඩ්ඩෙන්ම ඉගෙන ගන්න කියලා. :growl::growl: මොකද පොඩ්ඩක් මුල අල්ලගන්න විතරයි ඕනේ. ඒ ටික කට්ට කාගෙන ගොඩ දාගත්ත නම් ඊලගට උඔට සින් සින් ගාලා අල්ලගන්න පුලුවන්. :cool::cool: සිංහලෙන් tutorial ගොඩක් අඩු නිසා උඔට ඉගෙන ගන්න වෙන්නේ ගොඩක් හෙමින්. මහන්සි වෙලා මුල ටික අල්ලගනින්. සිංහලෙන්ම ඉගෙන ගන්න ඔිනම නම් ඉතින් සිංහල tutorial හොයනවට වඩා හොදයි කාගෙන් හරි අහගන්න එක. ;);) ඒත් ඉතින් තනියෙන් දෙයක් හොයන්න ගියාම අමාරුයි. programming කියන්නේ english නෙවෙයි. :no::no: :yes:
     

    P_Namal

    Active member
  • Oct 19, 2007
    862
    224
    43
    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.


    මෙච්චර මහන්සි වෙලා අපේ යාලුවන්ට කියල දෙනවට ගොඩක් ස්තූතියි... :yes::yes:
     

    Dead Island

    Member
    Mar 22, 2012
    5,350
    681
    0
    මෙච්චර මහන්සි වෙලා අපේ යාලුවන්ට කියල දෙනවට ගොඩක් ස්තූතියි... :yes::yes:

    නමුත් මේක නෙමෙයි මගේ සාමාන්‍යය ක්‍රමය
    මම උත්සහා ගන්නේ මම ලියන හැම tutorial එකකින්ම තමන්ට ස්ව උත්සහායෙන් නැගිසිටින විදිය කියලා දෙන්නයි. කිසිම කෙනෙක්ට බෑ තමන්ට උගන්වන්න තමන්ට උගන්වන්න පුලුවන් තමන්ටම විතරයි.
     

    P_Namal

    Active member
  • Oct 19, 2007
    862
    224
    43
    කවුරු හරි දන්නවද &$ දාන එකෙන් මොකක්ද වෙන්නේ කියලා??

    PHP:
    <?php
    function add_some_extra(&$string)
    {
        $string .= 'and something extra.';
    }
    $str = 'This is a string, ';
    add_some_extra($str);
    echo $str;    // outputs 'This is a string, and something extra.'
    ?>
     

    sahanlak

    Member
    Nov 7, 2008
    1,369
    73
    0
    34
    Badulla
    කවුරු හරි දන්නවද &$ දාන එකෙන් මොකක්ද වෙන්නේ කියලා??

    PHP:
    <?php
    function add_some_extra(&$string)
    {
        $string .= 'and something extra.';
    }
    $str = 'This is a string, ';
    add_some_extra($str);
    echo $str;    // outputs 'This is a string, and something extra.'
    ?>
    Pass by reference, function eka atuledi karana wenas kam pass karana original variable ekata sidda wenawa.