Case 2: PHP Primer

Variable

A variable is a thing that holds space for a value. It is represented in PHP with a $ sign followed immediately by the variable name.

Example of a variable: $variableexample = 5;

This is a variable that is given a value of 5.

The name can be whatever you want, just needs to start with a $. Also, the name cannot begin with anything but a letter or an underscore. After a letter or underscore is used the name can consist of letters, numbers, or underscores. Variables in PHP are case sensitive. $This is a special reserved variable that cannot be assigned.

PHP.net Variables



Strings - echo, print, single vs. double quotes

echo outputs a string when the code is ran.

Example: echo “What a nice day”

Print also outputs a string.

Example:
print (“What a nice day”);
or
print “What a nice day”;

When using variables inside the string, single quotes will print the variable name, while double quotes will print the variable value.

Example:
$examplevariable = “Why?!?!?!?!”;
Echo “$examplevariable , just because” //would output: Why?!?!?!?! , just because
Echo ‘$examplevariable , just because’ //would output: $examplevariable , just because

It is important to note that print can use parenthesis but it doesn’t have to. As far as I can tell echo doesn’t use parenthesis.

PHP.net Strings



PHP code block, PHP comment

A PHP code block is a portion of a script containing PHP code.

The code block starts with tag. In this example the code inside the block would display “this is a php code block.”

It is important to note that on servers with shorthand support, you can just use a grater than followed by a question mark to start the code block. It is suggested that you use the regular to start the block because not all servers have shorthand support.

A PHP comment, just as in many other programming languages, is a comment that can be inserted into the code that will not be read by the compiler.

A PHP comment can be a:

// for single line comments

or

/*
For multiple line
Comments
*/

PHP.net Variables



Array – Associative

An associative array is on in which each element has an associated id key.

$liftarray = array(“bench press” => 385, “leg press” => 1090);

In this example a number (pounds I can lift) is associated to the lift. It can also be set up by individual elements in multiple statements

$liftarray[“bench press”] = 385;
$liftarray[“leg press”] = 1090;

This is good for associating values with elements to be used later. I.e. in this example I associated the lift with the value.

The array is created by creating a variable and giving it a value starting with array followed in parenthesies by the elements of the array. In the second example, the variable is created followed by brackets with the element inside of them. The key is given using the = id value.

W3C Schools Arrays



Array – Numeric

A numeric array is one in which each element of the array is stored with an index that reference the array.

There are two ways that numeric arrays can be created, The first is a normal array where elements are assigned a number automatically:

$examplearray = array(“one”,”two,”three”,”four”);

In this example the index is assigned automatically. The $examplearray[0] = “one”; $examplearray[1] = “two’; and so on.

The second is where the element in an array is inserted into a specific part of the index:

$examplearray[0]="one";
$examplearray[1]="two";
$examplearray[2]="three";
$examplearray[3]="four";

It is important to note that the index of a numeric array starts at 0. A common programming error is an assumption that the array starts at one. When manually inserting values into the array it is important to remember not to start at one.

W3C Schools Arrays

Array – Multidimensional

A multidimensional array is a term used to describe an array within an array.

$musclegroups = array
(
"chest"=>array
(
"bench press",
"incline press",
“flies”
),
"legs"=>array
(
"squat",
"leg curl",
"leg extension"
)
);

Going back to the idea of using lifts, the $musclegroups array has two arrays inside of it the chest array and the legs array. The elements in those arrays are the lifts that can be done to exercise those muscle groups.

It is important to note that the sub arrays are separated with a comma just like the values in those arrays. The sub arrays are labeled with an =>array next to the element followed in parenthesis by the elements of that array. The sub arrays are not set as variables even though other arrays are.

W3C Schools Arrays



Operators - Assignment, Addition, Subtraction, Multiplication, Division, Append

Operators are key symbols used to perform mathematical operations

Assignment operators are used to assign a value to a variable

Example: $examplevariable = 3;

In this example the = operator is used to assign the value 4 to the variable $examplevariable

The addition operator is a + and is used to perform addition

Example: $examplevariable = 99 +3;

The addition operator sets the value assigned by the assignment operator to 102.

The subdraction operator is – and is used to subtract one value from another.

Example: $examplevariable = 99 - 3;

The subtraction operator sets the value assigned by the assignment operator to 96.

The multiplication operator is * and is used to multiply one value by another.

Example: $examplevariable = 99 * 3;

The value returned by the multiplication operator is 297.

The division operator is / and is used to divide one value by another

Example: $examplevariable = 99 / 3;

The value assigned to $examplevariable is 33, 99 divided by 3

W3C Schools Operators



Comparison Operators - Equal to, Not equal to, Less than, Greater than, Greater than or equal to, Less than or equal to

The equal tooperator checks if two values are equal to each other.

Example: 4==5 //this would return false

The equal to is represented by the double equal sign (==) as mentioned above.

The not equal to operator checks to see if two number are not equal to each other

Example: 4!=5 //this would return true

As displayed above, the != sign represents the not equal to operator. It is important to note that it can also be represented by a greater than sign next to a lesss than sign.

The less than operator is used to see if one value is less than the other

Example: 4<5 //this would return true

As in most programing languages, less than is represented with its mathematical symbol <

The greater than operator is used to see if one value is less than the other

Example: 4>5 //this would return false

As in most programming languages, greater than is represented with its mathematical symbol >

The less than or equal to operator is used to see if one value is less than the other.

Example: 4<=5 //this would return true.

Less than or equal to is represented with the symbol <=.

The greater than or equal to operator is used to see if one value is less than the other.

Example: 4>=5 //this would return false

Greater than or equal to is represented with the symbol >=

W3C Schools Operators



Logical Operators - &, And, ||, Or, ! and Not

The and operator is used to see if one thing and another thing are true. It is represented with the double ampersands (&&)

Example: 3>2 && 2>3 // would return false because only one statement is true

The or operator is used to see if one thing or another thing are true. It is represented with the double bars (||)

Example: 3>2 || 2>3 // would return true because even though 2 isn’t greater than 3, three is greater than 2.

The not operator is used to see if one thing and not another thing are true. It is represented with the exclamation point (!)

Example: 3>2 ! 2>3 // would return true because 2 isn’t greater than 3, three is greater than 2.

W3C Schools Operators



Loops - For, Do, Do While, Foreach

for loop is one in which you specify a start and end point to how many time the loop is ran through as well as the incrementing condition

for ($conditionvariable = 1; $conditionvariable <= 10; $conditionvariable++)
{
echo "$condititonvariable is not yet equal to 10";
}

In this example the start point is $conditionvariable = 1; the endpoint is $conditionvariable = 10; and the increment is $conditionvariable++ (add one to the variable every time it runs through the loop)

A do loop executes code while a specified condition is true

This example starts with creating $conditionvariable and setting it as one. The while loop continues until untill $conditionvariable is no longer less than six. Every time the loop runs it will display “$conditionvariable is still less than 6”

A do while loop is a loop in which the code block is executed (do) and then the condition (while) is checked. If the condition is not met, the block is executed again.

$examplevariable = 1;
Do
{
echo “examplevariable is less than 10;
$examplevariable++;
}
while ($examplevariable < 10);

A foreach loop is one that loops through each element of an array.

$examplearray=array("Huston","Denver","Richmond");
foreach ($examplearray as $city)
{
echo $city;
}

In this example, the foreach loop goes through each element in the $examplearray array, puts it into a variable called $city and echos it.

W3C Schools looping