0% found this document useful (0 votes)
7 views4 pages

PHP Tag Usage Guidelines

The document provides PHP coding style guidelines and best practices including: - Always use <?php ?> tags and omit the closing tag. Use double quotes for variables in strings and single quotes otherwise. - Follow brace styles like if/elseif/else on the same line. Name classes with uppercase and underscores, methods with lowercase camelCase. - Always use === and !== for boolean comparisons. Typecast values before database insertion. Avoid duplicate code by creating methods or libraries.

Uploaded by

Michael Miko
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

PHP Tag Usage Guidelines

The document provides PHP coding style guidelines and best practices including: - Always use <?php ?> tags and omit the closing tag. Use double quotes for variables in strings and single quotes otherwise. - Follow brace styles like if/elseif/else on the same line. Name classes with uppercase and underscores, methods with lowercase camelCase. - Always use === and !== for boolean comparisons. Typecast values before database insertion. Avoid duplicate code by creating methods or libraries.

Uploaded by

Michael Miko
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PHP opening/closing tag

[Link],Example:<?=$var?>. AlwaysOMITtheclosingtag.

Single and double quotes


Usedoublequoteswhenyouhavevariableinthestring,[Link] ignoreeverythingwithinsinglequotes,thereforefasterprocessing. Example echo <a href=$url>Link</a>; echo <a href=hello/world>Link</a>;

Brace style
if (condition) { action1(); } elseif (condition) { action2(); } else { action3(); }

Naming convention
[Link] separatedwithunderscore. Incorrect class userCollection class UserCollection Correct class User_collection

[Link] separatedwithcamelCase. Incorrect function getcew() function GetCrew() function get_crew() Correct function getCrew() Privatemethodsshouldbeprefixedwithandunderscore Example private function _getUserCrew() DBcollectionname&[Link] shouldbeseparatedbyunderscore. Incorrect $usercrew $userCrew $User_crew Correct $user_crew

Comparing boolean values


Alwaysuse===and!==[Link] or0wouldbeevaluatetoFALSEinloosecomparisons.

Typecasting
AlwaystypecastnumericvaluesbeforeUpdating/InsertingintotheMongoDB. ThisistoavoidsavingInteger/FloatvalueasStringinDB,whichwillhaveproblem whenyoudoquery.

DRY - Dont Repeat Yourself


[Link],createa methodoralibrary. IfsamepieceofcodeisrequiredonseverallocationwithinthesameClass,please createanewmethodforit. IfsamepieceofcodeisrequiredonseveralClass,pleasecreateanewlibraryforit.

Clever code
Ingeneral,readabilityismoreimportantthanclevernessorbrevity.

Redirect
Minimizeredirect()usage,whenrequiredtouse,alwaysdie()orexit()immediatelyafter redirect().

Memcached Usage
Use<prefix>_<key>_<id>formatformemcachedkeywheneverpossible. Prefixpsshortforpirateship. [Link]:user_crew,fruit_skill,fruit_exp,quest_area... IDtheassociatedIDfromDB. InfullyourMCkeymaylooklikethat: ps_user_crew_[user_id]>storingtheuserscrewdbdata. [Link] aretheavailableMCavailabletouse/clear/update&toreducechancesofsavingthesamepiece ofdataonseveralMC. Whenqueryingalistofdata,likeallgameitem,allcrew,allfruits,pleaseusethefollowing approach,usinguserscrewasexample: 1. QueryforlistofIDonly 2. StorelistofIDsinMC,ps_user_crew_list_[user_id] 3. StoreindividualusercrewdataindifferentMC,ps_user_crew_[user_crew_id] 4. LoopthroughcrewlistandgetcrewdatafromspecificMC 5. Querywhenneededandremembertoclear/updateMCwhenthereschanges Whenusingalibrary,thelibraryshouldmanagerelatedMC,andprovidewaystocleartheMC. Example: class Crew_library { var $enable_mc = TRUE; var $ci; function __construct() { $ci = &get_instance();

} function getUserCrew($id) { $data = array(); if ($this->enable_mc === TRUE) // note that MC key is stored in config file $data = $this->memcached_library->get(MC_USER_CREW . $id); if (empty($data)) { $data = $this->ci->user_crew_model->getById($id); if (!empty($data)) $this->ci->memcached_library->set(MC_USER_CREW . $id, $data, 3600); } return $data; } function clearUserCrewMc($id) { $this->ci->memcached_library->delete(MC_USER_CREW . $id); return TRUE; } } Note:

Haveaon/offswitchforeachclassforMC DonotdependentirelyonMC,alldevelopmentshouldbedonewithMCswitchedoff Remembertosetalogicalexpirytime Gameshouldwork100%withoutMC Remembertoclear/updateMClistwhennewdataisinsertedviaAdmin

You might also like