Primer Page

Welcome to ComputerTechMonkey.net

Variable

A Variable is way to store a value in memory so it can accessed and use later for some purpose. It can be different type of values like a number, boolean or string of characters and always starts with a $ sign and named what ever you want it to be.

Examples:

$variable1 = 23;
$variable2 = 'a string of charactors';
$variable3 = true; // or false

Possible pitfalls:

Always include the $ in front of the name of the variable and make sure not to start the name with a number.
Carful not to use any predefined php variable names.
Don't forget to add a semicolon at the end.

Variables - php.net

Strings

(echo, print, single & double quotes)

Strings are lines of text that you can stored in a variable or directly into a function using single or double quote signs and is printed on screen using the echo or print commands.

Examples:

$string1 = 'This is a string of text';
$string2 = "this is also a string of text";
print('you can add text here also');
echo("or you can do this, $string2");

Possible pitfalls:

Forgetting the start and end quote marks.
Forgetting the parentheses or semicolon.

Strings - php.net

PHP code block, PHP comment

A PHP code block is the code that is typed between the opening and closing tags.

A comment is contained inside of the code block, can be a single line or a comment block and is normally used to describe what some code does.

Examples:

<?php // this is the opening code block tag for php

// a single comment line begins with two slashes.
/* a block comment begins with a slash and star /* and
ends with a star and then a slash.
*/
// below is the closing code block tag for php
?>

Possible pitfalls:

Forgetting the proper opening and closing php code tags.
Forgetting to end a comment block with */

Code block & PHP comments - w3schools.com

Array - associative

Associative Arrays are arrays that have a key that is associated to a value in the array of values. These values can be the same types as variables.

Examples:

// names are the keys
$nameage = array("Jim" => 52, "Jill" => 27, "Jack" => 33);
// The abbreviated month is the key
$months = array("Jan" => "January","Feb" => "February","Mar" => "March","Apr" => "April");

Possible pitfalls:

Forgetting to quote each associative key.
Forgetting the assignment => ("Jan" => "January").

Associative Arrays - w3schools.com

Array - numeric

Numeric Arrays have an index number assigned to each value in the array. This is the defaulted type when no association is given.

Examples:

// An array with no index assigned will start from 0
$cnt = array(1,2,3,4,5,6,7,8); // index 0 will have the value 1, index 1 will have 2, etc.
// You can also change the index start point by assigning a start for the first one
$cnt = array('5' => 1,2,3,4,5,6,7,8); // index will start at 5 and will have the number 1 as the value

Possible pitfalls:

Incorrectly assigning the array. $variable = array(1,2,3);

Numeric Arrays - w3schools.com

Array - Multidimensional

A multidimensional array is an array that also has another array inside it making more then one array.

Examples:

/* multidimensional array
* This array has a first array that contains an array with "one" => "123", "two" => "456", "three" => "789")
* and a second array that contains an array with "four" => "1011", "five" => "1213"
*/
$counting = array("first" => array("one" => "123", "two" => "456", "three" => "789"),
"second" => array("four" => "1011", "five" => "1213"));

Possible pitfalls:

Incorrectly assigning the multidimensional array.

Multidimensional Arrays - w3schools.com

Operators

Operators are what programming languages use to manipulate values or variables.
(assignment '=', addition '+', subtraction '-', multi '*', division '/', append '.')
=, += , -= , *= , /=

Examples:

$var1 = 1; // equal sign assigns 1 to the variable $var1
// adds 1 + 2 and assigns it to $var2
$var2 = 1 + 2; // you can also use -, *, /
// takes whatever $var2 has, adds 3 and assigns the total back to $var2
$var2 += 3; // you can also use -= subtraction, *= multiplies, /= divides

Possible pitfalls:

Forgetting to put the semicolon at the end.
Incorrectly assigning a value.

Operators - tizag.com

Comparison Operators

Compares relationships of variables or values to see if they meet a true or false condition.
( == equal-to, != not equal-to , < less-than, > greater-than, >= less-than or equal-to, <= greater-than or equal-to )

Examples:

($var1 == $var2) // comparison to see if both variables are equal ($var1 != $var2) // comparison to see if both variables are not equal ($var1 < $var2) // comparison to see if var1 is less-than var2 ($var1 > $var2) // comparison to see if var1 is greater-than var2 ($var1 <= $var2) // comparison to see if var1 is less-than or equal-to var2 ($var1 >= $var2) // comparison to see if var1 is greater-than or equal-to var2

Possible pitfalls:

Accidently using only one equal sign and making var1 equal to var2 instead of checking to see if they are equal

Comparison Operators - tizag.com

Logical Operators

Logic operators are to test two or more conditions and are used in conjunction with comparison operators.
( && , and , || , or , ! , not )

Examples:

// checks to see if both conditions are true
($var1 == $var2 && $var3 == $var4) // && same as and
// checks to see if one or the other condition is true
($var1 == $var2 || $var3 == $var4) // || same as or
// checks to see if var3 and var4 are not true
(!$var3 == $var4) // ! same as not

Possible pitfalls:

Make sure to use double equal signs

Logical operators - php.net

Conditional Statements

Conditional statements are command sets to perform different actions based on different conditions.
(if, if - elseif , if - elseif - else, switch)

Examples:

// if else, prints one thing if true and another for anything else
if ($var1 == $var2){
echo('only prints if true <br/>');
} else {
echo('only prints if not true <br/>');
}
// if elseif else, prints 1st thing if true or 2nd if that is true or the last if none are true
if ($var1 == $var2){
echo('only prints if true <br/>');
} elseif($var1 >= $var2){
echo('only prints if this one is true <br/>');
}else{
echo('only prints if everything else wasn't true <br/>');
}
/* Switch, checks the same variable for different values and runs something based on the value*/
switch ($inc) {
case 0:
echo "inc equals 0";
break;
case 1:
echo "inc equals 1";
break;
case 2:
echo "inc equals 2";
break;
}

Possible pitfalls:

Putting a semicolon at the end of the first line. if($var1 == $var2);
Forgetting to put the break; in a switch statement.

Conditional Statements - webcheatsheet.com

Loops

Loops are designed to continue to do something until a condition is met.
(for , while, do while, foreach)

Examples:

// for loop
for($inc = 0;$inc <= 4;$inc++){
print("$inc <br/>");
}

// while loop
$counting = 0;
while($counting < 10){ // it will check the condition then loop
print("Print this until we get to 10 times around $counting <br/>");
$counting++;
}

// do while loop
$counting1 = 0;
do{ // it will loop until the condition is met
print("Print this until we get to the num 4 $counting1 <br/>");
$counting1++;
}while($counting1 < 5)

// foreach loop
$myary1 = array("one", "two", "three");
foreach ($myary1 AS $another){
printf("%s ", $another);
}

Possible pitfalls:

Accidently putting a semicolon on the first line of a loop. for($inc = 0;$inc <= 4;$inc++);<-Right here
Making sure to put the brackets { opening and } closing.

While loops - w3schools.com For loops - w3schools.com