PHP array + function calling problem

GT9

Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]
    මචන්ලා මම php සම්බන්දව ගැටලුවක් තියෙනවා. :yes:

    මෙන්න මේකයි ගැටලුව. :D

    මචන්ලා මම සරල php function එකක් ලිව්වා මේකෙන් වෙන්නේ function එක call කරන තැන්වලදි. එකක් මුල් අගයට එකතු වෙලා echo වෙන එක. :D:yes:

    මෙන්න මේකයි කෝඩ් එක..

    Code:
    <?php
    
    function nuAuInc()
    {
    static $x=0;
    echo $x;
    $x++;
    }
    
    nuAuInc();
    nuAuInc();
    nuAuInc();
    
    ?>

    ඔය කෝඩ් එක run කරාම වෙන්නේ.. 012 echo වෙන එක.. ඉතින් එතන මට ගැටලුවක් නෑ...:D:cool:

    මට ගැටලුව එන්නේ මෙන්න මෙතෙන්දී...
    මේකයි මම පොඩි array එකක් හැදුවා.. මේතියෙන්නේ ඒක..:yes:

    Code:
    $names = array ("Sanka","Janith","Vidura","Roshen");
    
    echo $names[3];

    මේ කෝඩ් එක රන් කරාම Roshen කියලා print වෙනවා.. එතනත් ගැටලුවක් නෑ..:baffled::P:D

    ----------

    ගැටලුව වෙන්නේ මෙන්න මේ දේ මම කරන්න ගියාම..

    Code:
    <?php
    
    $names = array ("Sanka","Janith","Vidura","Roshen");
    
    echo $names[nuAuInc()];
    
    ?>

    මම දන්නෑ මචන් මේ දේ කරන්න පුලුවන්ද කියල..:rofl::baffled: මට කරගන්න ඕන මචන්..

    $names[3]; ඔන්න ඔය variable එකේ.. 3(array key index එක) වෙනුවට අර function එක (nuAuInc() එක) call කරන්න. මේදේ කරන්න පුලුවන්ද මචන්ලා? :confused:

    මම මෙහෙම කරාම.. Undefined index: කියල error එකක් එනව...:sorry:

    සරලවම මට දැනගන්න ඕන.. $names[3]; ඔන්න ඔය වගේ variable එකක array key index numbers වෙනුවට ‍මගේ function එක call කරන්න..මම උත්සාහ කරා මේ විදියට.. $names[nuAuInc()]; ඒත් හරියන්නෑ.. error එකක් එනවනේ..:sorry: මේ දේ කරන හැටි කියල දෙන්න ලොකු උදව්වක්.. :yes::D:yes:
     
    • Like
    Reactions: rskoora

    rskoora

    Well-known member
  • Apr 1, 2008
    4,001
    608
    113
    function nuAuInc()
    {
    static $x=0;

    $x++;

    return $x;
    }

    echo $names[nuAuInc()]; //Sanka

    echo $names[nuAuInc()]; //Janith

    run kale nam na habay wada karana ona..
     

    GT9

    Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]
    function nuAuInc()
    {
    static $x=0;

    $x++;

    return $x;
    }

    echo $names[nuAuInc()]; //Sanka

    echo $names[nuAuInc()]; //Janith

    run kale nam na habay wada karana ona..

    රිප්ලයි එකට තෑන්ක්ස් මචන්.. බට් මචන් වැඩ කරන්නෑනේ..:sorry:
    $names[nuAuInc()]; විදියට තමයි මචන් මමත් කරල බැලුවෙ bUT ERROR එකක් එනවනේ...

    :sorry::confused::eek:
     

    rskoora

    Well-known member
  • Apr 1, 2008
    4,001
    608
    113
    <?php
    $names = array ("Sanka","Janith","Vidura","Roshen");

    function nuAuInc()
    {
    static $x=0;

    $x++;

    return $x;
    }

    echo $names[nuAuInc()]; //Sanka

    echo $names[nuAuInc()]; //Janith


    wada karanawa saho mama dan run kala :P
     

    GT9

    Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]

    බම්ප් එකක් දුන්නට ඔයාටත් තෑන්ක්ස්. :D:yes:

    <?php
    $names = array ("Sanka","Janith","Vidura","Roshen");

    function nuAuInc()
    {
    static $x=0;

    $x++;

    return $x;
    }

    echo $names[nuAuInc()]; //Sanka

    echo $names[nuAuInc()]; //Janith


    wada karanawa saho mama dan run kala :P

    අම්මෝ මචන් මාරම සතුටුයි.. වැඩේ ගොඩ. :D:P සෑහෙන්න ස්තුතියි මචන් කර උදව්වට.. :yes::yes::yes: මචන් මම කරපු වෙලේ වැඩ කරේ නැත්තේ.. return $x; කිය ස්ටේට්මන්ට් එක දාල තිබුනෙ නැති හින්දද?
     

    rskoora

    Well-known member
  • Apr 1, 2008
    4,001
    608
    113
    බම්ප් එකක් දුන්නට ඔයාටත් තෑන්ක්ස්. :D:yes:



    අම්මෝ මචන් මාරම සතුටුයි.. වැඩේ ගොඩ. :D:P සෑහෙන්න ස්තුතියි මචන් කර උදව්වට.. :yes::yes::yes: මචන් මම කරපු වෙලේ වැඩ කරේ නැත්තේ.. return $x; කිය ස්ටේට්මන්ට් එක දාල තිබුනෙ නැති හින්දද?

    ow ban