Case 2 PHP Primer
Variable- I once heard a person describe a variable as a noun in a sentence. This could be adapted to any programming language.
$variable="Some mumbo jumbo";
In PHP, there are some strict guidelines for variables such as, the variable name has to be all lowercased, and must begin with a dollar sign.
A good reference to variables can be found at: Websitetheme.com
=====================================================================================
Strings- Can be thought of as a value. In basic algebra for example we’re used to seeing the example that x=3. The string would be 3, because it was the value that was assigned by x.
$variable="This is the String"
One important requirement for strings is that they need to have something attached to them so they can be recognized such as quotes in front and behind, apostrophes work well too.
A good reference to strings can be found at: php.net
=====================================================================================
code block- Is the standard way in which a set of coding is initiated and terminated. Just like a person can’t build a house without some kind of roof that is sustained by walls, coding cannot exist without being properly identified as to which programming language it is.
<?php
Some kind of coding in here
?>
The requirements are rather simple for PHP code block. To begin a code block a less than symbol is needed followed by a question mark, php is then added so the computer can recognize the programming language. After the opening code block, the coding is inserted then when a person is ready to terminate the coding, they insert a question mark followed by a greater than symbol.
A good reference to code block can be found at: Build Website 4 U
=====================================================================================
comment- Many times when programmers are creating their works of art in a programming language, they sometimes need to give themselves pointers. This is where comments come in handy. Comments can be used for a variety of reasons, but it is beneficial to have them. Comments can also be used when experimenting with coding , when a programmer wants to “remove” the php without literally removing it. If a coding line is commented out, it is as if it doesn’t exist.
// comment looks like this
/* comments
On
Multiple
Lines
*/
It is basically essential to begin comments with either a double / or when using multiple lines to begin with a /* and end with */
If these / and * aren’t used with comments then there will be plenty of coding errors because coding won’t be used.
A good reference to comments can be found at: tizag
=====================================================================================
Array- An array can be thought of as a form of a crude table that is utilized by the computer to find values. There are three kinds of arrays that are often used in programming they would be the following:
Associative- Is more or less how it sounds. By association a value is assigned. An example of this would be assigning the “cell” abc to have the value of 123. By doing this abc = 123 it is associated.
Numeric- Numeric is rather basic too. An array would have their “cells” identified as a numeric value and within those “cells” there would be the actual value.
Multidimensional- Is a little more complex. Breaking down the word it indicates that there are multiple “tables” that are used. Each of these specific “tables” have something in common with the content.
Examples:
Associative:
<?php
$array['associative'] = "1";
$array['numeric']= "2";
$array['multidimensional']= "3";
echo "Associative Array is #" . $array['associative'] . " on the list.";
echo "Numeric Array is #" . $array['numeric'] . " on the list.";
echo "Multidimensional Array is #" . $array['multidimensional'] . " on the list.";
?>
Numeric:
<?php
$pizza[0]="Pepperoni";
$pizza[1]="Sausage";
$pizza[2]="Ham & Pineapple";
$pizza[3]="Black Olive";
echo $pizza[0] . " and " . $pizza[2] . " are top sellers in the pizza industry.";
?>
Multidimensional:
$apartments = array
(
"Viking Village"=>array
(
"304",
"202",
"108"
),
"The Pines"=>array
(
"116"
),
"Somerset"=>array
(
"310",
"102",
"106"
)
);
Due to the complexity of arrays there is plenty of room for errors and pitfalls. One important thing to always remember when it comes to numerical arrays is that the first array always starts with a 0, not a 1. Also be sure to using names that are easy to remember if you go for an associative array.
A good reference to arrays can be found at: w3schools
=====================================================================================
Operators- Operators can be thought of as the basic, simple arithmetic functions.
Assignments- are a bit of a tricky one. It is important to pay attention to the actual word, assignment. A value is “assigned” to a variable.
Addition- is represented by the + symbol
Subtraction- is represented by the – symbol
Multiplication- is represented by the * symbol
Division-is represented by the / symbol
<?php
$a = 1;
$b = 2;
$c = $a + $b;
$d = $c - $a;
$e = $c * $b;
$f = $e / $b;
?>
Mathematics and IT majors don’t go well together and for this reason there are more than plenty pitfalls and mistakes made. The one mistake that is often made is people often get all the different variables mixed up and forget the assignments confused.
A good reference to operators can be found at: w3schools and
php.net
=====================================================================================
Comparison Operators- It as the name implies, the essence of a comparison operator is to “compare.” This can be compared in a variety of ways:
Equal to- This is self-explanatory on value or variable is equal to another. Represented as ==
Not equal to- Once again self-explanatory, the value or variable is not equal to the other.
Represent as!=
Less than- the value or variable is less than the other. Represented as <
Greater than- the value or variable is greater than the other. Represented as >
Greater than or equal to- the value or variable is greater than or equal to the other.
Represented as >=
Less than or equal to- the value or variable is less than or equal to the other. Represented as <=
$one == $one
$two != $one
$one < $two
$two > $one
$something >= $other
$something <= $other
One of the common pitfalls and errors that is commonly committed is people mistaking the greater than and the less than symbols. As a reference, I think back to my dear days of second grade. My teacher taught me a basic principle that was taught, “the alligator” eats the bigger number. This reference has always been an easy help. So if 5 is on the left and a three is on the right, “the alligator” eats the bigger number on the left.
A good reference to comparison operators can be found at: php.net
=====================================================================================
Logical Operators- is a way to include all kinds of variables by linking them by using words such as AND, OR, and NOT. This are represented by the symbols of & for and || for or, and ! for not. In order for it to work, one or both statements have to be true. It’ll all make sense in the example.
$and = (black && white);
$or = (left || right);
$not = (this != that);
The complexity of the logical Operators makes it rather easy to make mistakes. The best way to prevent from making mistakes is to properly understand each operator and what they do.
A good reference to Logical Operators can be found at: php.net
=====================================================================================
Conditional Statements- This is another one of those words that work great when you break it down. A statement based on a condition. Just like growing up, when you’re parents would say, if you don’t behave yourself then you won’t get desert. It is the same with php. An if statement is used, and then a then is stated what will happen. Sometimes an extra step is taken so if the then isn’t completed something else will happen.
if ($definition == $good)
{
Echo “Wonderful, glad you enjoyed it.”;
}
Else
{
echo “Well, maybe you should give it a try!”;
}
Just as in all programming there is plenty of room for mistakes. In the conditional statement, it helps to talk it through outloud as well as mentally think it through. Remember that sometimes the else statement could sometimes have elseif too!
A good reference to conditional statements can be found at: webcehatsheet.com
=====================================================================================
Loop- is essentially how it sounds, going round, and round infinitely. Loops can be broken when a set statement is triggered.
For ($variable = 1; $variable++)
{
If ($variable > 25)
{
break;
}
Echo $variable;
}
One pitfall that should be watched for is to make sure that conditions are set so it will be known what to do as the code processes.
A good reference to loops can be found at: php.net