Học làm web (x3) - PHP: crash course 3

Bài trước: Học làm web (x2) - PHP: crash course 2
-----

Understanding precedence and associativity (p38)

Using variable handling functions (p39)


– Testing and setting variable type (p39)

string gettype(mixed var);
bool settype(mixed var, string type);
mixed is pseudo-type, means many types are accepted.

Example,

$a = 56;
echo gettype($a).'<br />';
settype($a, 'float');
echo gettype($a).'<br />';

PHP also provides some specific type-testing functions. Each takes a variable as an argument and returns either true or false. The functions are

– is_array()—Checks whether the variable is an array

– is_double(), is_float(), is_real() (All the same function)—Checks whether the variable is a float

– is_long(), is_int(), is_integer() (All the same function)—Checks whether the variable is an integer

– is_string()—Checks whether the variable is a string

– is_bool()—Checks whether the variable is a boolean

– is_object()—Checks whether the variable is an object

– is_resource()—Checks whether the variable is a resource

– is_null()—Checks whether the variable is null

– is_scalar()—Checks whether the variable is a scalar—that is, an integer, boolean,
string, or float

– is_numeric()—Checks whether the variable is any kind of number or a numeric

Testing variable status (p40)

bool isset(mixed var[, mixed var[,…]])

This function takes a variable name as an argument and returns true if it exists and false otherwise. You can also pass in a comma-separated list of variables, and isset() will return true if all the variables are set.

You can wipe a variable out of existence by using its companion function, unset(), which has the following prototype:

void unset(mixed var [, mixed var [,…]])

The empty() function checks to see whether a variable exists and has a nonempty, nonzero value; it returns true or false accordingly. It has the following prototype:

bool empty(mixed var);

These functions are handy when you need to make sure that the user filled out the appropriate fields in the form.

Reinterpreting variables (p41)

Converting type of variable:

int intval(mixed var[, int base=10])
float floatval(mixed var)
string strval(mixed var)

Making decisions with conditionals (p41)


if statements (p41)

if ($totalqty == 0)
echo 'You did not order anything on the previous page!<br />';

Code blocks

if ($totalqty == 0) {
echo '<p style="color:red">';
echo 'You did not order anything on the previous page!';
echo '</p>';
}

else statements (p42)

if ($totalqty == 0) {
echo "You did not order anything on the previous page!<br />";
} else {
echo htmlspecialchars($tireqty).' tires<br />';
echo htmlspecialchars($oilqty).' bottles of oil<br />';
echo htmlspecialchars($sparkqty).' spark plugs<br />';
}

More…

if ($totalqty == 0) {
echo "You did not order anything on the previous page!<br />";
} else {
if ($tireqty > 0)
echo htmlspecialchars($tireqty).' tires<br />';
if ($oilqty > 0)
echo htmlspecialchars($oilqty).' bottles of oil<br />';
if ($sparkqty > 0)
echo htmlspecialchars($sparkqty).' spark plugs<br />';
}

elseif statements (p43)

if ($tireqty < 10) {
$discount = 0;
} elseif (($tireqty >= 10) && ($tireqty <= 49)) {
$discount = 5;
} elseif (($tireqty >= 50) && ($tireqty <= 99)) {
$discount = 10;
} elseif ($tireqty >= 100) {
$discount = 15;
}

Note that you are free to type elseif or else if—versions with or without a space are both correct.

Lab 4. Make a simple calculator.

The result screenshot:



Requirements:

– Using HTML, CSS to design the web front-end

– Using if, else, or elseif to calculate (PHP)


– Put the lab on the your free hosting, test to make sure it run well


– Push the lab up the your remote repository (Github, or Gitlab)

switch statement (p44)

The switch statement works in a similar way to the if statement, but it allows the condition to take more than two values. In an if statement, the condition can be either true or false. In a switch statement, the condition can take any number of different values, as long as it evaluates to a simple type (integer, string, or float). You need to provide a case statement to handle each value you want to react to and, optionally, a default case to handle any that you do not provide a specific case statement for.

Lab 5. Apply switch statement.

Bob wants to know what forms of advertising are working for him, so you can add a question to the order form. Insert this HTML into the order form, and the form will to look like following:

<tr>
            <td>How did you find Bob's?</td>
            <td><select name="find">
                    <option value = "a">I'm a regular customer</option>
                    <option value = "b">TV advertising</option>
                    <option value = "c">Phone directory</option>
                    <option value = "d">Word of mouth</option>
                </select>
            </td>
        </tr>



At the server side (back-end) using switch statement to handle for $find variable.

switch($find) {
        case "a" :
          echo "<p>Regular customer.</p>";
        break;
        case "b" :
          echo "<p>Customer referred by TV advert.</p>";
        break;
        case "c" :
          echo "<p>Customer referred by phone directory.</p>";
        break;
        case "d" :
          echo "<p>Customer referred by word of mouth.</p>";
        break;
        default :
          echo "<p>We do not know how this customer found us.</p>";
        break;

      }
-----------
Cập nhật [19/4/2019]
-----------
Xem thêm: Tổng hợp các bài viết về  Học làm web
Xem thêm: Học làm web (x4) - PHP: crash course 4