Welcome to geekForum.com! Log in or Sign up to interact with the community.

PHP Intro (Data Types and Manipulation)

Discussion in 'Coding Forum' started by The Craw, Oct 31, 2011.

  1. The Craw Member

    Member Since:
    Oct 29, 2011
    Hey guys. This is part one of the tutorials I plan on writing for this site, in which I'll be focusing on PHP data types and a few ways of manipulating them.

    [hr]

    The first data type is called "boolean", and it is one of the simplest data types. We'll run through some simple examples of using it and I'll explain as we go.

    In this example, we're setting a variable $correct_answer to either TRUE or FALSE, depending on the whether a given mathematical expression is correct or incorrect.
    Code:
    <?php
      if (1+1 == 2)
      {
        $correct_answer = TRUE;
      }
      echo $correct_answer;
    ?>
    
    Okay, so the above code checks to see if the math is correct, and if it is, it sets the value TRUE to the variable $correct_answer. Then it outputs the value with the echo function. Now let's try one with an incorrect conditional.
    Code:
    <?php
      if (10-1 == 86000)
      {
        // We know this will never pass. :P
        $incorrect_answer = FALSE;
      }
      echo $incorrect_answer
    ?>
    
    In the above example, we see a comment string just before the variable assignment. Any time you put "//" in PHP, the rest of the that code line becomes a comment. Nothing in a comment is actually executed, so you can put anything you want in there, and not worry about it.

    [hr]

    Now for the next data type Integers. The the title of this one should be pretty self explanatory. Integers can be any whole number including negative numbers.

    In this example, we're going to be a little more complicated than that last, and assign the value to a variable before the conditional, so we can use it for comparing values.
    Code:
    <?php
      $our_int = 12;
    
      // Since $our_int does equal 12, we get to output the text.
      if ($our_int == 12)
      {
         echo 'Hey, what do ya know?! We got it right. :D';
      }
    ?>
    
    Any questions? No? I thought not. :p

    The next example compares two values, but we won't use a variable this time.
    Code:
    <?php
      if (-10 == 10)
        echo 'yay!';
    ?>
    
    This example will not output anything, since the conditional failed to pass. Obviously, negative 10 is not the same as 10. :p Also! You may have noticed that I didn't use curly brackets on the conditional statement this time. Whenever you're executing one function after a conditional, you don't need the brackets. But if you wanted to use "echo" twice for example, then you would need the brackets, see?

    [hr]

    Okay, this post is getting a little long, so I'll post the other two data types tomorrow. Stay tuned for more info! ::)
  2. Clara Listensprechen Member

    Member Since:
    Jul 14, 2011
    Question...

    I've encountered comments in the format of /* comment */ as well as // comment //

    Both are valid, or is one a peculiarity of a particular version of PHP?
  3. The Craw Member

    Member Since:
    Oct 29, 2011
    PHP has many types of comments you can use. It's up to you to choose which one you like best. Personally I like the double slash "//".

    Code:
    <?php
    // This is a one line comment.
    
    /*
    This
    is
    a
    multi-line
    comment
    */
    
    # Shell style comment.
    ?>
    
    All of the above comments are valid, but the shell style one "#" isn't recommended, as some servers don't support it from what I've heard.
  4. The Craw Member

    Member Since:
    Oct 29, 2011
    It might be a few days before I can get to the next part of the tutorial. I'm pretty swamped with work right now. :-X
  5. Dukie New Member

    Member Since:
    Jun 13, 2011
    [quote author=The Craw link=topic=191.msg859#msg859 date=1320275134]
    It might be a few days before I can get to the next part of the tutorial. I'm pretty swamped with work right now. :-X
    [/quote]

    No problem :) take your time :) this is great!
  6. Clara Listensprechen Member

    Member Since:
    Jul 14, 2011
    [quote author=Dukie link=topic=191.msg860#msg860 date=1320287166]
    [quote author=The Craw link=topic=191.msg859#msg859 date=1320275134]
    It might be a few days before I can get to the next part of the tutorial. I'm pretty swamped with work right now. :-X
    [/quote]

    No problem :) take your time :) this is great!
    [/quote]
    Agreed.

    In other languages, the hashmark typically designates a hexidecimal number, which is why I'm surprised that it's used as a comment at all.
  7. The Craw Member

    Member Since:
    Oct 29, 2011
    [quote author=Clara Listensprechen link=topic=191.msg861#msg861 date=1320292843]
    [quote author=Dukie link=topic=191.msg860#msg860 date=1320287166]
    [quote author=The Craw link=topic=191.msg859#msg859 date=1320275134]
    It might be a few days before I can get to the next part of the tutorial. I'm pretty swamped with work right now. :-X
    [/quote]

    No problem :) take your time :) this is great!
    [/quote]
    Agreed.

    In other languages, the hashmark typically designates a hexidecimal number, which is why I'm surprised that it's used as a comment at all.
    [/quote]

    I'm not sure how hexadecimal numbers work, but you can read more about defining integers here.
  8. Clara Listensprechen Member

    Member Since:
    Jul 14, 2011
    In php (and css and html etc), hexadecimal numbers are typically used to specify colors, and are typically in 6 digit format. There is a 3 digit "shorthand" form. A single hexadecimal digit runs from 0 to F, and in the 6 digit color designation, the two most significant digits = red element of the RGB combination. The least two significant digits = blue while the middle 2 = green. In the 3-digit "shorthand", each digit adjusts each element, respectively.
  9. The Craw Member

    Member Since:
    Oct 29, 2011
    Interesting. I had just memorized the common ones without actually knowing how it worked. Good ta' know.
  10. Clara Listensprechen Member

    Member Since:
    Jul 14, 2011
    Hehehe--two of the most common, of course, are black and white (000000 and FFFFFF respectively). Hexadecimal works just like decimal except you don't stop at 9 to go to 10. You go 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F and then 10.

    And yeah, I'm fluent in hexadecimal. :)

Share This Page