1 / 19

Chapter 4 – The Building Blocks

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)

rio
Download Presentation

Chapter 4 – The Building Blocks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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.

  4. 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.

  5. 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

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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

  11. Concatenation and Variables • Use the dot (.) to concatenate variables and strings together. • PHP converts numeric values to strings.

  12. 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.

  13. 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.

  14. 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.

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

  16. 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.

  17. 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.

  18. 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

  19. 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.

More Related