• home
  • forum
  • my
  • kt
  • download
  • Monkey PHP: Lets Get Logical

    Author: 2008-08-23 12:27:40 From:

    One of the main concepts in prodecural and object oriented programming languages is using logic statement. Basically logic statements ask whether a certain value is equal to another value. The tutorial today will cover just some of the basics. More will be covered in this topic within the next tutorial.

    Let's Get Logical!
    In most programming languages the if statement is the main logic test. The example below will check the value of $num to see it its value is equal to 5. We use an echo statement here to output a message. Echo and other functions will be covered later in the lessons.

    Code Box
    if ( $num == 5 ) {
        // Anything between the opening { and the closing } is what
        // happens when $num is 5.
        echo "The variable was set to 5";
    };

    If we needed to do something else if the value wasn't 5 then here's what we do. This is typically called an if-else statement.

    Code Box
    if ( $num == 5 ) {
        echo "The variable was set to 5";
    } else {
        echo "The variable was NOT set to 5";
    };


    So far we have checked if a number is of a certain value. We can also check the true / false value of functions or boolean variables.

    Code Box
    if ( call_home() ) {
        echo "Call home was success";
    };

    In the above example we are running a function (learn later tutorial) and only if the function runs successfully will the output be displayed.

    Code Box
    if ( $learner ) {
        echo "Learner is set";
    };

    In the code above we are basically checking if the variables learner has a value other than "false" or 0. So for any other value stored in $learner then the output is produced.

    A few more examples...

    Code Box
    if ( $num >= 5 ) {
        // This time we are checking whether $num contains a value of 5 or above.
    };


    Code Box
    if ( $num < 5 ) {
        // This time we are checking whether $num is less than 5.
    };


    Code Box
    if ( $num != 5 ) {
        // This time we are checking to make sure that $num ISNT equal to 5.
    };


    Code Box
    if ( $num = call_home() ) {
        // This kind of logical statement can be confusing.
        // But you can come back to look at this
        // once you have fully learnt about functions.
        // Basically this is checking whether the function
        // named "call_home" is returning a value other than 0 or false.
        // At the same time we are also storing the value returned
        // by the function into the variable $num.
    };

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (15)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (54)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot