[PHP][Tutorial]Functions

Results 1 to 21 of 21
  1. #1
    Vous Pas Reel HotelUnderSeige is offline
    MemberRank
    Nov 2007 Join Date
    ColoradoSpringsLocation
    1,290Posts

    [PHP][Tutorial]Functions

    A function is just a name we give to a block of code that can be executed whenever we need it. This might not seem like that big of an idea, but believe me, when you understand and use functions you will be able to save a ton of time and write code that is much more readable!
    This is how you make a function :D.
    Open up notepad, or what ever you use to write your php,
    PHP Code:
    <?php
    function hello() {
    echo 
    "hello world";
    }
    hello();
    ?>
    On the first line we start the php, then on the second line we create the function "hello()", on the third line we write what the function will do, then on the fourth line we call up the function, and on the last line we close the php, Ok now if you were to execute that you should see "hello world';.
    Ok now lets say we wanted it to hello to what ever name we type in :D, so this is ho we do it
    PHP Code:
    <?php
    function hello($name) {
    echo 
    "hello $name";
    }
    hello("nameyouwanttoputin");
    ?>
    Ok so now on line 2 we added a variable, then on like 3 we echo $name, then on line 5 we execute the function with "nameyouwanttoputin", Ok so what happens is when you execute the function "hello" and put something in bweeten the (), you define $name, So if you were to exeucte hello(), it should say "nameyouwanttoputin", or what ever yoyu put bweeten the () on the 5th line.
    You could do as many variables as you like,
    PHP Code:
    <?php
     
    function hello($name$lastname) {
     echo 
    "hello $name $lastname";
     }
     
    hello("HotelUnder""Seige");
     
    ?>
    Ok so we put in 2 variables, then on the 5th line we defined them, it would echo hello HotelUnder Seige, So that was simple enough right? Well hope it helped :D
    Last edited by HotelUnderSeige; 18-01-10 at 07:15 AM.


  2. #2
    Account Upgraded | Title Enabled! Mr.Lucifer is offline
    MemberRank
    Apr 2007 Join Date
    797Posts

    Re: [PHP][Tutorial]Functions

    It looks good.

    Quote Originally Posted by Dj.flames View Post
    This was not even helpfull.
    could have made it noob friendly,
    i already know this i will be making a better tutorial with screenies on what it turns out to be,
    and how to make a program
    Something's telling me you don't know shit about what you're talking about.

  3. #3
    Member Uploadz is offline
    MemberRank
    Jan 2009 Join Date
    Uploadz.co.ccLocation
    54Posts

    Re: [PHP][Tutorial]Functions

    yeaa preet easyyy P:P
    nice tut still

  4. #4
    Account Upgraded | Title Enabled! Cancel Man is offline
    MemberRank
    Nov 2008 Join Date
    Canberra, ACTLocation
    203Posts

    Re: [PHP][Tutorial]Functions

    Quote Originally Posted by Dj.flames View Post
    What Makes You Think That Nub im Pro PHP I bet You Dont Even Know What PHP Stands For,
    Dont Mess With The Best,
    When You Chill With The Rest (Noob Rests Ahahah)
    The fact you capitalize everything.
    Oh here come some 1337 skillz.
    "i knoewz wat php iz!!!!1111!!!One"

    I've never used custom functions, so this helped me quite a bit.
    Thanks.

  5. #5
    right + down + X GhostSnyper is offline
    MemberRank
    May 2006 Join Date
    AZ, USALocation
    2,819Posts

    Re: [PHP][Tutorial]Functions

    Anyone who is knowledgeable in an area doesn't act like a bloody twat. Be humble, and thankful for what you do know... Just because YOU think it's easy and not helpful at all doesn't mean that it won't help a newbie. Also, stop using 'nub' and 'Pro.' First of all, I doubt you can tell me the difference between newb and noob without looking it up, secondly, that nub crap is stupid gaylo idiot talk, and third of all, this is a forum where 'programmers' support each other. 'Coders' are welcome, but if you can't teach it, then shut the hell up.

    Back to the topic, well done mate. Although, next time, try and include a small php web page to show them what their results would be., you know, like using the input methods to change background color, as well as displaying text. You could even show them basic input boxes, or something similar, and show them how they can use that to trigger and manipulate the method to do on the fly transformations. I'm not as good at PHP as I should, but the function is similar to the way I'd do it in java

    PHP Code:
     private final void setPageProperties (java.awt.Color colorString name) {

        
    pageBackground color// main color of page
        
    titleBarName name// sets title to "My web page, by " + name
        
    updatePage(); // updates and refreshes content to reflect these changes

    That is, assuming that I'm using a small webserver in java :3

  6. #6
    Member Guildjuhh is offline
    MemberRank
    Jan 2009 Join Date
    65Posts

    Re: [PHP][Tutorial]Functions

    Actualy this tut is stupid.

    1) Take vars out of quotes.
    2) Do not echo in a function.
    3) Use tabbing

    You haven't told about Public, private and abstract functions. This is just beginner PHP...

  7. #7
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: [PHP][Tutorial]Functions

    Quote Originally Posted by Guildjuhh View Post
    Actualy this tut is stupid.

    You haven't told about Public, private and abstract functions. This is just beginner PHP...
    I think this can actually be quite helpful for those who never before even thought of using functions. Not that it teaches much anything, but as an eye-opener.

  8. #8
    Member Guildjuhh is offline
    MemberRank
    Jan 2009 Join Date
    65Posts

    Re: [PHP][Tutorial]Functions

    Quote Originally Posted by Negata View Post
    I think this can actually be quite helpful for those who never before even thought of using functions. Not that it teaches much anything, but as an eye-opener.
    True, but it's not worthy to call a tutorial...

  9. #9
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: [PHP][Tutorial]Functions

    Quote Originally Posted by Guildjuhh View Post
    True, but it's not worthy to call a tutorial...
    That's true.

  10. #10
    Gamma RastaLulz is offline
    MemberRank
    Dec 2007 Join Date
    EarthLocation
    3,327Posts

    Re: [PHP][Tutorial]Functions

    Any tutorial is worthy, as not everyone is not an elite coder like you. I don't see why you guys oppose simple tutorials, questions etc, when you once didn't know how to do the same thing. But now that you do, you act like everyone who does not know how to do it is no worthy of knowing. Which is quite selfish of you, regardless how you found out, you should be open to helping those who don't know as much as you, or simply allowing it, rather than opposing it. Anyways, great tutorial, and I'm sure that it'll help some of the not so elite coders we have here.

  11. #11
    The Gamma..? EliteGM is offline
    MemberRank
    Jul 2006 Join Date
    NandolandLocation
    4,077Posts

    Re: [PHP][Tutorial]Functions

    I don't want to be a total dick but please also teach standards properly;
    PHP Code:
    <?php
        
    function func_Name($arg$arg2)
        {
            if (
    $arg $arg2)
            {
                return 
    false;
            }
        }
        
    ?>
    Indent with 4 spaces OR a "tab".

  12. #12
    Alpha Member john_d is offline
    MemberRank
    Feb 2004 Join Date
    PhilippinesLocation
    2,868Posts

    Re: [PHP][Tutorial]Functions

    a tip if your going to make a if, foreach, for with only one line.. no need for braces

  13. #13
    The Gamma..? EliteGM is offline
    MemberRank
    Jul 2006 Join Date
    NandolandLocation
    4,077Posts

    Re: [PHP][Tutorial]Functions

    like this?
    PHP Code:
       <?php
        
    function func_Name($arg$arg2)
        {
            if (
    $arg $arg2)
                return 
    false;
        }
        
    ?>

  14. #14
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [PHP][Tutorial]Functions

    Quote Originally Posted by john_d View Post
    a tip if your going to make a if, foreach, for with only one line.. no need for braces
    That's true. But I generally find that code harder to read. Of course it's entirely a personal matter, but I find braced code even for one-liners be more complete and clear. As long as you're consistent with your code-style :)

  15. #15
    Account Upgraded | Title Enabled! Hidden is offline
    MemberRank
    Apr 2008 Join Date
    .Location
    367Posts

    Re: [PHP][Tutorial]Functions

    he means:
    PHP Code:
    <?php
    if($hi==TRUE)
    echo 
    ' hello ' ;
    else
    echo 
    'hi';
    ?>

  16. #16
    :) Horizon is offline
    MemberRank
    Jan 2009 Join Date
    Illinois, U.S.Location
    2,381Posts

    Re: [PHP][Tutorial]Functions

    Quote Originally Posted by Guildjuhh View Post
    Actualy this tut is stupid.

    1) Take vars out of quotes.
    2) Do not echo in a function.
    3) Use tabbing

    You haven't told about Public, private and abstract functions. This is just beginner PHP...
    If you really hate this tut so much and think you could do it better, then make your own tut.

  17. #17
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [PHP][Tutorial]Functions

    Quote Originally Posted by Hidden View Post
    he means:
    PHP Code:
    <?php
    if($hi==TRUE)
    echo 
    ' hello ' ;
    else
    echo 
    'hi';
    ?>
    In which case I'd rather have brackets xD

    PHP Code:
    <?php

    if ($hi)
    {
         echo 
    'Hello';
    }
    else
    {
         echo 
    'Hi';
    }

    ?>
    For me, when code gets too compact, it's really hard to read. I even add rows of slashes (////////) just to separate things and stretch the code.

  18. #18
    Alpha Member john_d is offline
    MemberRank
    Feb 2004 Join Date
    PhilippinesLocation
    2,868Posts

    Re: [PHP][Tutorial]Functions

    other tips would be to learn how to use

    die();
    continue();

  19. #19
    Banned Monsta. is offline
    BannedRank
    Jun 2008 Join Date
    England - MerseLocation
    1,221Posts

    Re: [PHP][Tutorial]Functions

    PHP Code:
    <?php
    function mathAdd($num1$num2){
    $answer strval($num1) + strval($num2);
    return 
    $answer;
    }
    ?>

  20. #20
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP][Tutorial]Functions

    Quote Originally Posted by Hidden View Post
    he means:
    PHP Code:
    <?php
    if($hi==TRUE)
    echo 
    ' hello ' ;
    else
    echo 
    'hi';
    ?>

    I personally read simple if statements fine using this method:
    bool ? if_value : false_value;
    PHP Code:
    echo ($hi==TRUE) ? 'hello' 'hi'
    But that's just me. If I'm using a system that will always only be one line, I'll do this method. If there's a possibility I'll put in more lines in the future, I'll use a longer statement just in case. However, for a function where it only takes a condition and returns true or false.. Like a function that checks if a user is a certain rank

    PHP Code:
    function check_rank($rank,$allowed_ranks//$rank is needed, $allowed_ranks is optional array/PSL
    {
        if(
    strlen($rank)>0)
        {
            if(
    strlen($allowed_ranks)<1)
            {
                
    $allowed_ranks 'Admin';
            } 
            
            
    $allowed_ranks = ( count($allowed_ranks)>explode('|',$allowed_ranks) : $allowed_ranks );
            
            
    $bool = if(preg_match('/'.$allowed_ranks.'/i',$rank)>0;
            return 
    $bool;
        }
    }

    $permission_to_enter check_rank($_SESSION['rank'],'Admin|Mod|Registered');
    echo ( 
    $permission_to_enter 'You May Pass.' 'You Shall Not Pass!' ); 
    Notice this line:
    echo ( $permission_to_enter ? 'You May Pass.' : 'You Shall Not Pass!' );

    There's really no need for further work on this line.It's simple, if the rank is allowed, say it's allowed. If not, say it's not allowed.

    Simple, no?


    EDIT: Still, you all should look up and listen hard to Daevius, he pretty much taught me everything, so take me as a grain of salt in comparison. I'm just showing you all some other organization methods that you might personally like too ;)

    It is always nice to keep decipherable code. I remember not too long ago I didn't understand the bool?true:false; method, and saw it as Chinese or something.. Now it makes more sense than english to me.. Do whatever is the best compromise between "best for you," and "best for the world," Always ;)

  21. #21
    Alpha Member john_d is offline
    MemberRank
    Feb 2004 Join Date
    PhilippinesLocation
    2,868Posts

    Re: [PHP][Tutorial]Functions

    if statements depends on how they are needed

    Code:
    $this == true ? 'TRUE' : 'FALSE'
    i generally use inside echo or variables declerations... since it would be a waste of time to do a full if statement.

    and others like

    Code:
    if ($this == TRUE ) Die();
    else {
         echo 'it is not true';
         echo 'probably false then';
    }
    of

    Code:
    foreach ($this as $this1 => $this2) {
        if ($this2 == TRUE) continue;
        echo 'FALSE STUFF';
    
    
    }



Advertisement