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

Unit 3: Flow-Control Statements and PHP in Web Page



                                                                                                  Notes
            Self Assessment
            Choose the correct answer:
               1.  Which one is not a conditional statement?
                 (a)  if                         (b)  for
                 (c)  else                       (d)  elseif
               2.  The default keyword is a catch-all case that marks the point to begin execution of none of
                 the conditions being tested for is met.
                 (a)  True                       (b)  False
               3.  The statement that exit immediately to the end of the statement block is called .............
                 (a)  default                    (b)  switch
                 (c)  break                      (d)  None of these.
               4.  The statement that catch-all case that marks the point to begin execution of none of the
                 conditions being tested for is met is called .............
                 (a)  default                    (b)  switch
                 (c)  break                      (d)  none of these.

            3.2 Advance Conditional


            When working with comparing strings on computers we sometimes have to deal with more
            complicated comparisons than a simple conditional. This is because computers expect to be
            working with numbers when comparing values. In PHP, when comparing a string to a number,
            the string will first be cast to a number. This means that all strings that do not begin with a
            numeric value will be cast to zero. For instance, if (‘PHP’ > 5) will actually be compared as if
            (0 > 5) this can be a problem.
            Even when working with strings, computers will, unless told otherwise, compare the numeric
            values of the individual characters in the string until the condition in question is determined. This
            means that without any other information, most computers tend to sort strings into ASCII order.
            In ASCII, upper-case and lower-case letters have different values, so the sort is case-sensitive. For
            instance ‘BAT’ will compare to be less than ‘cat’, even though B comes before C in the alphabet,
            because the B is in upper case. This is not always what we want.
            Even worse if we compare numeric strings when we want to compare their numeric values; if
            we do not cast them to numbers first, we can get very odd results. For instance, the expression
            (‘2’ > ‘100’) will evaluate to true because in the first position 2 is greater than 1. The other character
            positions are irrelevant since the first string only has one character.
            Normally comparisons are also limited to equality and difference. Which is to say, we can
            test for equality, inequality, and the direction of inequality (greater than or less than), and
            that is it. What happens if we want to test whether two things are similar? Or whether two
            words sound the same?
            This looks as other ways to compare strings, both for non-ASCII sorts and for comparisons that
            test more than equality or direction of difference.
            3.2.1 Comparing Strings
            If you want to make sure you are always comparing strings, rather than using comparison
            operators, you should use string comparison functions. Then ensure that all arguments are cast
            to strings before comparing them and allow you to control the method in which the comparison
            occurs.



                                             LOVELY PROFESSIONAL UNIVERSITY                                    59
   60   61   62   63   64   65   66   67   68   69   70