Download presentation
Presentation is loading. Please wait.
Published byEdgar Hall Modified over 9 years ago
1
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1 Wiley and the book authors, 2002 PHP Bible Chapter 5: Syntax, Variables, and Printing
2
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition2 Wiley and the book authors, 2002 Summary Understanding the basic rules of PHP code Storing information in variables Printing output that will display in a Web page
3
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition3 Wiley and the book authors, 2002 PHP is Forgiving PHP tries to be as forgiving as possible Pickiness in a programming language can be a good thing because it helps make sure that the code you’re writing is really what you mean PHP’s design philosophy is at the other end of the spectrum in that it emphasizes convenience for the programmer over correctness There are a minimum of syntax rules that code must follow
4
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition4 Wiley and the book authors, 2002 PHP’s Syntax PHP is whitespace (spaces, tabs, and carriage returns) insensitive Not ignored, but multiple whitespace characters in a row are treated as a single whitespace character PHP is sometimes case sensitive All variables are case sensitive Function names and basic language constructs (e.g. if, then, else, etc.) are not case sensitive Statements are expressions terminated by semicolons An expression is any combination of tokens that has a value Single expressions can be combined to make more complicated expressions (e.g. pow(2*3,3*2) ) Operators that take 2 inputs go in between their inputs Functions take their inputs in parentheses immediately after their names with the inputs (known as arguments) separated by commas
5
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition5 Wiley and the book authors, 2002 PHP’s Syntax Expressions are evaluated Whenever the interpreter encounters an expression in code, that expression is immediately evaluated Precedence, associativity, and evaluation order When in doubt about the precedence of operators in group expressions, use parentheses to group the expressions so that they execute in the order which you expect Precedence at the same level is evaluated left to right ( 3.0 / 4.0 / 5.0 evaluates to 0.15, not 3.75) Expressions and types “Usually” the programmer is careful to match the types of expressions with the operators and functions that combine them Common expressions are Mathematical (with mathematical operators combining numbers) Boolean (combining true-or-false statements with ands and ors) String (with operators and functions constructing strings of characters)
6
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition6 Wiley and the book authors, 2002 PHP’s Syntax Assignment expressions Very common type of expression where a variable (which always starts with a $ sign) is set to equal the result of evaluating some expression (e.g. $eight = 2 * (2 * 2) ) Even assignment expressions are expressions and so have values themselves. The value of an expression is the same as the value that is assigned. Therefore, you can use assignment expressions in the middle of more complicated expressions $ten = ($two = 2) + ($eight = 2 * (2 * 2)) A statement in PHP is any expression that is followed by a semicolon ( ; ) Reasons for expressions and statements For its value For its side effect
7
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition7 Wiley and the book authors, 2002 PHP’s Syntax Braces make blocks Although statements cannot be combined like expressions, you can always put a sequence of statements anywhere a statement can go by enclosing them in a set of curly brackets ( { … } ) Comments A comment is a portion of a program that only exists for the human reader of the code. They are invaluable in helping the next person who reads your code figure out what you were thinking (or not thinking) when you wrote it, even when that person is yourself C-style multiline comments: starts with character pair /* and ends with */ /* this is a multiline comment */ Single-line comments, # or //, comment to the end of the line
8
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition8 Wiley and the book authors, 2002 Variables The main way to store information in the middle of a PHP program is by using a variable (a way to name and hang onto any value that you want to use later) Variable errata All variables in PHP are denoted with a leading dollar sign ( $ ) The value of a variable is the value of its most recent assignment Variables are assigned with the = operator, with the variable on the left- hand side and the expression to be evaluated on the right Variables do not need to be declared before assignment Variables have no intrinsic type other than the type of their current value Variables that are used before they are assigned have default values After the initial $, variable names must be composed of letters, digits, and underscore characters. The first character after the $ may not be a number
9
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition9 Wiley and the book authors, 2002 Variables Assigning variables Just write the variable name and add a single equal sign followed by the expression that you want to assign to that variable Reassigning variables There is no distinction between assigning a variable for the first time and changing its value later (even if the new value is a different type) Unassigned variables Default behavior of PHP is to allow the use of unassigned variables (variables which had not been assigned a value) without errors. Display warnings for use of unassigned variables by putting the statement, error_reporting(E_ALL);, at the top of the script The default value of any unassigned variable used in the context of a numeric expression will be the value 0. If used in the context of a string expression, it will be the empty string
10
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition10 Wiley and the book authors, 2002 Variables Because variables do not have to be assigned before use, you can check whether or not a variable has been created using the isset function if (!isset($var_name)) To “unassign” a variable, use the unset function unset($var_name);
11
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition11 Wiley and the book authors, 2002 Variable scope “Scope” is the technical term for the rules about when a name has the same meaning in 2 different places, and when they refer to 2 different things Any PHP variable that is not declared inside a function has global scope and is accessible throughout a given thread of execution except inside functions The assignment of a variable will not affect the value of variables with the same name in other scopes Each time a PHP page completes execution, variables defined are lost Within functions in your PHP script, it can only access the parameters in the function definition and variables defined locally within the function (unless specifically overridden) Variables “persist” through multiple PHP sections in a single document
12
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition12 Wiley and the book authors, 2002 Constants Constants are similar to variables except their values cannot be changed through the normal execution of your script Constants do not have a $ before their name Constants can only have “scalar” values (numbers, strings, and booleans) Constants have global scope (even inside functions) You can create your own constants with the define “function” define(MY_CONSTANT, 42);
13
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition13 Wiley and the book authors, 2002 Output Most PHP statements execute silently (e.g. they don’t produce any output The only way embedded PHP code will display anything is with statements that print something to output or calling functions that call print statements Two most basic constructs for printing to output are echo and print Can be used with or without parentheses (although it is recommended to use them only with parentheses) echo can use multiple “arguments” without the parenthesis print can use only one “argument” with or without parenthesis print returns a value which represents whether the print statement succeeded print & echo will convert numbers to strings before printing them
14
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition14 Wiley and the book authors, 2002 Variables and strings String constants can be defined with single ( ‘ ) or double ( “ ) quotes PHP does some preprocessing of doubly quotes strings before constructing the string value itself Variables included in double-quoted strings are replaced with their values $var_name = ‘Andrew’; $var_output = “$var_name is a wonderful teacher\n”; print ($var_output); Would output Andrew is a wonderful teacher Multiple-character escape sequences are replaced with their single-character values (e.g. \n is replaced with the “end-of-line” character) Single-quoted strings does not perform any variable interpolation on them and only pays attention to 2 escape sequences, \’ and \\. Consequently, single-quoted strings execute much faster than equivalent double-quoted strings Note: To output a break in the browser window, you need to output a string of ‘ ’, not the new-line escape sequence. However, it is still a good idea to output new-line characters to make the HMTL generated easier to debug if necessary.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.