WEB TECHNOLOGY (PHP)
Unit - I
    INTRODUCTION
    1. Origin of PHP
        PHP is a recursive acronym for "PHP: Hypertext Preprocessor".
        PHP is a server side scripting language that is embedded in HTML. It is used to manage
           dynamic content, databases, session tracking, even build entire e- commerce sites.
        It is integrated with a number of popular databases, including My SQL, Postgre SQL,
           Oracle, Sybase, Informix, and Microsoft SQL Server.
        PHP is pleasingly zippy in its execution, especially when compiled as an Apache module
           on the Unix side. The My SQL server, once started, executes even very complex queries
           with huge result sets in record-setting time.
        PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4
           added support for Java and distributed object architectures (COM and CORBA), making
           n-tier development a possibility for the first t.
        PHP is forgiving: PHP language tries to be as forgiving as possible.
        PHP Syntax is C-Like.
        Common uses of PHP.
    2. PHP is better than its alternatives
        It is faster to code and faster to execute.
        The same PHP code runs unaltered on different Web servers and different
           operation systems.
Web Technology                                                                                  Page 1
        Additionally, functionality that is standard with PHP is an add-on in
           other environments. A more detailed argument follows.
        PHP is free. Anyone may visit the PHP Web site and download the complete source
           code. Binaries are also available for Windows. The result is easy entry into the
           experience. There is very little risk in trying PHP, and its license allows the code to be
           used to develop works with no royalties.
        This is unlike products such as Allaire's Cold Fusion or Every ware's Tango
           Enterprise that charge thousands of dollars for the software to interpret and serve
           scripts. Even commercial giants like Netscape and IBM now recognize the
           advantages of making source code available.
        PHP runs on UNIX, Windows 98, Windows NT, and the Macintosh. PHP is designed
           to integrate with the Apache Web Server.
        Apache, another free technology, is the most popular Web server on the Internet and
           comes with source code for UNIX and Windows. Commercial flavors of Apache
           like Web Ten and Stronghold support PHP, too.
        But PHP works with other Web servers, including Microsoft's Internet Information
           Server. Scripts may be moved between server platforms without alteration. PHP
           supports ISAPI to allow for the performance benefits of tightly coupling with Microsoft
           Web servers.
    3. How PHP works with the web server
        The PHP software works with the web server, which is the software that
           delivers web pages to the world.
        When you type a URL into your web browser‘s address bar, you‘re sending
           a message to the web server at that URL, asking it to send you an HTML file.The web
           server responds by sending the requested file. Your browser reads the HTML file and
Web Technology                                                                                     Page 2
           displays the web page.
        You also request a file from the web server when you click a link in a webpage. In
           addition, the web server processes a file when you click a web page button that submits
           a form. This process is essentially the same when PHP is installed. You request a file,
           the web server happens to be running PHP, and it sends HTML back to the browser,
           thanks to the programming in PHP.
        More specifically, when PHP is installed, the web server is configured to
           expect certain file extensions to contain PHP language statements.
        Often the extension is .php or .phtml, but any extension can be used. When the web
           server gets a request for a file with the designated extension, it sends the HTML
           statements as is, but PHP statements are processed by the PHP software before they‘re
           sent to the requester.
        When PHP language statements are processed, only the output, or anything printed
           to the screen is sent by the web server to the web browser.
          The PHP language statements, those that don‘t produce any output to the screen,
           aren‘t included in the output sent to the browser, so the PHP code is not normally seen
           by the user.
        For instance, in this simple PHP statement, <?php is the PHP opening tag, and ?>
           is the closing tag.
        <?php echo "<p>Hello World</p>"; ?>
        Here, echo is a PHP instruction that tells PHP to output the upcoming text. The
           PHP software processes the PHP statement and outputs the following:
        <p>Hello World</p>
        That regular HTML statement is delivered to the user‘s browser. The browser
           interprets the statement as HTML code and displays a web page with one
           paragraph
           — Hello World.
Web Technology                                                                                   Page 3
            The PHP statement isn‘t delivered to the browser, so the user never sees any
             PHP statements. PHP and the web server must work closely together.
    4. Hardware and Software Requirements and installation
       These are the following the software requirements
        WAMP Server
        LAMP Server
        MAMP Server
        XAMPP Server
     All these types of software automatic configure inside operating system after installation it
    having PHP, My SQL, Apache and operating system base configuration file, it doesn't need to
    configure manually.
    Server       Stands for
    WAMP            Microsoft window o/s, Apache Mysql PHP LAMP
                    Linux Operating System Apache Mysql
                    PHPMAMP Mac os Apache Mysql PHP
    XAMPP        x-os(cross operating system) Apache Mysql PHP Perl
    Hardware Requirements
        If you would like to use a dedicated hosting server for running PHPKB Knowledge Base
             Software or host it on a local server, you can use hardware requirements and example
             specifications below for a reference.
        Recommended Hardware Specifications
Web Technology                                                                                  Page 4
        On small instances, server load is primarily driven by peak visitors however you may
           use the following specifications for optimal performance.
        5 Concurrent Users              25 Concurrent Users           50 Concurrent Users
        1 GHz CPU
        2 GB RAM
        1 GB disk space
        Dual 2 GHz CPU
        4 GB RAM
        1 GB disk space
        Dual 2.6 GHz CPU
        8 GB RAM
        4 GB disk space
    The PHP Installer
        Although an installer is available from php.net, I would recommend the manual
           installation if you already have a web server configured and running.
        Manual Installation
        Manual installation offers several benefits:
        backing up, reinstalling, or moving the web server can be achieved in
           seconds (see 8 Tips for Surviving PC Failure) and
        you have more control over PHP and Apache configuration.
    Step 1: Download the files
        Download the latest PHP 5 ZIP package from www.php.net/downloads.php
Web Technology                                                                               Page 5
        As always, virus scan the file and check its MD5 checksum using a tool such as f sum.
           Step 2: Extract the files
        We will install the PHP files to C:\php, so create that folder and extract the contents
           of the ZIP file into it.
        PHP can be installed anywhere on your system, but you will need to change the
           paths referenced in the following steps.
    Step 3: Configure php.ini
         Copy C:\php\php.ini-development to C:\php\php.ini. There are several lines you
            will need to change in a text editor (use search to find the current setting).
         Where applicable, you will need to remove the leading semicolon
            to uncomment these setting.
    Define the extension directory:
     extension_dir = "C:/php/ext"
    Enable extensions.
    This will depend on the libraries you want to use, but the following extensions should be
    suitable for the majority of applications:
    extension=curl
    extension=gd2
    extension=mbstring
    extension=mysql
    extension=pdo_mysql
Web Technology                                                                                     Page 6
    extension=xmlrpc
    If you want to send emails using the PHP mail() function, enter the details of anSMTP server
    (your ISP‘s server should be suitable):
    [mail function]
    ; For Win32 only.
    SMTP =
    mail.myisp.com
    smtp_port = 25
    ; For Win32 only.
    sendmail_from = my@emailaddress.com
    Step 4: Add C:\php to the path environment variable
        To ensure Windows can find PHP, you need to change the path environmentvariable.
        Open Settings, type ‗environment variables‘ into the search field and openthe result.
        Select the ―Advanced‖ tab, and click the ―Environment Variables‖ button.
        Scroll down the System variables list and click on ―Path‖ followed by the ―Edit‖
           button. Click ―Edit text‖ and add ;C:\php to the end of the Variable value line
           (remember the semicolon).
Web Technology                                                                                   Page 7
     5. PHP pros and cons
     5.1. PHP : past, present, future(PHP3.O,PHP4.O,PHP5.O)
        PHP3 is oldest stable version and it was pure procedural
           language constructive like C
        Whereas PHP4 have some OOPs concept added like class and object
           withnew functionality
        PHP5 approximately all major oops functionality has been added along withbelow thing
        Implementation of exceptions and exception handling
        Type hinting which allows you to force the type of a specific argument
        Overloading of methods through the               call function
        Full constructors and destructors etc through a              constuctor and
              destructor function
            autoload function for dynamically including certain include files
           depending on the class you are trying to create.
        Finality : can now use the final keyword to indicate that a method cannot be
           overridden by a
        child. You can also declare an entire class as final which prevents it from having
           any children at all.
        Interfaces & Abstract Classes
        Passed by Reference :
        An       clone method if you really want to duplicate an object
        Numbers of Functions Deprecated or removed in PHP 5.x like
           ereg,ereg_replace,magic_quotes, session_register,register_globals, split(),
           call_user_method() etc
Web Technology                                                                                Page 8
    6 .Strength of Php
            Supports database connectivity. PHP can access over 20 different databases including My Sql,
            Oracle, and MS Access.
             Supports sessions. PHP can generate unique session IDs. The ID follows the user during
            a single session on a web site. This simplifies creating shopping cart applications and
            tracking user's behaviors.
             Eliminates client configuration problems. With PHP there is no need to worry if
            the client has the appropriate software installed, since the application is executed on
            the server.
             Reduces development time. Even a newcomer can begin developing PHP
            applications in hours. Yet PHP contains many advanced features for professional
            programmers.
             Maintains source code security. The user does not see your source code as they do
            with JavaScript.
    7.   Basic PHP Development7. 1 How
    PHP script work
          PHP always start with a browser making a request for a web page.
          This request is going to hit the web server.
          The web server will then analyze it and determine what to do with it.
          If the web server determines that the request is for a PHP file (often index.php
            ), it‘ll pass that file to the PHP interpreter.
          The PHP interpreter will read the PHP file, parse it (and other included files) and then
            execute it.
          Once the PHP interpreter finishes executing the PHP file, it‘ll return an output. The web
            server will take that output and send it back as a response tothe browser.
Web Technology                                                                                        Page 9
     7. 2 Basic PHP syntax
          PHP script starts with the <?php and ends with the ?> tag.
          The PHP delimiter <?php and ?> in the following example simply tells the PHP engine to
            treat the enclosed code block as PHP code, rather than simple HTML.
    Syntax :
    <?php
    // Some code to be executed echo
    "Hello, world!";
    ?>
          Every PHP statement end with a semicolon (;) — this tells the PHP engine that the
            end of the current statement has been reached.
    Comment Lines
          A comment is simply text that is ignored by the PHP engine. The purpose of comments
            is to make the code more readable.
          It may help other developer (or you in the future when you edit the source code)
            to understand what you were trying to do with the PHP.
          PHP support single-line as well as multi-line comments.
          To write a single-line comment either start the line with either two slashes (//)or a hash
Web Technology                                                                                     Page 10
         symbol (#).
 For example:
 <?php
 // This is a single line comment
 # This is also a single line comment echo "Hello,
      world!";
 ?>
 However to write multi-line comments, start the comment with a slash followed by an asterisk (/*)
   and end the comment with an asterisk followed by a slash (*/).
      Case Sensitivity in PHP
 Variable names in PHP are case-sensitive.
 As a result the variables $color, $Color and $COLOR are treated as three different variables.
 <?php
 // Assign value to variable
 $color = "blue";
 // Try to print variable value
 echo "The color of the sky is " . $color . "<br>"; echo "The
      color of the sky is " . $Color . "<br>"; echo "The color of
      the sky is " . $COLOR . "<br>";
 ?>
 7.3 PHP     variables
          All variables in PHP are denoted with a leading dollar sign ($).
Web Technology                                                                                   Page 11
        The value of a variable is the value of its most recent assignment.
        Variables are assigned with the = operator, with the variable on the left-handside and
           the expression to be evaluated on the right.
        Variables can, but do not need, to be declared before assignment.
        Variables in PHP do not have intrinsic types - a variable does not know in
           advance whether it will be used to store a number or a string of characters.
        Variables used before they are assigned have default values.
        PHP does a good job of automatically converting types from one to another
           when necessary.
        PHP variables are Perl-like.
    7.4. PHP Data types
      PHP has a total of eight data types which we use to construct our variables –
        Integers − are whole numbers, without a decimal point, like 4195.
        Doubles − are floating-point numbers, like 3.14159 or 49.1.
        Booleans − have only two possible values either true or false.
        NULL − is a special type that only has one value: NULL.
        Strings − are sequences of characters, like 'PHP supports string operations.'
        Arrays − are named and indexed collections of other values.
        Objects − are instances of programmer-defined classes, which can package up
           both other kinds of values and functions that are specific to the class.
        Resources − are special variables that hold references to resources external to
           PHP (such as database connections).
Web Technology                                                                                    Page 12
    Integers
            They are whole numbers, without a decimal point, like 4195.
            They are the simplest type.
            They correspond to simple whole numbers, both positive and negative. Integers can be
            assigned to variables, or they can be used in expressions, like
            $int_var = 12345;
            $another_int = -12345 + 12345;
            Integer can be in decimal (base 10), octal (base 8), and hexadecimal (base 16)format.
            Decimal format is the default, octal integers are specified with a leading 0,and
            hexadecimals have a leading 0x.
            Doubles
          They like 3.14159 or 49.1. By default, doubles print with the minimum number of
    decimal places needed.
     For example, the code
    <?php
    $many = 2.2888800;
    $many_2 = 2.2111200;
    $few = $many + $many_2;
    print("$many + $many_2 = $few <br>");
    ?>
Web Technology                                                                                      Page 13
           It produces the following browser output −
           2.28888 + 2.21112 = 4.5
    Boolean
           They have only two possible values either true or false.
           PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE,
           which can be used like so −
           if (TRUE)
           print("This will always print<br>");
           else
           print("This will never print<br>"
       NULL
           NULL is a special type that only has one value: NULL.
           To give a variable the NULL value, simply assign it like this −
           $my_var = NULL;
           The special constant NULL is capitalized by convention, but actually it is case
           insensitive; you could just as well have typed −
           $my_var = null;
           A variable that has been assigned NULL has the following properties
           − It evaluates to FALSE in a Boolean context.
           It returns FALSE when tested with IsSet() function.
Web Technology                                                                               Page 14
    Strings
    They are sequences of characters, like "PHP supports string operations". Following are valid
    examples of string
              $string_1 = "This is a string in double quotes";
              $string_2 = 'This is a somewhat longer, singly quoted string';
              $string_39 = "This string has thirty-nine characters";
              $string_0 = ""; // a string with zero characters.
              8.Displaying Type Information with var_dump():
              var_dump() provides information about all the types contained within the variable,
              as well as about the variable itself.
              $testing = 5;
              var_dump( $testing )
              Output
            int(5)
    7.5.   Displaying type information
              This function displays structured information about one or moreexpressions that
              includes its type and value.
            Arrays and objects are explored recursively with values indentedto show
              structure.
Web Technology                                                                                     Page 15
    Example #1 var_dump()
    <?php
    $a = array(1, 2, array("a", "b", "c"));
    var_dump($a);
    ?>
    The above example will output:
    array(3) {
        [0]=>
        int(1)
        [1]=>
        int(2)
        [2]=>
        array(3) {
            [0]=>
            string(1) "a"
            [1]=>
            string(1) "b"
            [2]=>
            string(1) "c"
        }
    }
    <?php
    $b = 3.1;
Web Technology                                Page 16
    $c = true;
    var_dump($b, $c);
    ?>
    The above example will output:
    float(3.1)
    bool(true)
    7.6.     Testing for a Specific Data Type
            PHP provides a special function corresponding to each data type.
            These functions accept a variable or value and return a Boolean
    Function
           is_array()
           Returns true if the argument is an array
           is_bool()
           Returns true if the argument is boolean
           is_double()
           Returns true if the argument is a double
           is_int()
           Returns true if the argument is an integer
           is_object()
Web Technology                                                                  Page 17
       Returns true if the argument is an object
       is_string()
       Returns true if the argument is a string
       is_null()
       Returns true if the argument is null
       is_resource()
       Returns true if the argument is a resource
    7.7. Operators
        PHP Operator is a symbol i.e used to perform operations on operands
        In simple words, operators are used to perform operations on variables orvalues.
    For example:
       $num=10+20;//+ is the operator and 10,20 are operands
       In the above example, + is the binary + operator, 10 and 20 are operands and
       $num is variable.
    PHP Operators can be categorized in following forms:
    a. Arithmetic Operators
    b. Assignment Operators
    c. Bitwise Operators
    d. Comparison Operators
    e. Incrementing/Decrementing Operators
    f. Logical Operators
    g. String Operators
Web Technology                                                                              Page 18
    h. Array Operators
    Arithmetic operators:
    The PHP arithmetic operators are used to perform common arithmetic operationssuch as
    addition, subtraction, etc. with numeric values.
    Operator         Name Example               Explanation
    +      Addition         $a + $b             Sum of operands
    -      Subtraction $a - $b                  Difference of operands
    *      Multiplication             $a * $b          Product of operands
    /       Division        $a / $b             Quotient of operands
    %       Modulus         $a % $b             Remainder of operands
    **     Exponentiation             $a ** $b         $a raised to the power $b
    Assignment Operators
    The assignment operators are used to assign value to different variables. The basicassignment
    operator is "=".
    Operator         Name Example               Explanation
    =       Assign          $a = $b             The value of right operand is assigned to the
    left operand.
    +=     Add then Assign            $a += $b         Addition same as $a = $a + $b
    -=     Subtract then Assign                 $a -= $b       Subtraction same as $a = $a - $b
    *=     Multiply then Assign                 $a *= $b       Multiplication same as $a = $a * $b
    /=     Divide then Assign
    (quotient)       $a /= $b         Find quotient same as $a = $a / $b
    %=     Divide then Assign
Web Technology                                                                                       Page 19
    (remainder) $a %= $b           Find remainder same as $a = $a % $b
    Bitwise operators
    The bitwise operators are used to perform bit-level operations on operands. These operators allow
    the evaluation and manipulation of specific bits within the integer.
    Operator       Name Example              Explanation
    &      And     $a & $b         Bits that are 1 in both $a and $b are set to 1, otherwise 0.
    |      Or (Inclusive or)       $a | $b          Bits that are 1 in either $a or $b are set to 1
    ^      Xor (Exclusive or) $a ^ $b               Bits that are 1 in either $a or $b are set to 0.
    ~      Not     ~$a     Bits that are 1 set to 0 and bits that are 0 are set to 1
    Comparison Operators
    Comparison operators allow comparing two values, such as number or string. Below the list
    of comparison operators are given:
    Operator       Name Example              Explanation
    ==     Equal $a == $b          Return TRUE if $a is equal to $b
    === Identical          $a === $b         Return TRUE if $a is equal to $b, and they are of
    same data type
    !==     Not identical          $a !== $b        Return TRUE if $a is not equal to $b, and
    they are not of same data type
    !=      Not equal      $a != $b          Return TRUE if $a is not equal to $b
    <>      Not equal      $a <> $b          Return TRUE if $a is not equal to $b
    <       Less than      $a < $b           Return TRUE if $a is less than $b
    >      Greater than $a > $b              Return TRUE if $a is greater than $b
    <=     Less than or equal to             $a <= $b       Return TRUE if $a is less than or equal
    $b
Web Technology                                                                                         Page 20
    >=     Greater than or equal to         $a >= $b          Return TRUE if $a is greater than orequal
    $b
    <=> Spaceship          $a <=>$b       Return -1 if $a is less than
    $b Return 0 if $a is equal $b Return 1 if
    $a is greater than $b
    Incrementing/Decrementing Operators
    The increment and decrement operators are used to increase and decrease the valueof a variable.
    Operator       Name Example             Explanation
    ++     Increment          ++$a Increment the value of $a by one, then return $a
    $a++ Return $a, then increment the value of $a by one
    --     decrement          --$a   Decrement the value of $a by one, then return $a
    $a--   Return $a, then decrement the value of $a by one
    Logical Operators
    The logical operators are used to perform bit-level operations on operands. These operators allow the
    evaluation and manipulation of specific bits within the integer.
    Operator       Name Example             Explanation
    and    And     $a and $b         Return TRUE if both $a and $b are
           trueOr Or                 $a or $b   Return TRUE if either $a or
           $b is true
    xor    Xor     $a xor $b         Return TRUE if either $ or $b is true but not both
    !      Not     ! $a       Return TRUE if $a is not true
    &&     And     $a && $b          Return TRUE if either $a and $b are true
    ||     Or      $a || $b          Return TRUE if either $a or $b is true
Web Technology                                                                                       Page 21
    String Operators
    The string operators are used to perform the operation on strings. There are twostring operators
    in PHP, which are given below:
    Operator         Name Example             Explanation
    .        Concatenation          $a . $b          Concatenate both $a and $b
    .=      Concatenation and Assignment $a .= $b                First concatenate $a and $b,
    then assign the concatenated string to $a, e.g. $a = $a . $b
    Array Operators
    The array operators are used in case of array. Basically, these operators are used to compare the
    values of arrays.
    Operator         Name Example             Explanation
    +        Union $a + $y          Union of $a and $b
    ==       Equality        $a == $b         Return TRUE if $a and $b have same key/value
    pair
    !=       Inequality      $a != $b         Return TRUE if $a is not equal to $b
    ===      Identity        $a === $b        Return TRUE if $a and $b have same key/value
    pair of same type in same order
    !==      Non-Identity           $a !== $b        Return TRUE if $a is not identical to $b
    <>       Inequality      $a <> $b         Return TRUE if $a is not equal to $b
    7.7 Variable manipulation
           Variable manipulation is a method of specifying or editing variables in a
             computer program.
           Variable manipulation can be used to create dynamic content in HTML and
Web Technology                                                                                   Page 22
           programming languages. However, the method can also be used by an attacker, to
           alter data that a browser sends to a Web server. A variable, in this context, is a
           string of text symbols and/or numerals that can change, depending on conditions or
           on information passed to the program.
        In mathematics, variable manipulation is the process of rearranging a multivariable
           equation to express a specific variable as a function of other variables.
        The variable thus singled-out is known as the dependent variable, while the
           other variables are called independent variables.
        In this context, a variable is a mathematical quantity whose numerical value can
           change, in contrast to constants that have fixed numerical values.
    7.8 .Dynamic variables
        PHP allows us to use dynamic variable.
        Dynamic variable is variable variables.
        We can name a variable with the value stored in another variable.
        That is, one variable contains the name of another variable.
       Example:
       $text = ‗Hello World‘;
       $dynamicText = ―text‖;
       echo ‗The dynamic ‗.$dynamicText. ‗ is ‗.${$dynamicText};
       Output : The dynamic text is Hello World
       We can implement it ${$dynamicText} or $$dynamicText .
Web Technology                                                                                  Page 23
           Here we can associate variable to another variable. We can use it as alternative of
           associative array and list. The time complexity also less then array and list also.
        7.8 String in PHP
            A string is a collection of characters. String is one of the data type
               ssupported by PHP.
            The string variables can contain alphanumeric characters. Strings are created when;
            You declare variable and assign string characters to it
            You can directly use them with echo statement.
            String are language construct, it helps capture words.
 Learning how strings work in PHP and how to manipulate them will make you a very effective
   and productive developer.
   Web Technology                                                                                  Page 24
                                                 Unit - II
    .Control structures
            Control structure allows you to control the flow of code execution in your application.
            Generally, a program is executed sequentially, line by line, and a control structure
            allows you to alter that flow, usually depending on certain conditions.
    PHP supports a number of different control structures:
            if
            else
            elseif
            switch
            while
            do-while
            for
            foreach
      The If statement
     The if construct allows you to execute a piece of code if the expression provided along with it
    evaluates to true.
    Example:
    <?php
    $age = 50;
    if ($age > 30)
Web Technology                                                                                     Page 25
    {
        echo "Your age is greater than 30!";
    ?>
     Using the Else clause with if Statement
     if the expression evaluates to true. On the other hand, if the expression evaluates to false, it
    won't do anything.
     More often than not, you also want to execute a different code snippet if the expression
    evaluates to false.
    Syntax
    if (expression)
        // code is executed if the expression evaluates to TRUE
    else
        // code is executed if the expression evaluates to FALSE
    Example
Web Technology                                                                                          Page 26
    <?php
    $age = 50;
    if ($age < 30)
        echo "Your age is less than 30!";
    else
        echo "Your age is greater than or equal to 30!";
    ?>
     Multiple if, nested If Statement
     We can consider the elseif statement as an extension to the if-else construct.
     If you've got more than two choices to choose from, you can use the elseif statement.
    Syntax
    if (expression1)
        // code is executed if the expression1 evaluates to TRUE
    elseif (expression2)
Web Technology                                                                               Page 27
    {
        // code is executed if the expression2 evaluates to TRUE
    elseif (expression3)
        // code is executed if the expression3 evaluates to TRUE
    else
     // code is executed if the expression1, expression2 and expression3 evaluates toFALSE, a default
    choice
    Example
    <?php
    $age = 50;
    if ($age < 30)
        echo "Your age is less than 30!";
    elseif ($age > 30 && $age < 40)
        echo "Your age is between 30 and 40!";
Web Technology                                                                                 Page 28
    }
    elseif ($age > 40 && $age < 50)
        echo "Your age is between 40 and 50!";
    else
        echo "Your age is greater than 50!";
    ?>
Switch Statement
           The switch statement is somewhat similar to the elseif statement which we've just
              discussed in the previous section.
           The only difference is the expression which is being checked.
           In the case of the elseif statement, you have a set of different conditions, and
              an appropriate action will be executed based on a condition.
           On the other hand, if you want to compare a variable with different values, you can use
              the switch statement.
    <?php
    $favourite_site = 'Code';
    switch ($favourite_site) {case
    'Business':
Web Technology                                                                                  Page 29
         echo "My favourite site is business.tutsplus.com!";break;
        case 'Code':
         echo "My favourite site is code.tutsplus.com!";break;
        case 'Web Design':
         echo "My favourite site is webdesign.tutsplus.com!";break;
        case 'Music':
         echo "My favourite site is music.tutsplus.com!";break;
        case 'Photography':
         echo "My favourite site is photography.tutsplus.com!";break;
        default:
         echo "I like everything at tutsplus.com!";
    ?>
    ? Operators
     This is called the Ternary Operator, and it's common to several languages,including PHP,
    Javascript, Python, Ruby...
    $x = $condition ? $trueVal : $falseVal;
    // same as:
    Example:
    if ($condition) {
         $x = $trueVal;
    } else {
Web Technology                                                                                  Page 30
         $x = $falseVal;
    } errorLoops
                      A Loop is an Iterative Control Structure that involves executing the same
    number of code a number of times until a certain condition is met.
    12.1 While loop
          They are used to execute a block of code a repeatedly until the set condition gets satisfied
          When to use while loops
          While loops are used to execute a block of code until a certain condition
             becomes true.
          You can use a while loop to read records returned from a database query.
          Types of while loops
          Do… while - executes the block of code at least once before evaluating the condition
          While… - checks the condition first. If it evaluates to true, the block of code is
             executed as long as the condition is true. If it evaluates to false, the execution of the
             while loop is terminated.
    While loop
    It has the following syntax
         <?php
    while (condition){
    block of code to be executed;
    }
    ?>
Web Technology                                                                                     Page 31
        HERE,
    ―while(…){…}‖ is the while loop block code
    ―condition‖ is the condition to be evaluated by the while loop
    ―block of code…‖ is the code to be executed if the condition gets satisfied How it works
    The flow chart shown below illustrates how the while… loop works PHP
    Loop and Control Structures
    Practical example
    The code below uses the while… loop to print numbers 1 to 5.
    <?php
    $i = 0;
    while ($i < 5){
    echo $i + 1 . "<br>";
    $i++;
    }
    ?>
    Output:
    1
    2
    3
    4
    5
    12.2 Do While
    The difference between While… loop and Do… while loop is do… while is executed at-least
    once before the condition is evaluated.
    Let‘s now look at the basic syntax of a do… while loop
Web Technology                                                                                 Page 32
    <?php
    do{
    block of code to be executed
    }
    ?>
        while(condition);HERE,
    ―do{…} while(…)‖ is the do… while loop block code ―condition‖ is the
    condition to be evaluated by the while loop
    ―block of code…‖ is the code that is executed at least once by the do… while loop How it works
    The flow chart shown below illustrates how the while… loop works PHP
    Loop and Control Structures
    We are now going to modify the while… loop example and implement it using the do… while
    loop and set the counter initial value to 9.
    The code below implements the above modified example
    <?php
    $i = 9;
    do{
          echo "$i is"." <br>";
    }
    while($i < 9);
    ?>
    The above code outputs:9
    12.3 For Loop
                      The above code outputs ―21 is greater than 7‖ For loops For... loops execute the
    block of code a specified number of times. There are basically two types of for loops;
    for
    for… each.
    Let‘s now look at them separately. For loop It has the following basic syntax
    <?php
Web Technology                                                                                    Page 33
    for (initialize; condition; increment){
    //code to be executed
    }
    ?>
        HERE,
    ―for…{…}‖ is the loop block
    ―initialize‖ usually an integer; it is used to set the counter‘s initial value. ―condition‖ the
    condition that is evaluated for each php execution. If it evaluates to true then execution of the
    for... loop is terminated. If it evaluates to false, the execution of the for... loop continues.
    “increment” is used to increment the initial value of counter integer.
    String function
                          They are sequences of characters, like "PHP supports string
    operations". Following are valid examples of string
Web Technology                                                                                         Page 34
    $string_1 = "This is a string in double quotes";
    $string_2 = 'This is a somewhat longer, singly quoted string';
    $string_39 = "This string has thirty-nine characters";
    $string_0 = ""; // a string with zero characters.
Web Technology                                                       Page 35
Web Technology   Page 36