Variables and Constants

advertisement
Variables and Constants
• PHP stands for: ”PHP: Hypertext Preprocessor”, and it is
a server-side programming language.
• Special markup that indicates you are about to start
using PHP looks like this:
<?php
…
• At the end of your PHP, use the closing markup:
?>
Variables and Constants
tMyn
1
• In the classroom PCs the configuration has been
implemented so that each PC does have a Web
server software (Apache HTTP Server) of it’s own.
• All the PHP files must be saved to the directory
C:\Program Files\Apache
Group\Apache2\htdocs\YourPersonalFoulderName
• This means that http://localhost will display the home
page of the local web site.
• Let’s test the first PHP page. Make sure the web
server is running (it should always be running by
default)
Variables and Constants
tMyn
2
Variables and Constants
tMyn
3
Variables and Constants
tMyn
4
• From the previous example we saw that you can
intersperse your PHP code with HTML. So the HTML will
be displayed by your browser, and the PHP will be run
on your server – and if that PHP generates some HTML,
that HTML will be displayed in your browser as well.
• You can insert PHP anywhere in your HTML page and
the PHP engine on the web server will run it, as long as
it is contained in the <?php … ?> markup. When that
PHP is run, any HTML it generates will be inserted into
the page at the location of that PHP.
Variables and Constants
tMyn
5
• We used echo to display text using double quotes. Even
if we could call echo as being a function, technically
speaking echo is built-in PHP language construction.
• PHP statement ends with a semicolon.
• Never forget that when you work with PHP online, you
are interacting with the user through a browser. That
means that the text you send back to the browser will be
interpreted as HTML, not just simple text.
• That also gives you the chance to make use of HTML to
format your text:
Variables and Constants
tMyn
6
Variables and Constants
tMyn
7
Variables and Constants
tMyn
8
• You can also separate the items you want to print with
commas:
Variables and Constants
tMyn
9
Variables and Constants
tMyn
10
Variables and Constants
tMyn
11
• You can also assemble text strings together into one
string using a dot. In that case, PHP takes your
expression and assembles it together (concatenation)
into one single string:
Variables and Constants
tMyn
12
Variables and Constants
tMyn
13
Variables and Constants
tMyn
14
• There are three types of comments in PHP.
• The first type of comment lets you create multiline
comments, which start with /* and ends with */.
• Single-line comment starts with // or #:
Variables and Constants
tMyn
15
Variables and Constants
tMyn
16
Variables and Constants
tMyn
17
• Variables in PHP are represented by a dollar sign
followed by the name of the variable. The variable name
is case-sensitive.
• Variable names follow the same rules as other labels in
PHP. A valid variable name starts with a letter or
underscore, followed by any number of letters, numbers,
or underscores. As a regular expression, it would be
expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*‘.
• By default, variables are always assigned by value. That
is to say, when you assign an expression to a variable,
the entire value of the original expression is copied into
the destination variable. This means, for instance, that
after assigning one variable's value to another, changing
one of those variables will have no effect on the other.
Variables and Constants
tMyn
18
• PHP also offers another way to assign values to
variables: assign by reference. This means that the new
variable simply references (in other words, "becomes an
alias for" or "points to") the original variable. Changes to
the new variable affect the original, and vice versa.
• To assign by reference, simply prepend an ampersand
(&) to the beginning of the variable which is being
assigned (the source variable).
Variables and Constants
tMyn
19
Variables and Constants
tMyn
20
Variables and Constants
tMyn
21
• It is not necessary to initialize variables in PHP however
it is a very good practice. Uninitialized variables have a
default value of their type depending on the context in
which they are used - booleans default to FALSE,
integers and floats default to zero, strings (e.g. used in
echo()) are set as an empty string and arrays become to
an empty array.
Variables and Constants
tMyn
22
• PHP provides a large number of predefined variables to
all scripts. The variables represent everything from
external variables to built-in environment variables, last
error messages to last retrieved headers.
• Some examples:
• $_GET
An associative array of variables passed to the current
script via the URL parameters.
• $_POST
An associative array of variables passed to the current
script via the HTTP POST method.
• $_SESSION
An associative array containing session variables
available to the current script.
Variables and Constants
tMyn
23
• PHP does not require that you create different types of
variables for different types of data:
Variables and Constants
tMyn
24
Variables and Constants
tMyn
25
Variables and Constants
tMyn
26
• There is a shortcut way of displaying the values of the
variables in the previous example, called string
interpolation.
• When you use interpolation, you only have to place the
variable whose value you want to insert inside a doublequoted text string (not single-quoted!!):
Variables and Constants
tMyn
27
Variables and Constants
tMyn
28
Variables and Constants
tMyn
29
• Sometimes, you don’t want a data item to be a variable.
For example, the value of pi shouldn’t change.
• The thing to do here is to create a constant, whose value
can’t be modified.
• You create a constant in PHP with the define function:
Variables and Constants
tMyn
30
Variables and Constants
tMyn
31
Variables and Constants
tMyn
32
The define() function defines a constant.
Constants are much like variables, except for the following differences:
A constant's value cannot be changed after it is set
Constant names do not need a leading dollar sign ($)
Constants can be accessed regardless of scope
Constant values can only be strings and numbers
Syntax
define(name,value,case_insensitive)
Parameter
Description
name
Required. Specifies the name of the constant
value
Required. Specifies the value of the constant
case_insensitive
Optional. Specifies whether the
constant name should be case-insensitive. If set to TRUE,
the constant will be case-insensitive. Default is FALSE
(case-sensitive)
Variables and Constants
tMyn
33
• PHP does you a big favor (?) by letting you store data
without having to specify the data’s type.
• Sometimes, however, you have to know about the
internal data types that PHP uses, such as:
–
–
–
–
–
boolean: Holds true/false values
integer: Holds numbers like -1, 0, 5 and so on
float: Holds floating-point numbers like 3.141592, 2.789321
string: Holds text like “PHP should be fun!”
array: Holds arrays of data items
Variables and Constants
tMyn
34
• Let’s do some tests. In the beginning there is a variable
which is set to “0”. So even if it seems to be an integer
number zero, it is actually a string “0”. What happens, if
we try to add integer value 1 to that variable’s value?:
Variables and Constants
tMyn
35
Variables and Constants
tMyn
36
Variables and Constants
tMyn
37
• In this case, PHP does the best it can: adding the integer
value 1 to the string “0” leaves $stringVariable
holding the integer 1.
• What if you add a floating point value to the string “0”?:
Variables and Constants
tMyn
38
Variables and Constants
tMyn
39
Variables and Constants
tMyn
40
• Let’s still try to confuse PHP:
Variables and Constants
tMyn
41
Variables and Constants
tMyn
42
Variables and Constants
tMyn
43
Download