PHP Masters come here!

shan_wicram

Member
Aug 21, 2011
857
84
0
මචන්ලා මට PHP සම්බන්දව ගැටලුවක් තියෙනවා මේකයි කේස් එක.

මට දැන ගන්න ඕන if conditional block එකක් ඇතුලෙ තියෙන function එකක් එම if conditional block එකෙන් පිටතදී භාවිතා කරන්නේ කියල.

හරියට මේ වගේ දෙයක්.


PHP:
<?php
$first_name = 'shan';
$second_name = 'shan';

if ($first_name == $second_name) {
function welcome() {
echo "hello buddy";
}

// working 
welcome();
} else ( echo "your not a my friend";)

// not working :( can some one help me to solve this problem? pls :)
welcome();
?>

variables වලිදි නම් ඉස්සරහට GLOBAL දාල වැඩේ කරගන්න පුලුවන්. මේකෙදි කොහොමද කරන්නේ? අනිවා මම දන්නවා එළකිරියේ හොඳ වැඩකාරයෝ ඉන්නවා. උදව්වක් කරන්න පුලුවන් නම් ගොඩක් වටිනවා..:D:D:D
 
Last edited:

sanoka

Well-known member
  • Dec 6, 2007
    5,147
    1,233
    113
    why you want to declare the function inside the IF clause, declare it separately and call it when ever you need it.
     
    • Like
    Reactions: shan_wicram

    shan_wicram

    Member
    Aug 21, 2011
    857
    84
    0
    මම වැරදි වෙලාවකද දන්නෑ ‍ත්‍රෙඩ් එක දැම්මේ! :D එළකිරි කොල්ලෝ පුන්‍යකාලෙට පන්සල් ගිහිල්ලා වගේ... :cool::D
     

    IcedEarth

    Member
    Feb 1, 2010
    1,523
    244
    0
    Somewhere In Ice Age
    Code:
    [LEFT]   [COLOR=#000000] [COLOR=#0000BB]<?php
    $first_name [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]'shan'[/COLOR][COLOR=#007700];
    [/COLOR][COLOR=#0000BB]$second_name [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]'shan'[/COLOR][COLOR=#007700];
    
    [/COLOR][/COLOR][COLOR=#000000][COLOR=#007700]function [/COLOR][COLOR=#0000BB]welcome[/COLOR][COLOR=#007700]() {
    echo [/COLOR][COLOR=#DD0000]"hello buddy"[/COLOR][COLOR=#007700];[/COLOR][/COLOR]
    [COLOR=#000000][COLOR=#007700]}
    
    if ([/COLOR][COLOR=#0000BB]$first_name [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#0000BB]$second_name[/COLOR][COLOR=#007700]) {
    [/COLOR][/COLOR][COLOR=#000000][COLOR=#0000BB]welcome[/COLOR][COLOR=#007700]()[/COLOR][/COLOR]
    [COLOR=#000000][COLOR=#007700]}
    
    [/COLOR][COLOR=#FF8000]
    [/COLOR][COLOR=#0000BB]welcome[/COLOR][COLOR=#007700]();
    } else ( echo [/COLOR][COLOR=#DD0000]"your not a my friend"[/COLOR][COLOR=#007700];)
    [/COLOR][COLOR=#FF8000]
    [/COLOR][COLOR=#0000BB]welcome[/COLOR][COLOR=#007700]();
    [/COLOR][COLOR=#0000BB]?>[/COLOR] [/COLOR]    [/LEFT]
     

    shan_wicram

    Member
    Aug 21, 2011
    857
    84
    0
    If eka athule function eka nodaa function eka eliye hadala if eken function ekata call karanna!!!

    අම්මෝ.. පට්ට මචන්. තෑන්ක්ස් වැඩේ ගොඩ.. :yes::yes::yes::D:D:D

    why you want to declare the function inside the IF clause, declare it separately and call it when ever you need it.

    වැඩේ ගොඩදාගත්ත මචන්. තෑන්ක්ස් රිප්ලයි කරාට..:yes::D:yes::cool:
     

    shan_wicram

    Member
    Aug 21, 2011
    857
    84
    0
    Code:
    [LEFT]   [COLOR=#000000] [COLOR=#0000BB]<?php
    $first_name [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]'shan'[/COLOR][COLOR=#007700];
    [/COLOR][COLOR=#0000BB]$second_name [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]'shan'[/COLOR][COLOR=#007700];
    
    [/COLOR][/COLOR][COLOR=#000000][COLOR=#007700]function [/COLOR][COLOR=#0000BB]welcome[/COLOR][COLOR=#007700]() {
    echo [/COLOR][COLOR=#DD0000]"hello buddy"[/COLOR][COLOR=#007700];[/COLOR][/COLOR]
    [COLOR=#000000][COLOR=#007700]}
    
    if ([/COLOR][COLOR=#0000BB]$first_name [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#0000BB]$second_name[/COLOR][COLOR=#007700]) {
    [/COLOR][/COLOR][COLOR=#000000][COLOR=#0000BB]welcome[/COLOR][COLOR=#007700]()[/COLOR][/COLOR]
    [COLOR=#000000][COLOR=#007700]}
    
    [/COLOR][COLOR=#FF8000]
    [/COLOR][COLOR=#0000BB]welcome[/COLOR][COLOR=#007700]();
    } else ( echo [/COLOR][COLOR=#DD0000]"your not a my friend"[/COLOR][COLOR=#007700];)
    [/COLOR][COLOR=#FF8000]
    [/COLOR][COLOR=#0000BB]welcome[/COLOR][COLOR=#007700]();
    [/COLOR][COLOR=#0000BB]?>[/COLOR] [/COLOR]    [/LEFT]

    ඔව් මචන් මේ විදියට තමයි කරගත්තේ. ලොකුම ලොකු උදව්වක් කරේ මචන් ගොඩක් ස්තුතියි..:yes::yes::yes:

     

    rskoora

    Well-known member
  • Apr 1, 2008
    4,001
    608
    113
    dude , you can't do that when some thing comes in to the if {} block that belongs only to between that block !!

    this is a totally bad practice you are trying now. better to try do it in OOP way there are 1000's of resource available in net

    gud luck
     
    • Like
    Reactions: shan_wicram

    shan_wicram

    Member
    Aug 21, 2011
    857
    84
    0
    dude , you can't do that when some thing comes in to the if {} block that belongs only to between that block !!

    this is a totally bad practice you are trying now. better to try do it in OOP way there are 1000's of resource available in net

    gud luck

    ස්තුතියි මචන් පැහැදිලි කරල දුන්නට. object oriented programming ගැන තාම ලොකු අවබෝදයක් නෑ. මම තාම Php වලට අලුත්. පොඩි ටෙස්ටින් වගයක් කරද්දි තමයි මේ ගැටලුව ආවෙ. දැන් තේරුනා..:D:yes: