PHP Script Tags
There are multiple ways to apply PHP code blocks to documents. Using the standard tag is the recommended approach because it works in all PHP environments regardless of the server settings and helps with stable environment migration.
PHP standard tag
// script goes here...
PHP short tag
// script goes here...
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.
$var1 = "value"; // OK
$user_1 = "value"; // OK
$1var = "value"; // Error
$|*% = "value"; // Error
PHP Ignores Whitespace
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.
$name="John"; // Good and minified
$name = "John"; // Good but not as slim byte-wise, easier on the eyes
Semicolon Terminates Lines and Statements
Semicolon tells the compiler that the instruction is terminating at that point. If you forget the semicolon PHP will treat all of your code as one continuous statement or expression until it meets a semicolon or the end of the script, which could prevent the script from running.
$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.
$var = "moon";
$Var = "sun";
echo "The ".$var." rises when the ".$Var." sets.";
// The moon rises when the sun sets.
Use one variable's value as the name for another variable.
$var = "dynamo";
$$var = "I like playing in the park";
String Concatenation in 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.
$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.
return "Sample Data";
$data = @sample();
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.
// Single line comment
# Single line shell style comment
Reserved Words in PHP
You should not use the following PHP language constructs as your function, constant, class or method names. They perform special operations built into PHP or have significant values by default.
Keywords in PHP
__halt_compiler, abstract, and, array, as, break, callable, case, catch, class, clone, const, continue, declare, default, die, do, echo, else, elseif, empty, enddeclare, endfor, endforeach, endif, endswitch, endwhile, eval, exit, extends, final, for, foreach, function, global, goto, if, implements, include, include_once, instanceof, insteadof, interface, isset, list, namespace, new, or, print, private, protected, public, require, require_once, return, static, switch, throw, trait, try, unset, use, var, while, xor
Predefined Classes in PHP
Directory, stdClass, __PHP_Incomplete_Class, Exception, php_user_filter, Closure, self, static, parent
Predefined Constants in PHP
PHP_VERSION, PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, PHP_VERSION_ID, PHP_EXTRA_VERSION, PHP_ZTS, PHP_DEBUG, PHP_MAXPATHLEN, PHP_OS, PHP_SAPI, PHP_EOL, PHP_INT_MAX, PHP_INT_SIZE, DEFAULT_INCLUDE_PATH, PEAR_INSTALL_DIR, PEAR_EXTENSION_DIR, PHP_EXTENSION_DIR, PHP_PREFIX, PHP_BINDIR, PHP_BINARY, PHP_MANDIR, PHP_LIBDIR, PHP_DATADIR, PHP_SYSCONFDIR, PHP_LOCALSTATEDIR, PHP_CONFIG_FILE_PATH, PHP_CONFIG_FILE_SCAN_DIR, PHP_SHLIB_SUFFIX, E_ERROR, Error reporting constant, E_WARNING, E_PARSE, E_NOTICE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED, E_ALL, E_STRICT, __COMPILER_HALT_OFFSET__, TRUE, FALSE, NULL