PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO). Sergey D Skudaev. Читать онлайн. Newlib. NEWLIB.NET

Автор: Sergey D Skudaev
Издательство: Издательские решения
Серия:
Жанр произведения: Компьютеры: прочее
Год издания: 0
isbn: 9785449090157
Скачать книгу
($temperature_C) {

      Case: 0

      $message=“Water freezing point.”;

      Break;

      Case: 100

      $message=“Water boiling point!”:

      Break;

      Default:

      $message=“Neither freezing nor boiling point.”;

      }

      If we pass in our switch statement variable with the value of 100 we will get Output: “Water boiling point!”

      If we pass 0 we will get Output: =“Water freezing point.”

      Otherwise, we will get Output: “Neither freezing nor boiling point.”

      List

      List is a language construction. It is used to assign a list of variables in one operation.

      In the line below, I assign array $names from the array example above to a list of variables.

      list ($name1, $name2, $name3, $name4, $name5, $name6) = $names;

      print ($name5); will print John because it is the 5th name in the array.

      Multidimensional Arrays

      An example of a two-dimensional array is plane seats. A plane has rows identified by numbers and a few seats in a row identified by letters A, B, C, D, E, and F.

      Let us imagine that we have 7 rows with 6 seats in each of the rows. Define an array of seats in a row as $s. Then in two for loops we can create an array of all seats.

      $s=array (“A”, “B”, “C”, “D”, “E”, “F”);

      for ($i=0; $i <7; $i++) {

      $row = $i+1;

      for ($j=0; $j <6; $j++) {

      $seats [$i] [$j] =$row.$s [$j];

      print ($seats [$i] [$j]);

      if (($j % 5 ==0) && ($j> 0))

      print (”<br>”);

      }

      }

      In an inner J loop, we assign a seat value to the two-dimensional array.

      $row = $i +1 because $i starts with 0 and the row starts with 1.

      Then we concatenate a row number with a seat letter in $s array.

      We insert a line break <br> after every 6 seats.

      For that we check if modulo of 5 equals 0. This occurs when $j=5. Since $j starts with 0 we have 6 seats in a row before we insert a break.

      Additionally, we check if $j is greater than 0 because 0 modulo 5 gives 0 and creates an extra break we do not want to have.

      Output:

      1A1B1C1D1E1F

      2A2B2C2D2E2F

      3A3B3C3D3E3F

      4A4B4C4D4E4F

      5A5B5C5D5E5F

      6A6B6C6D6E6F

      7A7B7C7D7E7F

      $_GET and $_POST methods

      Here you will learn how to use the request variables $_GET and $_POST. These variables provide you with different ways to transfer data from one web page to another. Let’s look at the two methods, starting with GET. First, let’s create a simple HTML form.

      $_GET []

      <html>

      <head>

      <title> Form Methods

      </title>

      </head>

      <body>

      <form method=“get” action="actionpage.php”>

      <p> <input type=“text” name=“greeting” size=“15”> </p>

      <p> <input type=“text” name=“name” size=“15”> </p>

      <p> <input type=“submit” name=“submit” value=“Salutation”> </p>

      </form>

      </body>

      </html>

      Save it as a form_methods.php file in the Apache htdocs/post folder created by you.

      And now, we’ll create a actionpage.php file for the Output: data transferred from the form.

      <?

      echo $_GET [‘greeting’];

      echo $_GET [‘name’];

      echo”! ”;

      ?>

      Save this file in the same directory as the form_methods.php file. This form looks like the following:

      Figure 1. A simple HTML form

      Let us enter a greeting and a name and click the Salutation button.

      You can see that the data sent from a form with the GET method is displayed in the browser’s address bar:

      http://localhost/post/formoutputpage.php? <=>

      greeting=Hello&name=Emily&submit=Salutation

      The Output web page displays Hello Emily!

      $_POST []

      Now let’s use the POST method instead of the GET method. Edit form_method.php form.

      <html>

      <head>

      <title> Form Methods </title>

      </head>

      <body>

      <form method=“post” action="formoutputpage.php”>

      <p> <input type=“text” name=“greeting” size=“15”> </p>

      <p> <input type=“text” name=“name” size=“15”> </p>

      <p> <input type=“submit” name=“submit” value=“Salutation”> </p>

      </form>

      </body>

      </html>

      Edit formoutputpage.php file as follow:

      <?

      echo $_POST [‘greeting’];

      echo “”. $_POST [‘name’];

      echo”! ”;

      ?>

      The browser address bar displays formoutputpage.php, but no data transferred using the POST method is visible, so the web page output remains the same:

      Hello Emily!

      You don’t have to create a second page to read data submitted with form because it is possible to submit the form to the same page. To do this use super global $_SERVER [“PHP_SELF”].

      <?php

      $self=$_SERVER [“PHP_SELF”];

      $greeting=“”;

      $name=“”;

      If