Syntax Guidelines and Rules
Code Comments in PHP
Code comments are usually used to remind yourself and others about what a script is doing and where it is doing it.
<?php
# Single line comment
// Single line comment
/*
Multiline
Comment
*/
?>
PHP Ignores Whitespace Inside Its Tags
You can minify and slim your code slightly by removing all whitespace and new lines. If you are demonstrating code it is best to not minify your code if you wish to allow people to read it easily. They can minify their code later.
<?php
$name="John"; // Good and minified
$name = "John"; // Good but not as slim byte-wise, easier on the eyes
?>
Rules Regarding Variable Naming
Standard variables in PHP always begin with a dollar sign($). Use only alphanumeric characters and underscores([ a-z0-9_ ]) in your variable and function names. Never begin a variable or function name with a number, and try not to use any Reserved Words(see reserved word list below) in PHP.
<?php
$var1 = "value"; // OK
$user_1 = "value"; // OK
$1var = "value"; // Error
$|*% = "value"; // Error
?>
Semicolon Terminates Lines and Statements
Semicolon tells the compiler that the line is terminating at that point. If you forget the semicolon PHP will treat all of your code as one continuous statement or expression and syntax errors will arise.
<?php
$user_1 = "Sandy"; // GOOD
$user_1 = "Sandy" // BAD
$user_1 = "Sandy"
; // OK but is not the best practice
?>
PHP is Case Sensitive
The following variables are different objects since PHP is case sensitive in its syntax.
<?php
$var = "moon";
$Var = "sun";
echo "The ".$var." rises when the ".$Var." sets.";
// The moon rises when the sun sets.
?>
Dynamically Naming Variables in PHP
It might be a rare occasion that you must use this, but here is how you can use one variable's string value as the name for another newly created variable.
<?php
$var = "dynamo";
$$var = "I like playing in the park";
echo $dynamo;
?>
String Concatenation in PHP
We use the period(.) to concatenate(join strings or append variable data into strings) in PHP. If you are familiar with JavaScript it performs the same append operation that the plus sign(+) does in JavaScript regarding string concatenation.
<?php
$animal_1 = "dog";
$animal_2 = "cat";
echo "A ".$animal_1." enjoys chasing a ".$animal_2;
echo 'A '.$animal_1.' enjoys chasing a '.$animal_2;
?>
Escaping Quote Marks With Backslash
If we use double quotes to encapsulate our string we must escape any double quotes that need to reside in the string. The same logic applies to single quotes.
<?php
$str1 = "I watched \"The Avengers\" and it was cool.";
$str2 = 'I watched \'The Avengers\' and it was cool.';
$str3 = "I watched 'The Avengers' and it was cool.";
$str4 = 'I watched "The Avengers" and it was cool.';
echo $str1; // I watched "The Avengers" and it was cool.
echo $str2; // I watched 'The Avengers' and it was cool.
echo $str3; // I watched 'The Avengers' and it was cool.
echo $str4; // I watched "The Avengers" and it was cool.
?>
@ Error Suppression Character
You may find some scripts that have an "@" symbol before certain expressions, used to suppress any error messages that the expression may generate.
<?php
function sample(){
return "Sample Data";
}
$data = @sample();
?>