Page 35 - Open Soource Technologies 304.indd
P. 35

Unit 2: Language Basics



            2.2.2 Floating-Point Number                                                           Notes
            Floating-point numbers are also sometimes called real numbers. They are numbers that have a
            fractional component. Unlike basic math, all fractions are represented as decimal numbers. If
            you are familiar with C, PHP floating-point numbers are equivalent to the double data type.
            Floating-point numbers get their name from their decimal point. When using scientific notation
            to represent the number, the point floats in relation to the exponent being applied to the numeric
            component of the notation.
            PHP recognizes two types of floating-point numbers. The first is a simple numeric literal with
            a decimal point. The second is a floating-point number written in scientific notation. Scientific
            notation takes the form of [number] E[exponent], for example, 1.75E-2.
            Some examples of valid floating-point numbers include:
            3.14

            0.001
            -1.234
            0.314E2      //31.4

            1.234E-5   //0.00001234
            -3.45E-3   //-0.00345

                          If you want to know if a variable contains a floating-point number, you can
                          use the is_float() function to test the value. If it is a floating-point number, the
                          function will return true.
            2.2.3 String
            A string is a text literal of an arbitrary length. Most of working with Web pages is about working
            with strings. A string is indicated by being enclosed in quotes, either double or single quotes.
            Unlike some programming languages, PHP differentiates between single and double quotes.
            Strings inside double quotes are parsed or interpolated, while strings inside single quotes are not.
            What this means is that if you include variables or special characters in double-quoted strings, those
            values are processed and become part of the string. Putting variable names and special characters
            in single-quoted strings causes the variable names and special character escape sequences to be
            written out exactly as you typed them. In other words, they are literals. This means that you can
            embed variables directly inside strings in PHP when using double quotes, but not when using
            single quotes. This makes concatenating strings a little easier.

            Thus we have the following situation:
            $myVar = “xyz”;       // assign a value to $myVar
            echo $myVar;          // writes ‘xyz’

            echo “abc to $myVar”; // writes ‘abc to xyz’
            // but with single quotes
            echo ‘abc to $myVar’: // writes ‘abc to $myVar’

            This does make it easy to write PHP code out inside a string, as well as to process PHP code
            within a string.







                                             LOVELY PROFESSIONAL UNIVERSITY                                    29
   30   31   32   33   34   35   36   37   38   39   40