Hi folks.

I am currently in the process of learning about MYSQL.

I am working through a chapter in a book I am reading.

While reading this chapter there was a number of things that didn't make much sense to me.

I just have a number of random questions I need help with, below:

1) what is the difference between a 'float', 'double' and a 'decimal (precision scale)' in php? Because I always thought they were the same thing? And the book I am reading just confuses matters. I just need to know (without too much jargon), what the differences are. And what is the difference between 'decimal (precision scale)' and just standard 'decimal'?

2) In the book I am reading it explains 'Date and Time Data Types'. And I really do not understand the meaning behind: TIME.
In the book it explains that its allowed range of values are: – 838:59:59 to 838:59:59
I don't understand what these numbers mean.

3) I just need to ask one question about the 'try block' syntax. In the example below:

try {
$conn = new PDO( $dsn, $username, $password );
$conn- > setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo “Connection failed: “ . $e- > getMessage();
}

.. in this snippet of code, i am not 100% sure of the meaning of:

} catch ( PDOException $e ) {

...... to be more specific:

PDOException $e

.... now this is just a guess of mine, but is the:

PDOException

.. in:

PDOException $e

used for 'hinting' the variable '$e'?

4) In the book I am reading it talks about the value NULL.
I always believed that NULL meant the same as false or '0'. But now I don't know what to believe. I was reading a section about MYSQL operators. To be more specific, this:

<=>

... in the book, this operator is described as the "NULL-safe version of equal to".

it also states the following:

< = > is useful if you think either of the values you ’ re
comparing might be NULL. Remember that NULL values propagate throughout an expression, so if any
value in an expression is NULL , the result will also be NULL . This isn ’ t very helpful when you ’ re trying to
compare two values.

... how are NULL values spread across an expression? This sounds very complicated.

... can you also explain the differences between NULL and FALSE?

5) This question is about 'abstract classes'. In the book I am learning php from, they use an abstract class in an application I learned how to build. However, none of the methods in the abstract class were declared abstract. Am I correct in thinking that the only affect this will have is:

  • none of the methods inside the abstract class will be abstract?
  • and the only affect that will occur will be that you can’t instantiate (create objects from) the abstract class directly.

Paul.

    NULL more properly means "the absence of a value". In databases you can't compare NULL to another value because NULL doesn't have a value to compare with. "How old are you?" "Twelve." "How old is the other guy?" "Dunno." You can't say which of the two is the elder.

    ... how are NULL values spread across an expression?

    The bit you quote says "Remember that..." so it probably mentions it earlier.

    NULL is not like FALSE; FALSE provides one bit of information - NULL doesn't provide any. Is the other guy old enough to get married? You can't tell.

      Paul help!;11051673 wrote:

      1) what is the difference between a 'float', 'double' and a 'decimal (precision scale)' in php? Because I always thought they were the same thing? And the book I am reading just confuses matters. I just need to know (without too much jargon), what the differences are. And what is the difference between 'decimal (precision scale)' and just standard 'decimal'?

      This sounds as if you're a bit confused between "what is PHP" and "what is SQL", or maybe "what is MySQL" or "what is MS-SQL".

      MySQL has several number types. MS-SQL/Transact SQL has several also, and they are, possibly, more complex --- for example, you can specify the amount of precision (number of digits to the right of the decimal point) you wish to store. More on that later.

      Both of these (MySQL, MS-SQL) are Relational DataBase Management Systems (RDBMS), and they must, to adhere to the ANSI SQL standard, have strict type definitions. In computing (especially in languages) this is known as "strict typing".

      In contrast, PHP is a programming/scripting language that is "loosely typed". You do not have to specify what "type" a variable is ... you just have to declare it, and PHP decides what to call it based on a sort of "looks like a ducks, walks like a duck, talks like a duck, it must be a duck" principle known as ype coercion.

      I mentioned "precision" above ... your question about "decimal (precision scale)". In US money, for example, the smallest coin is a penny or "one cent", 1/100th of a dollar, so in the US when we think of decimal numbers we often think of something like "5.99" or "4,000.57". However, a thermometer might be graded in 1/10th of a Fahrenheit degree ("98.6"), and a precision scale in a laboratory might be graded in 1/1000 of an ounce or pound (or more likely a kilogram), so that's like "1.035" kg. In some systems you need to tell your SQL server in advance how many places you want to the right of the decimal ... that's the "precision" you desire for your number.

      HTH,

        Paul help! wrote:

        2) In the book I am reading it explains 'Date and Time Data Types'. And I really do not understand the meaning behind: TIME.
        In the book it explains that its allowed range of values are: – 838:59:59 to 838:59:59
        I don't understand what these numbers mean.

        Judging from the format and the name "time", I'm going to guess they represent hours, minutes and seconds.

        11.3.2 The TIME Type

        MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or 'HHH:MM:SS' format for large hours values). TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).

        http://dev.mysql.com/doc/refman/5.6/en/time.html

        Which is a bit silly, since a time interval is a different type of entity from a single point in time (SQL Server users would use an integer for the job, and PostgreSQL has an "interval" type which can record durations of up to 178 megayears to microsecond precision).

        Why 838 hours? It would seem that MySQL's developers allowed the internal workings of the software to leak through in some arbitrary way.

          Thanks for the help guys.

          Can someone help me out with my questions '3' and '5' (above)?

          Paul.

            Paul help! wrote:

            used for 'hinting' the variable '$e'?

            Not quite: a type hint would require that the argument be of the declared type, whereas for exceptions the idea is that the first catch block with a type that matches the type of the exception thrown or propagated would cause control to enter the code within the catch block. This way, you can handle those exceptions that you can handle at that point, and for those you cannot, you allow them to propagate.

            Paul help! wrote:

            5) This question is about 'abstract classes'. In the book I am learning php from, they use an abstract class in an application I learned how to build. However, none of the methods in the abstract class were declared abstract. Am I correct in thinking that the only affect this will have is:

            • none of the methods inside the abstract class will be abstract?
            • and the only affect that will occur will be that you can&#8217;t instantiate (create objects from) the abstract class directly.

            Yes.

              19 days later

              thanks guys.

              I suppose what I find confusing about my 'question 2' (above) is that - I do not understand what happens when: 838:59:59 has passed by?

              does it just reset itself to the beginning and start over like it would if it was a typical 24 form of time?

                According to the manual page I linked to earlier:

                By default, values that lie outside the TIME range but are otherwise valid are clipped to the closest endpoint of the range. For example, '-850:00:00' and '850:00:00' are converted to '-838:59:59' and '838:59:59'.

                  Paul help!;11051673 wrote:

                  1) what is the difference between a 'float', 'double' and a 'decimal (precision scale)' in php? Because I always thought they were the same thing? And the book I am reading just confuses matters. I just need to know (without too much jargon), what the differences are. And what is the difference between 'decimal (precision scale)' and just standard 'decimal'?

                  I've never heard the term decimal (precision scale) myself. One thing I have noticed is that every time you start a new project with new collaborators, the jargon you share changes a little. A float is a floating-point decimal number. It could be 0.5 or it could be 9.87654321 x 10-38. The basic idea is that you have a mantissa and an exponent to specify some number. The exponent gives you an idea of scale: how tremendously big or tremendously small is the value we are representing? The mantissa gives you some sense of its accuracy and value. You may have encountered this idea as 'scientific notation' in mathematics classes. It's especially useful in computing because numbers are most conveniently represented using some fixed number of bytes, usually 4 but sometimes 8, depending on your hardware and operating system. A double is a float is usually more accurate because it is specified with twice the number of bytes.

                  Paul help!;11051673 wrote:

                  2) In the book I am reading it explains 'Date and Time Data Types'. And I really do not understand the meaning behind: TIME.
                  In the book it explains that its allowed range of values are: &#8211; 838:59:59 to 838:59:59
                  I don't understand what these numbers mean.

                  I think dalecosp is right that this question seems to be referring to MySQL concepts rather than PHP concepts. As someone who has coded PHP for about 12 years, I will warn you that time in coding is persistently complicated by time zones, leap years, leap seconds, implicit time conversions, and lots of other stuff. Always keep in mind that our common ways to express time -- "it's 8am" -- are relative and tend to assume some particular time zone (not to mention some planet/solar system/galaxy/local cluster/universe). You should probably try and first understand the closest thing we have to a universal way of expressing time which is sometimes called Unix Time.

                  Paul help!;11051673 wrote:
                  catch (PDOException $e) {

                  This bit of code says this catch block will catch only exceptions of type PDOException or exception types that extend PDOException. You can define numerous catch blocks for a try/catch statement so that you can respond more specifically to all different types of exceptions differently. E.g., you might do one thing if it's a PDOException ("your database is acting up!") or OutOfMemoryException ("you're out of memory"). Dealing with exceptions is all about describing what went wrong and routing your program's behavior accordingly.

                  Paul help!;11051673 wrote:

                  4) In the book I am reading it talks about the value NULL.
                  I always believed that NULL meant the same as false or '0'. But now I don't know what to believe.

                  This is a bit of an oversimplified explanation but NULL is the value that gets assigned to variables if you haven't yet assigned anything to them yourself. Not that you'd ever do this in an actual program but it demonstrates what I mean:

                  <?php
                  $foo;
                  var_dump($foo);
                  

                  The output:

                  NULL

                  Or this is a more feasible example. You define a class with some var in it and instantiate the new object. If you don't assign your class vars any value, they will be NULL:

                  <?php
                  class myClass {
                    public $foo;
                  }
                  $obj = new myClass();
                  var_dump($obj);
                  

                  The output:

                  object(myClass)#1 (1) {
                    ["foo"]=>
                    NULL
                  }
                  

                  You might also fetch records from a DB and some of the columns in your db record might have a NULL value. Or you might call a function that returns a NULL result

                  You can distinguish between NULL and FALSE using strict equality (=== instead of ==):

                  <?php
                  if (NULL == FALSE) echo "equal\n";
                    else echo "not equal\n";
                  
                  if (NULL === FALSE) echo "strictly equal\n"; // note the extra "="
                    else echo "not strictly equal\n";
                  

                  the output:

                  equal
                  not strictly equal
                  

                  and sometimes this is very important. Try this same code but replace FALSE with 0. Then check FALSE for == and === with 0. It's important to know when to check for equality and strict equality sometimes. You can also use [man]is_null[man] or [man]empty[/man] and [man]isset[/man] and possibly other functions. Only experience will help you understand when to check for what.

                  Paul help!;11051673 wrote:

                  5) This question is about 'abstract classes'. In the book I am learning php from, they use an abstract class in an application I learned how to build. However, none of the methods in the abstract class were declared abstract. Am I correct in thinking that the only affect this will have is:
                  none of the methods inside the abstract class will be abstract?
                  and the only affect that will occur will be that you can&#8217;t instantiate (create objects from) the abstract class directly.

                  As laserlight said, both statements are true. The point of defining an abstract class is that it's meant to be extended by other classes. It's similar to an [man]interface[/man] but it allows you to define methods specifically.

                    sneakyimp wrote:

                    I've never heard the term decimal (precision scale) myself.

                    You may know it as "fixed point". Precision is the number of digits, scale is how many of those digits are to the right of the decimal point. It's an SQL thing.

                      Paul help!;11051673 wrote:

                      4) In the book I am reading it talks about the value NULL.
                      I always believed that NULL meant the same as false or '0'. But now I don't know what to believe.

                      Null is the value when there is no value. That's right. Say it again. It's the value when there is no value. (as demonstrated by sneaky's post)

                      The reason NULL operates similarly to false or 0 is because PHP will perform type conversion on the fly, and NULL is considered "falsey". What that means is when NULL is used in comparisons (using ==), PHP will automatically convert it to false.

                      Confused?

                      EDIT: Also wanted to include another example of conversion-on-the-fly:

                      $age = 30;
                      echo $age;
                      // Outputs 30
                      

                      The language construct "echo" requires a string, but $age is an integer. The reason this works is because PHP converts $age to a string on the fly, so echo just spits it out.

                      Here is a link to a great article in the PHP manual about comparisons and types: Link

                      sneakyimp;11052261 wrote:

                      It's similar to an [man]interface[/man] but it allows you to define methods specifically.

                      The other major difference is you can implement multiple interfaces but only extend one abstract class.

                        Write a Reply...