PHP is an awesome language. Its main role is being a server-side language with a flexible syntax which is derived from C. PHP is used to fuel many webpages and is excellent at parsing text. Lets begin there.
All PHP scripting is between and ?> tags. We can output text to the screen using the echo command.
A single character represents one byte of data. A series of characters is commonly referred to as a string.
We can concatenate, or join strings using the dot. Here's an example of appending text.
<?
$string1 = "Hello";
$string2 = "World";
echo $string1 . " " . $string2;
?>
The above is also an example of setting variables. All variables begin with a $ in PHP. All commands end with a semi-colon. ;
I prefer to concatenate strings like above, but PHP's syntax is very flexible and due to the nature of double quotes, we could say the same as above
<?
$string1 = "Hello";
$string2 = "World";
echo "$string1 $string2";
?>
We can use single quotes instead, and these are interpreted as literal text. I often use double quotes, where you need to escape a few specific characters.
<?
$newlines = "This text\n\nHas two newlines"; // \n example
$tab = "This text\tcontains tab"; // \t example
$dollar = "Total: \$4.50";
$backslash = "And of course it takes two backslashes \\ to get one";
$literal = 'This string uses single quotes';
$literal .= 'I can write $4.50 and \ backslash if I want';
?>
Comments can be done in three different ways:
<?
$example = "This line runs, or performs";
/* $example .= "This line does not because it is commented out using C style of comments */
?>
I use C style comments often because they are a very easy way to comment out many lines of code using only 4 characters. Begin commenting with a /* and everything will be commented until */ gets recognized.
C++ style of comments are also available. Just use two forward slash characters and everything from that point until the end of the line are disregarded
<?
$example = "This line runs, or performs.";
// $example .= "this entire line is commented out
$example .= " This part runs"; // but this does not
?>
And finally, the third way is an alias of the C++ style of comments, using only one character, the #
<?
$example = "This line works";
# $example .= "This line is commented";
?>
Commenting your code is also very helpful when editing. Instead of analyzing 60 lines of code and deciphering what it does, you could leave a comment for yourself that explains 'this connects to the database, and attempts to login using the credentials provided, redirect to apropriate page if user is so-and-so, redirect elsewhere for user so-and-so, redirect back to login if failed. etc.
Math
The "order of operations" they taught you in grade 6 still apply. Again, those are that Multiplication and Division occur before addition or subtraction. (Unless parenthesis are used to override the order of operations). The PHP Interpreter will analyze the math from the innermost parenthesis sets then outwards.
The multiplication symbol is an asterisk (*) and the division symbol is one forward slash (/). The modulus (%) is available for finding remainder of division.
<?
$simple = 5 + 5;
echo $simple; // displays 10
$intermediate = 5 + 23 * 4 - 2 + 18 / 2;
echo $intermediate;
// due to the natural order of operations, 23*4=92 and 18/2 = 9. Therefore 5 + 92 - 2 + 9 = 104 is the final answer.
$advanced = 5 + 23 * ((4-2) + 18) / 2;
echo $advanced;
// due to the parenthesis taken in to consideration first, 4-2=2, 2+18=20, 23 * 20 = 460, 460/2 = 230, therefore 5+230 = 235.
$modulus = 7 % 5;
echo $modulus; // displays 2. The remainder of 7/5
?>