-----
String concatenation
This period is the string concatenation operator, which adds
strings (pieces of text) together. You will often use it when sending output to
the browser with echo. This way, you can avoid writing multiple echo commands.
You can also place simple variables inside a double-quoted
string to be echoed. Consider this example:
$tireqty = htmlspecialchars($tireqty);
echo "$tireqty tires<br />";
This process, replacing a variable with its contents within
a string, is known as interpolation.
Note that interpolation is a feature of double-quoted
strings only. You cannot place variable
names inside a single-quoted string in this way. Running the
following line of code
echo '$tireqty tires<br />';
simply sends $tireqty tires<br /> to the browser.
Within double quotation marks, the variable name is replaced with its value. Within
single quotation marks, the variable name or any other text is sent unaltered.
Variables ans
Literals (p23)
Heredoc:
echo <<<theEnd
line 1
line 2
line 3
theEnd
It is error if write as following:
echo <<<theEnd
line 1
line 2
line 3
theEnd
[Warning
It is very important to note that the line with the closing
identifier must contain no other characters, except a semicolon (;). That means
especially that the identifier may not be indented, and there may not be any
spaces or tabs before or after the semicolon. It's also important to realize
that the first character before the closing identifier must be a newline as
defined by the local operating system.]
1.1.4 Understanding identifiers (p23)
Identifiers are the names of variables. The names of
functions and classes are also identifiers.
You need to be aware of the simple rules defining valid
identifiers:
– Identifiers can be of any length and can consist of
letters, numbers, and underscores
– Identifiers cannot begin with a digit
– In PHP, identifiers are case sensitive. $tireqty is not
the same as $TireQty. Trying to use them interchangeably is a common
programming error. Function names are an exception to this rule: Their names
can be used in any case.
– A variable can have the same name as a function. This
usage is confusing, however, and should be avoided. Also, you cannot create a
function with the same name as another function.
One of the features of PHP is that it does not require you
to declare variables before using them. A variable is created when you first
assign a value to it. For example:
$totalqty = 0;
$totalamount = 0.00;
1.1.5 Examining variable types (p24)
PHP supports the following basic data types:
– Integer: Used for whole numbers
– Float (also called double): Used for real numbers
– String: Used for strings of characters
– Boolean: Used for true or false values
– Array: Used to store multiple data items (see Chapter 3,
“Using Arrays”)
– Object: Used for storing instances of classes (see Chapter
6)
Three special types are also available: NULL, resource, and
callable
Variables that have not been given a value, have been unset,
or have been given the specific value NULL are of type NULL.
Type Strength (p25)
PHP is called a weakly typed or dynamically typed language.
In most programming languages, variables can hold only one type of data, and that
type must be declared before the variable can be used, as in C. In PHP, the
type of a variable is determined by the value assigned to it.
Type Casting (p25)
(Same change type)
Variable Variables
(p25)
(Same pointer)
PHP provides one other type of variable: the variable
variable. Variable variables enable you to change the name of a variable
dynamically. As you can see, PHP allows a lot of freedom in this area. All
languages enable you to change the value of a variable, but not many allow you
to change the variable’s type, and even fewer allow you to change the
variable’s name.
A variable variable works by using the value of one variable
as the name of another.
For example, you could set
$varname = 'tireqty';
You can then use $$varname in place of $tireqty. For
example, you can set the value of
$tireqty as follows:
$$varname = 5;
This is equivalent to
$tireqty = 5;
Instead of having to list and use each form variable
separately, you can use a loop and variable variable to process them all
automatically. You can find an example illustrating this in the section on for
loops later in this chapter.
1.1.6 Declaring and using constants (p26)
Example:
define('TIREPRICE', 100);
echo TIREPRICE;
Note: It does not have a dollar sign in front of constant.
One other difference between variables and constants is that
constants can store only boolean, integer, float, or string data.
1.1.7 Understanding variable scope (p27)
The term scope refers to the places within a script where a
particular variable is visible. The six basic scope rules in PHP are as
follows:
– Built-in superglobal variables are visible everywhere
within a script.
– Constants, once declared, are always visible globally;
that is, they can be used inside
and outside functions.
– Global variables declared in a script are visible
throughout that script, but not inside
functions.
– Variables inside functions that are declared as global
refer to the global variables of the
same name.
– Variables created inside functions and declared as static
are invisible from outside the
function but keep their value between one execution of the
function and the next.
(We explain this idea fully in Chapter 5.)
– Variables created inside functions are local to the
function and cease to exist when the
function terminates.
The arrays $_GET and $_POST and some other special variables
have their own scope rules. They are known as superglobals and can be seen
everywhere, both inside and outside functions.
The complete list of superglobals is as follows:
– $GLOBALS—An array of all global variables (Like the global
keyword, this allows you to
access global variables inside a function—for example, as
$GLOBALS['myvariable'].)
– $_SERVER—An array of server environment variables
– $_GET—An array of variables passed to the script via the
GET method
– $_POST—An array of variables passed to the script via the
POST method
– $_COOKIE—An array of cookie variables
– $_FILES—An array of variables related to file uploads
– $_ENV—An array of environment variables
– $_REQUEST—An array of all user input including the
contents of input including $_GET, $_POST, and $_COOKIE (but not including
$_FILES)
– $_SESSION—An array of session variables
1.1.8 Using operators (p28)
– Arithmetic operators (p28)
– String operator (p29)
– Assignment operators (p29)
– Combined assignment operators (p30)
– Pre- and Post-increment and decrement (p30)
– Reference operator (p31)
unset(variable) means destroy variable.
– Comparison operator (p31)
==: equal, example 0 == '0' => true
===: identical, example 0 === '0' => false
– Logical operators (p33)
– Bitwise operators (p33)
– Other operators (p33)
Comma operator (,): separates function arguments and other
list of items. New and ->, used to instantiate a class and access class
members, respectively.
The ternary operator: condition
? value if true : value if false
– The error suppression operator (p34)
– The execution operator (p34)
– Array operators (p35)
– The type operator (p35)
Lab 3. Working out the form totals.
Now that you know how to use PHP’s operators, you are ready
to work out the totals and tax
on Bob’s order form. To do this, add the following code to
the bottom of your PHP script:
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo "<p>Items ordered:
".$totalqty."<br />";
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalamount = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $sparkqty * SPARKPRICE;
echo "Subtotal:
$".number_format($totalamount,2)."<br />";
$taxrate = 0.10; // local sales tax is 10%
$totalamount = $totalamount * (1 + $taxrate);
echo "Total including tax:
$".number_format($totalamount,2)."</p>";
If you refresh the page in your browser window, you should
see output similar following:
-----------
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 (x3) - PHP: crash course 3
Cập nhật [17/4/2019]
-----------
Xem thêm: Học làm web (x3) - PHP: crash course 3