[PHP][Tutorial]Functions

Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #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.

  2. #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.

  3. #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();

  4. #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;
    }
    ?>

  5. #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 ;)

  6. #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';
    
    
    }



Page 2 of 2 FirstFirst 12

Advertisement