PHP: Hypertext Preprocessor - Study Notes
Chapter Summary
PHP (Hypertext Preprocessor) is a server-side scripting language used to build dynamic web pages. PHP code sits inside an HTML page and runs on the web server, and it works alongside HTML, CSS and JavaScript to produce pages that can read from a database. This chapter covers the basic syntax, how to declare variables, the data types, the echo statement for output, and the different groups of operators.
Learning Objectives
- Explain what server-side scripting is and why web pages need it.
- Describe the history and features of PHP, including the fact that it runs on many platforms.
- Explain how PHP works together with HTML, CSS and JavaScript.
- Declare variables and use the eight PHP data types.
- Write output correctly using the echo statement.
- Use arithmetic, assignment, comparison, logical, increment/decrement and string operators.
Key Concepts and Definitions
- Server-Side Scripting: Code that runs on the web server. The server sends only the resulting page to the client browser.
- Dynamic Web Page: A page whose content is produced when it is requested, so it can change with user input, database contents or the time of day.
- Variable: A name for a memory location that holds a value, which can be stored, changed and read while the program runs.
- Identical Operator (===): A comparison operator that returns true only when both the values and their data types are the same.
- Concatenation Operator (.): The string operator that joins two or more strings into one.
Worked Methods
Variable Declaration and Output
A PHP variable name starts with a dollar sign, and you create it simply by assigning a value, as in $marks = 92;. PHP is loosely typed: you do not declare a type, and the variable takes the type of the value you give it. $avg = 88.5; makes a float, and $name = "Kavin"; makes a string. The echo statement prints values. It can take several items separated by commas, as in echo $name, $avg;, or one string joined with the dot operator, as in echo "Name: " . $name;.
Operator Applications
Arithmetic operators such as + and % (modulus) do calculations. Comparison operators such as >= test how two values relate. Logical operators join conditions: AND (&&) is true only if both conditions are true, and OR (||) is true if at least one is. The increment (++) and decrement (--) operators can be written before the variable (prefix) or after it (postfix), and the position decides whether the value is changed before or after it is used.
Common Exam Traps
- Case sensitivity: Variable names are case-sensitive, so
$Userand$userare two different variables. Keywords, built-in function names and class names are not case-sensitive, so ECHO and echo both work. - == vs. ===: The equal operator compares values only, while the identical operator also compares types.
5 == "5"is true, but5 === "5"is false because one is an integer and the other is a string. - Prefix vs. postfix:
++$xincreases x first and then gives the new value.$x++gives the old value first and then increases x. If$x = 5, thenecho $x++;prints 5, butecho ++$x;prints 6. - Invalid variable names: After the $ sign, a name must start with a letter or an underscore, and may contain only letters, digits and underscores.
$2mark,$total-marksand$my nameare all invalid.
Exam Tips
- End every PHP statement with a semicolon (;). A missing semicolon causes a parse error.
- Be able to list and explain the eight data types: String, Integer, Float, Boolean, Array, Object, NULL and Resource.
- PHP runs on the server, so if you view the page source in the browser you see only the HTML output, never the PHP code.
- The single dot (.) joins strings, and dot-equal (.=) adds a string to the end of an existing variable.