Slide 1

advertisement
Chapter 4 – The Building Blocks
Data Types
Literals
Variables
Constants
Data Types
• Specify what kind of data can be stored and
manipulated within a program.
– Core/Scalar (integer, float, string, boolean)
– Special/Composite (arrays, objects, null,
resources)
• Data are commonly stored in variables.
Numeric Literals
• PHP supports integers (do not contain decimal
point) and floating-point numbers (must
contain decimal point or exponent specifier).
String Literals
• Row of characters enclosed in single or double
quotes.
• An empty set of quotes is called the null string.
Here-docs
• Creates a block of text that simplifies writing
strings containing lots of single quotes, double
quotes, and variables.
• A user-defined delimiter starts and terminates
the here-doc.
• The delimiter is preceded by three “<<<“ and
must start with a letter or underscore.
Escape Sequences
•
•
•
•
•
•
•
•
•
•
Consists of a backslash followed by a single character.
\’
Single quotation
\”
Double quotation
\t
Tab
\n
New Line
\r
Return/Line Feed
\$
Literal dollar sign
\\
Backslash
\70
Octal value
\x05
Hexadecimal character
Boolean Literals
• Logical values that have only one of two
possible values, TRUE or FALSE (0 or 1; ON or
OFF; Yes or No), both case insensitive.
• Used to test whether a condition is true or
false.
Special Data Types
• Null – Represents “no value”. An uninitialized
variable contains the value NULL.
• Resource – Holds a reference to an external
resource such as a database object or file
handler. (Will be discussed in Chapters 11 & 15).
gettype() function -> Returns a string to
identify the data type of its argument.
PHP Variables
• Start with a dollar sign ($), followed by a letter
and any number of alphanumeric characters,
including the underscore.
$first_name = “Roberto”;
$last_name = “Mattos”;
• Values assigned to variables can change
throughout the run of a program.
• Initialization when declaring is not mandatory.
Displaying variables
• Print and Echo contructs (they are not
functions)
• If a variable is enclosed in double quotes, it
will be evaluated. In single quotes, it will not.
Shortcut Tags
Used to embed PHP within HTML portion of the
file. Not recommended to use.
Variables and mixed Data Types
• PHP is loosely typed. You are not required to
specify the type of data you are going to store
in the variable when you declare it.
• At runtime the PHP interpreter will convert
the data to the correct type
Concatenation and Variables
• Use the dot (.) to concatenate variables and
strings together.
• PHP converts numeric values to strings.
References
• Assigning a value to a variable by reference ->
one variable is an alias or pointer to another
variable; they point to the same data.
• Changing one variable automatically changes
the other.
• To assign by reference, prepend an ampersand
(&) to the beginning of the old variable that
will be assigned to the new variable.
Dynamic Variable
• It is a variable whose name is stored in
another variable.
• Curly Braces ensure that PHP parser will
evaluate the dollar signs properly.
Scope of Variables
• Local Variable -> exists only within a function.
They are not available to the rest of the script.
• Global Variable -> accessible everywhere within
a script other than from within functions.
• Superglobal Variable -> Special variables to help
you manage forms, cookies, sessions and files
and to get info about your environment and
server.
Managing variables
• PHP provides a number of functions to help you
manage variables.
– isset() – returns true if variable has been set.
– empty() – returns true if variable does not exist or has
been assigned and empty string, 0 (zero as number), “0”
(zero as string), NULL or no value at all.
– is_bool()
– is_double()
– is_ object()
– is_resource()
– is_string()
– unset()
Form Variables
• On php.ini file there is a directive called
register_globals and it is set to OFF. Keep it that way
for security reasons.
• You should add the following line of code to your php
program, if you are dealing with forms:
extract($_REQUEST);
• $_REQUEST is a superglobal array containing all the
information submitted to the server from the HTML
form.
Form Variables (cont.)
• For each HTML form parameter, PHP creates a
global variable by the same name and makes
it available to your script.
Example of HTML input type:
<input type=“text” name=“your_name”>
PHP creates a variable calles $your_name.
Predefined Variables
• PHP provides a number of predefined variables for
describing the environment, server, browser, version,
configuration file etc.
• Some are not fully documented as they depend on
which server is running…
• phpinfo(INFO_VARIABLES); /* retrieve built in
variables that have been set */
• See full list of predefined variables at
http://www.phpfreaks.com/PHP_Reference/Predefin
ed-Variables/8.php
Constants
• PHP provides its own predefined constants, but lets
you create your own.
• A constant is a value that, once set, cannot be
changed or unset during the execution of the script.
• They have global scope.
• It facilitates program maintenance.
• By convention, constants are capitalized.
• define() function creates a named constant.
• defined() function returns TRUE is constant has been
set.
• constant() function returns the value of that constant.
Download