There is a lot of PHP specific syntax that every PHP programmer uses in their day to day work, that everyone has just learned that it is the way it is meant to be. But, to truly be able to go through, read someone’s code and truly understand it, you need to know exactly what the syntax means, and why it’s formatted that way. Throughout this small post, there are just some thoughts and practices for coding using the PHP syntax.
The Semicolon
The basic syntax of PHP, which is the first thing every PHP developer needs to know, is the semicolon. Most lines of code end with a semicolon, which essentially means, to execute the code that has been loaded before this semicolon and after the last one.
$foo = "bar";
$fooArray = "Bar",
"This",
"Something";
You can break your code into multiple lines in some cases, but technically the code above is still only two lines of code. The first line of code assigns the variable of $foo, and the second line sets an array called $fooArray. Even though the array is broken into three separate lines, it still executes as one line of code.
Notice how still, each line of code ends with a semicolon. You cannot split every line up like this, but when assigning arrays, building long strings, or even working with objects, you can drop down and create a new line. The reasoning behind this will be covered more in the “Making your code Pretty” article.
The period as a “String Connector”
Some languages use the plus symbol (+), some languages use variations of quotes, and PHP uses the period (.) as a string connector.
// --- Long Method
echo "Your IP address is: ";
echo $_SERVER['REMOTE_ADDR'];
echo " <br/>";
echo "The current date/time is: ";
echo date("Y-m-d H:i:s");
echo " <br/>";
// --- Short Method
echo "Your IP address is: ".$_SERVER['REMOTE_ADDR']." <br/>";
echo "The current date/time is: ".date("Y-m-d H:i:s")." <br/>";
The two above examples will yield the exact same results, example #1 not using any string connectors, example #2 joining strings using the period. You can use this syntax to set variables, within functions, or nested inside of loops. Essentially what you are doing is appending to the current string with what is after the period. By joining multiple strings together with the “something”.$something.”something” method, you can easily assemble long strings without having to write an incredible amount of code.
(Encapsulating with Parenthesis)
This is just like algebra folks… things within parenthesis will be evaluated before the rest of the equation.
echo 1 + 6 * (( 8 - 5 ) / 2 ); // Order of Operations: // 8 - 5 = 3 // 3 / 2 = 1.5 // 6 * 1.5 = 9 // 1 + 9 = 10 // echo 10
The comment above shows the order of operations, which is identical to that of an algebra equation. Evaluate the values inside the parenthesis first, work your way outwards, then use the standard order.
Order of Operations
Source: http://www.math.com/school/subject2/lessons/S2U1L2GL.html1. First do all operations that lie inside parentheses.
2. Next, do any work with exponents or radicals.
3. Working from left to right, do all multiplication and division.
4. Finally, working from left to right, do all addition and subtraction.
Brackets and Uses
Brackets, as in [], are primarily used for assigning or calling values from an array.
// Example #1
$array = array("red","green","blue");
echo $array[0]; // Will display "red"
echo $array[1]; // Will display "green"
echo $array[2]; // Will display "blue"
// Example #2
$array = array("color1"=>"red");
echo $array['color1'];
By default an array will populate its keys with numbers, in the above example, it does just that. When passing values into an array, each value is assigned an auto-incremented number, starting at zero. To reference these values in an array in the most simple fashion, you simply put the key you wish to call on within brackets.
These keys do not have to be numbers, they can be strings as well, and the values of the array can be pretty much any type of variable or object. The second example shows that you can pass in a string within the brackets to pull out the value for that specified key.
For more information on arrays, please see: PHP Basics – The Array.
What are those “Curly Brackets” used for?
The { and }? They are actually called “Braces” and are used to define the starting and ending of a structure of code. The most common use of them while starting is to start a loop or if-statement. The “Open Brace” or {, signifies we are starting an structure of code that will continue until the “Close Brace”, or }. This is easily seen in an if-statement as seem below.
if($foo == "bar") { // This brace starts the structure
// Do something!
} // This brace ends it
The code encased in the braces will only execute if the condition of the if-statement evaluates to TRUE. There are also other methods of starting and stopping an if-statement or loop, but we will wait to get into those later, as to not confuse people.
Braces are also used to define function, class and namespace contents. These are a little further along, but still solid examples.
function doWork($foo) {
// Perform Actions on $foo
return $bar;
}
class foo extends bar {
private $var = "test";
public function doWork($var) {
// Do Work!
$this->var = $mod;
}
}
Notice in the second example, there are nested braces within. Each brace has a start and a stop, telling the system which pieces belong in which places. When your code starts getting extremely messy or complex, its always a good idea to keep the braces lined up with the same spacing for ease of reading. Yet another topic for another day.
The Key=>Value Syntax
The last syntax covered in this post is the $key=>$value assignment syntax, which can be a little confusing at first. This is primarily used in arrays and is meant to set specific keys to specific values.
$key = "key";
$value = "value";
$array = array($key=>$value);
$array = array("key"=>"value");
echo $
The two above examples will both assign the $array variable to contain the same array; a single key named “key” will be assigned the value of “value”. The => syntax states that you are using a key=>value combo to pull this off. This can also be used in a foreach() loop to iterate through an array of values, as seen below.
$array = array("color1"=>"red", "color2"=>"blue");
foreach($array as $key=>$value) {
echo $key." = ".$value."<br/>";
}
// Outputs:
// color1 = red
// color2 = blue
The loop will go through every item in the $array variable, and display every key=>value combo within it.
Well, that’s it for the basics of syntax. Stick around for more info at another time!

Maybe you should right a small book?
I’m reading Learning php & mysql by o’relly, php & mysql for dummies and head first php and mysql. Your writing style is alot simpler to understand in some of these examples. Why not? You could post it online and charge a small fee.
. .
.
\ /
”””””
Do you still have a two-part edition of the show or is this limited just to summer time? ,
Been swamped recently with the new website, but I do intend to get back here to write up some more stuff someday