chapter 4 – the building blocks data types literals variables constants

19
Chapter 4 – The Building Blocks Data Types Literals Variables Constants

Upload: mervin-day

Post on 26-Dec-2015

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

Chapter 4 – The Building Blocks

Data TypesLiterals

VariablesConstants

Page 2: 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.

Page 3: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 4: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 5: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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

Page 6: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 7: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 8: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 9: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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 TagsUsed to embed PHP within HTML portion of the

file. Not recommended to use.

Page 10: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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

Page 11: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

Concatenation and Variables

• Use the dot (.) to concatenate variables and strings together.

• PHP converts numeric values to strings.

Page 12: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 13: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 14: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 15: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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()

Page 16: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 17: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.

Page 18: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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/Predefined-Variables/8.php

Page 19: Chapter 4 – The Building Blocks Data Types Literals Variables Constants

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.