Syntax Guidelines and Rules

Published :
Author :
Adam Khoury
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(); ?>

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