PHP: The Basics CISC 282 October 18, 2017 Approach Thus Far User requests a webpage (.html) Server finds the file(s) and transmits the information Browser receives the information and displays it HTML, CSS, and/or JavaScript Without JavaScript, a website is entirely static Server returns the same information User sees the same content 2
A Different Approach User requests a webpage (.php) Server finds the file and executes it The file is in fact a program Program creates the information for transmission Browser receives the information and displays it HTML, CSS, and/or JavaScript This server-side scripting produces dynamic content Server creates different information User sees different content 3 PHP Primarily a procedural programming language Created by Rasmus Lerdorf in 1995 Adopted and augmented by other developers It is open-source Acronym represents "PHP Hypertext Processor" Initially meant "Personal Home Page" Very popular and widely available Website is www.php.net Complete documentation about the language 4
PHP vs. JavaScript Some similarities Interpreted languages Relaxed syntax Untyped variables Some differences Interpreted server-side JavaScript is interpreted in the browser You'll need to upload to the CISC 282 server for testing Embedded in HTML content Considered bad practice in JavaScript Errors are displayed as output (or lack thereof) in HTML JavaScript errors are more silent 5 Basic PHP Embedding HTML Source <?php PHP code?> HTML Source filename.php Opening tag Closing tag Interpreted and transmitted PHP block /filename.php All HTML content web browser 6
PHP Blocks Several blocks can be embedded in the same file When a browser requests a.php file The server applies the PHP preprocessor All HTML content is ignored All PHP code is interpreted Each PHP block is replaced by its output The server then transmits the results Can't see embedded code using "View Page Source" Can't see it in the browser inspector either 7 Coding Fundamentals End all expressions with a semi-colon Not optional as in JavaScript Output is produced with echo or print statements echo "output" or echo 'output' Functions are called in the usual manner functionname(arg1,,argn); Comments are designated in three different ways # and // for single lines /* */ for several lines 8
Fundamental Types Type Examples string "in double quotes", 'in single quotes', int 1, 3, 5, 7, 11, float 2.15, 0.3, boolean TRUE, FALSE (case insensitive) array [1, 3, 5, 7], object new classname(), NULL NULL 9 Working with Types Can use casting to convert from one type to another float int: rounded down string int/float: the integer/float value in the prefix All characters in the postfix are ignored 0 if there's no integer/float prefix Casting has precedence over many operators Use parentheses to ensure the correct result There are several functions that test type gettype(arg) returns a string e.g., gettype("3.1") = "string"; is_typename(arg) returns a boolean e.g., is_int(4) = TRUE and is_string(4) = FALSE; 10
Arithmetic Operators +, -, *, / and % Follow typical rules of precedence Automatically switch between types Mixing ints and floats always yields a float Division with ints may yield a float Constants M_PI: M_E: e M_LN2: log e 2 11 Some Arithmetic Functions abs(x) Function ceil(x), floor(x) min(x 1,, x N ), max(x 1,, x N ) Result The absolute value of x The value of x rounded up/down The smallest/largest value in the given list of arguments rand(x, y) A random integer in [x, y] sqrt(x) The square root of x 12
Variables Declared using the syntax $varname = varvalue; Not typed Can store a value of any variety What if you accidentally use an undeclared variable? A warning message is generated An empty value is substituted for the default (NULL) 0, 0.0, "", etc. Basic assignment operator is = +=, =, *=, /= and %= also perform the associated operation ++ and increment and decrement.= also performs string concatenation 13 Strings Enclosed in either single- or double-quotes Can access characters like elements in an array e.g., $sample = "a string" $sample[0] "a" and $sample[3] "t" Strings are concatenated with the. operator e.g., "a"."string" "astring" PHP automatically converts between types for + and. e.g., 2 + 4. "6" + 8 74 Use the typical sequences for special characters \\, \n, etc. 14
Some String Functions Function strlen(astring) trim(astring), ltrim(astring), rtrim(astring) strtoupper(astring), strtolower(astring) ord(achar), chr(anint) printf(astring) Result The number of characters in astring The contents of astring with the whitespace removed on one or both sides The contents of astring in upper/lowercase characters The corresponding ASCII/character value for the given parameter The content of astring formatted as printf would in C 15 Interpreted Strings A string that uses the values of variables e.g., "You attend $uni" "You attend Queen's" Double-quotes are required Single-quoted strings are not interpreted in this manner Enclose variables in braces to place within a word e.g., "You attend {$uni}u" "You attend Queen'sU" 16
Using Logic Boolean values are TRUE and FALSE In non-logical expressions TRUE evaluates to 1 FALSE evaluates to 0 or an empty string e.g., 1+TRUE 2 and 1.FALSE "1" Several values are considered falsey 0 and 0.0 "0" and an empty string NULL and undefined variables (by extension) An empty array All other values are considered truthy 17 Logical Operators Operator Description!=, == (In)equality of values!===,=== (In)equality of values and types <, <= Less than (or equal to) >, >= Greater than (or equal to) &&,,! AND, OR and NOT 18
if Statements if (condition) { } elseif (condition) { } else { } or if (condition): elseif (condition): else: endif; 19