Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 41

Thread: General Knowledge

  1. #11
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Depending on the context and how often you write it in a certain program, it can be useful to have a function rather than any other sort of symbols in a program. If you use it in if statements, for example, it looks a lot simpler when skimming to have if(is_even($x)) than to write if($x%2===0), even though the result is the same. If you're planning to put that in 100 places in a program then I think it makes sense to use is_even rather than %...===0. Of course if you're only using it once, that's rather silly. Much like naming certain properties (such as in CSS) with a reasonable name for their use, a well-suitably named function can make the code appear a lot smoother than a bunch of math symbols, much like CSS should not contain a number of properties named "prop1," "prop2," ... "propn."
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #12
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    On the contrary: if ($x % === 0) is exactly the sort of place where there is no point in having a function. It is useful in languages that don't have a way of treating an operator as a function to wrap operators in functions if one is intending to pass them to higher-order functions, but apart from that there is no reason.

    What you suggest, functions as documentation, is again usually a good idea, but this particular instance is so common that, like addition, there's simply no gain. Any programmer worth their salt will recognise it instantly.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #13
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I agree with Twey.
    Once you get to something so short such as:
    Code:
    return $x % 2 === 0;
    It is unreasonable for a function, and it is completely comprehensible.
    Jeremy | jfein.net

  4. #14
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Just for clarity, the three equals signs (===) do mean that both operands have to be the same type as well as equalling the same value right?

    For example:

    1 == "1" // Would return true?

    and

    1 === "1" // Would return false?

  5. #15
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

  6. #16
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Correct.
    PHP Code:
    <?php
    function single($n) {
      return 
    $n == 5;
    }
    function 
    dbl($n) {
      return 
    $n === 5;
    }
    echo 
    single('5');
    echo 
    dbl('5');
    ?>
    Jeremy | jfein.net

  7. #17
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Smile

    So, I did something wrong here the even function was working but then it stopped after I added in a check for a new month and it now brings up the only accepts integers error.

    Maybe too many date functions in on process (I have 4 date functions)? I'm using php4 also if that makes a difference.

    I'm switching servers in the next few days anyone know if a small orange company is good or now of a good one to use? I have 3 domains I need to swap over. My current server IX webhosting has crashed my domains 3+ times today and denies it's happening (2 techs, 1 said he knew) so I can't stay with them.

    It also did occur to me that I would need a way to get the php to write even when the page was not loaded so that at the new month point it would write regardless of if a visitor was on the page at 11:59:59 or not.

    PHP Code:
    $date2 date('m.j.y'); 
    function 
    is_even($int) {
        if ( !
    is_int($int) ) {
            
    trigger_error('The is_even() function only accepts integers.'E_USER_WARNING);
            return;
                   
    // Or get rid of trigger_error(xxx) and replace it with: return false;
                   // to accept any input. This will cause odd numbers and decimals
                   // to return false.
            
        
    }
            
        
    $result $int == true false;
        
        return 
    $result;
        
    }  
    $int date('j');
    if ( 
    is_even($int) ) {
           
    $first '<tr class="even"><td>';
    } else {
           
    $first '<tr class="odd"><td>';  
    }   
    $newmonth date('m.d H:i:s');
    if (
    $newmonth == "1/31 23:59:59" || $newmonth == "2.28 23:59:59" || $newmonth == "2.29 23:59:59" || $newmonth == "3.31 23:59:59" || $newmonth == "4.30 23:59:59" || $newmonth == "5/31 23:59:59" || $newmonth == "6/30 23:59:59" || $newmonth == "7/31 23:59:59" || $newmonth == "8/30 23:59:59" || $newmonth == "9/30 23:59:59" || $newmonth == "10/31 23:59:59" || $newmonth == "11/30 23:59:59" || $newmonth == "12/31 23:59:59") {
        
    $who "<tr><td class=\"row\" colspan=\"3\"> NEW MONTH</td></tr>" $first $entervalue "</td><td>" $date1 "</td><td>" $date2 "</td><td>" $user "</td><td>" $computer "</td></tr>\n</table>\n";
        } else {
        
    $who $first $entervalue "</td><td>" $date1 "</td><td>" $date2 "</td><td>" $user "</td><td>" $computer "</td></tr>\n</table>\n";
    }
    $where str_replace("</table>"$who$red);
    $logger fopen($info"w+"); 
    fwrite($logger$where); 
    fclose($logger); 
    Thanks for any ideas and thoughts you can offer with these questions as well.

  8. #18
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Probably you had a string like '2' or a float like 2.4. You don't actually want to only accept integers here, because of PHP's weak typing: just accept whatever you're given. Remove that clause from the function entirely.
    Code:
        $result = $int % 2 == 0 ? true : false;
        
        return $result;
    is just pleonastically redundant and redundantly pleonastic, and can be written return $int % 2 == 0;.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #19
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    $int = date('j');

    The date() function returns a string. So you have two options, either make, $int an integer before using it in the even() function, or change the function.

    You can make the result of the date function an integer by doing this:

    PHP Code:
    $int = (int) date('j'); // typecast date('j') to an integer 
    Or, you can use Twey's function, but remember it will accept strings and return true.

    PHP Code:

    // $int = "this is a string"; will return true

    function is_even($int) {

        return 
    $int == 0;



  10. The Following User Says Thank You to JasonDFR For This Useful Post:

    bluewalrus (02-03-2009)

  11. #20
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Oh yea that makes sense thanks again.

    Twey I just saw your response, won't the date always be a whole integer?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •