Section 0: Module Objectives or Competencies
Course Objective or Competency |
---|
The student will be able to write basic PHP applications in order to connect to and manipulate a database using PHP. |
Section 1: Basics
Some of the materials for these notes came from this text chapter.
A PHP script consists of a series of commands, or statements.
- Each statement is an instruction that must be followed by the web server before it can proceed to the next.
- PHP statements are always terminated by a semicolon (;).
Example:
$myTag = "paragraph";
echo "<p>This is a $myTag tag.</p>";
The echo statement is used to generate content (usually HTML code) to be sent to the browser.
- An echo statement simply takes the text it’s given and inserts it into the page’s HTML code at the position of the PHP script that contains it.
- The string of text can contain HTML tags.
- The example above generates the html statement "<p>This is a paragraph tag</p>", and it is displayed onscreen as a paragraph with the text "This is a paragraph tag."
Section 2: Variables and Data Types
All variable names in PHP begin with a dollar sign.
The dollar sign is followed by a letter or underscore, followed by any number of letters, numbers, or underscores.
$myTag = "paragraph";
$examNumber = 3;
Like JavaScript, PHP is a loosely typed language, which means that a variable may contain any type of data, be it a number, a string of text, or some other kind of value, and may change types over its lifetime.
The following statement assigns a new value to the existing $examNumber, and in the process the variable changes type because it now contains a string of text.
$examNumber = 'three';
Variable names are case-sensitive, so $examNumber and $ExamNumber are two distinct variables.
A list of PHP data types can be found here.
String literals can be enclosed in single or double quotes. link
Section 3: Constants
You can define constants with the const keyword.
Unlike with variables, you should not prepend a constant with a $.
Only scalar data (boolean, integer, float and string) can be contained in constants.
<?php
// works as of PHP 5.3
const courseNum = 'CIS-4407';
echo courseNum;
?>
Constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time; they cannot be declared inside functions, loops or if statements.
Section 4: Comments
Comments can be denoted by using /* and */ for multi-line comments, and // for single line comments. link
Section 5: Operators
Here are details about the PHP operators.
Assignment operators
The equals sign that was used in the variable declarations is called the assignment operator, as it’s used to assign values to variables.
- There are additional assignment operators, like +=, -=, *=, /=, %=, etc.
Arithmetic Operators
The standard arithmetic operators (+, -, *, /, %) may be used to perform various mathematical operations on values.
Comparison Operators
The comparison operators are fairly standard.
- The only notable feature is the not equal to can be written as != or <>.
Logical Operators
The logical operators are also fairly standard.
- && (and), || (or), !(not)
String Operators
Concatenation is performed using the . operator.
<?php
$txt1="Hello class!";
$txt2="Welcome to PHP!";
echo $txt1 . " " . $txt2;
?>
Section 6: Something Cool
Here is an example of something cool that can be done in PHP that you can't do in every language:
<?php
$var1 = 'PHP';
echo '$var1 rules!'; // outputs '$var1 rules!'
echo "$var1 rules!"; // outputs 'PHP rules!'
?>
If you surround a string with double quotes instead of single quotes you can include the name of a variable right inside a text string, and have the value inserted in its place.
- This process of converting variable names to their values is known as variable interpolation.
- However, as the previous line demonstrates, a string surrounded with single quotes will not interpolate the variable names it contains.
Of course, you can always use the concatenation operator (.) to generate the desired output:
<?php
$var1 = 'PHP';
echo $var1 . ' rules!'; // outputs 'PHP rules!'
?>