SlideShare a Scribd company logo
PHP Best Practices Bangalore PHP Users Meetup 31 st  October 2009 http://www.meetup.com/Bangalore-PHP-Users
Overview About this talk Coding Standard Documentation Sub Version General Practices
About this talk Common good practises for coding PHP Tips for clean PHP code How to avoid common mistakes Tricks and Tips Tools to ease your work
Use a Coding Standard
Why use coding standard? Consistency Readability Maintainability Collaboration
Okay, I’LL Create one…
Learn from others Don’t invent your own standard. All the issue has been debated to death. Use an established standard Stick to an standard you establish, don’t mix
What choices exist? PEAR Coding Standards http://pear.php.net/manual/en/standards.php Zend Framework Coding Standards http://framework.zend.com/manual/en/coding-standard.html eZcomponents Coding Standards http://ez.no/products/ez_publish/documentation/development/standards/php
Some Zend Framework standards Derived from PEAR standards One class, one file Underscore in class name map to directory separators: Zend_Controller_Action: Zend/Controller/Action.php
Some Zend Framework standards Naming conventions: Class name are MixedCase – Zend_Pdf Method name are camelCase - filterInput()   Constants are ALL_CAPS – SET_TIME Properties and variables are camelCase Private and protected member are _underscorePrefixed
Some Zend Framework standards Layout Conventions: No closing ?> tag for files containing only code Indentation: spaces only, no tabs;4 spaces per level of indentation No shell style comments(#) Keep lines no more than 75-80 characters long
Example
Any tool to check coding standards? PHP_CodeSniffer is one such tool: PHP_CodeSniffer is a PHP5 script that tokenises and "sniffs" PHP, JavaScript and CSS files to detect violations of a defined coding standard.  Your own coding standards. Subversion integration http://pear.php.net/manual/en/package.php.php-codesniffer.php
PHP_CodeSniffer Example Default uses PEAR style coding standard
PHP_CodeSniffer Example
Documentation
Documentation Documentation is the    most boring work Don't have time!
Documentation You don’t have time to code? Re-read your code 6 month after you wrote  it! Think about people who have to use your code Code should communicate its purpose The better the names, the fewer comments.
What choices exist? Source Documentation phpDocumentor http://phpdoc.org Doxygen http:// www.stack.nl/~dimitri/doxygen / End User Documentation DocBook http://www.docbook.org/
Documentation phpDocumentor Derived from Javadoc, written in PHP. phpDocumentor tags are the most used standard for generating documentation from php source code Other documentation generators, such as Doxygen, support these same tags. Don’t invent your own tags. Supported by a number of different IDEs. Zend Studio is perhaps the most prevalent. Command line or web interface. Not only HTML, but also .chm or PDF
Documentation phpDocumentor example
Documentation phpDocumentor example
Documentation
Documentation
Source Control
Why do I need it? How do i know if somebody did something? How do others know i did something? How do i get my updates from others? How do i push my updates out to others? Do we have the old version? What changed?
What choices exist? Distributor Source Control: Developers works on their own repositories and share changesets Git Darcs Arch Non-Distributed Source Control Developer work on local checkouts, and check in to a central repository Subversion
Please enter commit message
General Practices Essential INI Settings My Top Two PHP Security Practices
Set register_globals = Off
Set magic_quotes = Off  There are three php.ini settings that relate to magic_quotes:  ; Magic quotes ; ; Magic quotes for incoming GET/POST/Cookie data. magic_quotes_gpc = Off ; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc. magic_quotes_runtime = Off ; Use Sybase-style magic quotes (escape ' with '' instead of \'). magic_quotes_sybase = Off Example:- “This is my code’s string” gets converted to “This is my code\’s string”
Set error_reporting = E_ALL | E_STRICT STRICT messages will help you to use the latest and greatest suggested method of coding, for example warn you about using deprecated functions.  Available since PHP 5.0 Production: display_errors = Off log_errors = on error_log = path/logs/php_error.log
Set short_open_tag = 0   If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version=&quot;1.0&quot;?>'; ?> Safe to use <?php ?> tag Might be deprecated, But no news yet on php.net Good practice is to use <?php ?> tag
No direct access to the php.ini Use htaccess directive: php_flag php_flag is reserved for boolean values, like register_globals and magic_quotes_gpc. example:- php_flag register_globals Off   php_value  php_value for things that are not boolean, like error_reporting and error_log. example:- php_value error_log /var/www/logs/php_errors.log
My Top Two PHP Security   Practices Top Two PHP Security Practices, expressed in four words: Filter input Escape output -  Chris Shiflett
Filter Input Don't trust external data, The rule #1 of every developer Should be &quot;Filter All Foreign Data&quot; With the delivery of PHP 5.2.0, this got a lot easier, because PHP included, by default, the Filter library.  Manual -  http:// www.php.net /filter Downloads -  http://pecl.php.net/get/filter Filter homepage -  http://pecl.php.net/filter
Filter library examples $email   =  filter_input(INPUT_POST, 'name', FILTER_VALIDATE_EMAIL); $age     =  filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT); $url     =  filter_input(INPUT_COOKIE, 'url', FILTER_VALIDATE_URL);  $raw_msg = filter_input(INPUT_POST, 'msg', FILTER_UNSAFE_RAW);  $options =  array('options'=> array('min_range'=>7, 'max_range'=>77)); $age  = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT,$options); filter_has_var(INPUT_POST, 'submit')  is same as  isset($_POST['submit'])
With properly filtered input, you're already pretty well protected against malicious attacks.  The only remaining step is to escape it such that the format of the input doesn't accidentally interfere with the format of the SQL statement.  INSERT INTO MyTable (MyColumn) VALUES ('My Dear Aunt Sally's Picnic Basket')   Escaping Output
Escaping Output Use dedicated escaping function provided by the database  interface: MySQL mysql_real_escape_string() PostgreSQL pg_escape_string() pg_escape_bytea() SQLite sqlite_escape_string() Other databases ADOdb, qstr function -  http://adodb.sourceforge.net/ PEAR, quote function -  http://pear.php.net/ http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string
Questions? Thanks for your attention
Contact Slides will be on slideshare http://slideshare.net/ansarahmed Contact options Email:ansarahmed8@gmail.com/ansarahmed_8@yahoo.co.in Blog:  http://ansarahmed.blogspot.com Follow me on twitter: @ansarahmed @phpbangalore

More Related Content

What's hot (14)

Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Php ppt
Php pptPhp ppt
Php ppt
Sanmuga Nathan
 
Php intro
Php introPhp intro
Php intro
Rajesh Jha
 
Php mysql
Php mysqlPhp mysql
Php mysql
Shehrevar Davierwala
 
C:\Users\User\Desktop\Eclipse Infocenter
C:\Users\User\Desktop\Eclipse InfocenterC:\Users\User\Desktop\Eclipse Infocenter
C:\Users\User\Desktop\Eclipse Infocenter
Suite Solutions
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
Arjun Shanka
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
Nidhi mishra
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
pratik tambekar
 
PDF Localization
PDF  LocalizationPDF  Localization
PDF Localization
Suite Solutions
 
Php
PhpPhp
Php
Shyam Khant
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
S Bharadwaj
 
PHP
PHPPHP
PHP
sometech
 

Viewers also liked (8)

PHP: Best Mailing Practices
PHP: Best Mailing PracticesPHP: Best Mailing Practices
PHP: Best Mailing Practices
webhostingguy
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
Best Practices in Component Development for MODX
Best Practices in Component Development for MODXBest Practices in Component Development for MODX
Best Practices in Component Development for MODX
Jan Tezner
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
Patrick Allaert
 
Best Practices in PHP Application Deployment
Best Practices in PHP Application DeploymentBest Practices in PHP Application Deployment
Best Practices in PHP Application Deployment
Shahar Evron
 
Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
Hitesh Patel
 
TYBSC IT SEM 6 PROJECT MANAGEMENT NOTES
TYBSC IT SEM 6 PROJECT MANAGEMENT NOTESTYBSC IT SEM 6 PROJECT MANAGEMENT NOTES
TYBSC IT SEM 6 PROJECT MANAGEMENT NOTES
WE-IT TUTORIALS
 
PHP Project PPT
PHP Project PPTPHP Project PPT
PHP Project PPT
Pankil Agrawal
 
PHP: Best Mailing Practices
PHP: Best Mailing PracticesPHP: Best Mailing Practices
PHP: Best Mailing Practices
webhostingguy
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
Best Practices in Component Development for MODX
Best Practices in Component Development for MODXBest Practices in Component Development for MODX
Best Practices in Component Development for MODX
Jan Tezner
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
Patrick Allaert
 
Best Practices in PHP Application Deployment
Best Practices in PHP Application DeploymentBest Practices in PHP Application Deployment
Best Practices in PHP Application Deployment
Shahar Evron
 
Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
Hitesh Patel
 
TYBSC IT SEM 6 PROJECT MANAGEMENT NOTES
TYBSC IT SEM 6 PROJECT MANAGEMENT NOTESTYBSC IT SEM 6 PROJECT MANAGEMENT NOTES
TYBSC IT SEM 6 PROJECT MANAGEMENT NOTES
WE-IT TUTORIALS
 
Ad

Similar to Php Best Practices (20)

Php
PhpPhp
Php
Rathan Raj
 
Php Ppt
Php PptPhp Ppt
Php Ppt
Hema Prasanth
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Steven Pignataro
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
Alokin Software Pvt Ltd
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfony
Francois Zaninotto
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
Steve Mortiboy
 
Php
PhpPhp
Php
Vineet Vats
 
Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with Python
Brian Lyttle
 
Xdebug
XdebugXdebug
Xdebug
Bryce Embry
 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdf
ShaimaaMohamedGalal
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
Muhamad Al Imran
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php documentor
Php documentorPhp documentor
Php documentor
Tricode (part of Dept)
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Steven Pignataro
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfony
Francois Zaninotto
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
Steve Mortiboy
 
Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with Python
Brian Lyttle
 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdf
ShaimaaMohamedGalal
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
justmeanscsr
 
Ad

Recently uploaded (20)

Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 

Php Best Practices

  • 1. PHP Best Practices Bangalore PHP Users Meetup 31 st October 2009 http://www.meetup.com/Bangalore-PHP-Users
  • 2. Overview About this talk Coding Standard Documentation Sub Version General Practices
  • 3. About this talk Common good practises for coding PHP Tips for clean PHP code How to avoid common mistakes Tricks and Tips Tools to ease your work
  • 4. Use a Coding Standard
  • 5. Why use coding standard? Consistency Readability Maintainability Collaboration
  • 7. Learn from others Don’t invent your own standard. All the issue has been debated to death. Use an established standard Stick to an standard you establish, don’t mix
  • 8. What choices exist? PEAR Coding Standards http://pear.php.net/manual/en/standards.php Zend Framework Coding Standards http://framework.zend.com/manual/en/coding-standard.html eZcomponents Coding Standards http://ez.no/products/ez_publish/documentation/development/standards/php
  • 9. Some Zend Framework standards Derived from PEAR standards One class, one file Underscore in class name map to directory separators: Zend_Controller_Action: Zend/Controller/Action.php
  • 10. Some Zend Framework standards Naming conventions: Class name are MixedCase – Zend_Pdf Method name are camelCase - filterInput() Constants are ALL_CAPS – SET_TIME Properties and variables are camelCase Private and protected member are _underscorePrefixed
  • 11. Some Zend Framework standards Layout Conventions: No closing ?> tag for files containing only code Indentation: spaces only, no tabs;4 spaces per level of indentation No shell style comments(#) Keep lines no more than 75-80 characters long
  • 13. Any tool to check coding standards? PHP_CodeSniffer is one such tool: PHP_CodeSniffer is a PHP5 script that tokenises and &quot;sniffs&quot; PHP, JavaScript and CSS files to detect violations of a defined coding standard. Your own coding standards. Subversion integration http://pear.php.net/manual/en/package.php.php-codesniffer.php
  • 14. PHP_CodeSniffer Example Default uses PEAR style coding standard
  • 17. Documentation Documentation is the most boring work Don't have time!
  • 18. Documentation You don’t have time to code? Re-read your code 6 month after you wrote it! Think about people who have to use your code Code should communicate its purpose The better the names, the fewer comments.
  • 19. What choices exist? Source Documentation phpDocumentor http://phpdoc.org Doxygen http:// www.stack.nl/~dimitri/doxygen / End User Documentation DocBook http://www.docbook.org/
  • 20. Documentation phpDocumentor Derived from Javadoc, written in PHP. phpDocumentor tags are the most used standard for generating documentation from php source code Other documentation generators, such as Doxygen, support these same tags. Don’t invent your own tags. Supported by a number of different IDEs. Zend Studio is perhaps the most prevalent. Command line or web interface. Not only HTML, but also .chm or PDF
  • 26. Why do I need it? How do i know if somebody did something? How do others know i did something? How do i get my updates from others? How do i push my updates out to others? Do we have the old version? What changed?
  • 27. What choices exist? Distributor Source Control: Developers works on their own repositories and share changesets Git Darcs Arch Non-Distributed Source Control Developer work on local checkouts, and check in to a central repository Subversion
  • 29. General Practices Essential INI Settings My Top Two PHP Security Practices
  • 31. Set magic_quotes = Off There are three php.ini settings that relate to magic_quotes: ; Magic quotes ; ; Magic quotes for incoming GET/POST/Cookie data. magic_quotes_gpc = Off ; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc. magic_quotes_runtime = Off ; Use Sybase-style magic quotes (escape ' with '' instead of \'). magic_quotes_sybase = Off Example:- “This is my code’s string” gets converted to “This is my code\’s string”
  • 32. Set error_reporting = E_ALL | E_STRICT STRICT messages will help you to use the latest and greatest suggested method of coding, for example warn you about using deprecated functions. Available since PHP 5.0 Production: display_errors = Off log_errors = on error_log = path/logs/php_error.log
  • 33. Set short_open_tag = 0 If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version=&quot;1.0&quot;?>'; ?> Safe to use <?php ?> tag Might be deprecated, But no news yet on php.net Good practice is to use <?php ?> tag
  • 34. No direct access to the php.ini Use htaccess directive: php_flag php_flag is reserved for boolean values, like register_globals and magic_quotes_gpc. example:- php_flag register_globals Off php_value php_value for things that are not boolean, like error_reporting and error_log. example:- php_value error_log /var/www/logs/php_errors.log
  • 35. My Top Two PHP Security Practices Top Two PHP Security Practices, expressed in four words: Filter input Escape output - Chris Shiflett
  • 36. Filter Input Don't trust external data, The rule #1 of every developer Should be &quot;Filter All Foreign Data&quot; With the delivery of PHP 5.2.0, this got a lot easier, because PHP included, by default, the Filter library. Manual - http:// www.php.net /filter Downloads - http://pecl.php.net/get/filter Filter homepage - http://pecl.php.net/filter
  • 37. Filter library examples $email   =  filter_input(INPUT_POST, 'name', FILTER_VALIDATE_EMAIL); $age     =  filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT); $url     =  filter_input(INPUT_COOKIE, 'url', FILTER_VALIDATE_URL);  $raw_msg = filter_input(INPUT_POST, 'msg', FILTER_UNSAFE_RAW); $options = array('options'=> array('min_range'=>7, 'max_range'=>77)); $age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT,$options); filter_has_var(INPUT_POST, 'submit') is same as isset($_POST['submit'])
  • 38. With properly filtered input, you're already pretty well protected against malicious attacks. The only remaining step is to escape it such that the format of the input doesn't accidentally interfere with the format of the SQL statement. INSERT INTO MyTable (MyColumn) VALUES ('My Dear Aunt Sally's Picnic Basket') Escaping Output
  • 39. Escaping Output Use dedicated escaping function provided by the database interface: MySQL mysql_real_escape_string() PostgreSQL pg_escape_string() pg_escape_bytea() SQLite sqlite_escape_string() Other databases ADOdb, qstr function - http://adodb.sourceforge.net/ PEAR, quote function - http://pear.php.net/ http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string
  • 40. Questions? Thanks for your attention
  • 41. Contact Slides will be on slideshare http://slideshare.net/ansarahmed Contact options Email:[email protected]/[email protected] Blog: http://ansarahmed.blogspot.com Follow me on twitter: @ansarahmed @phpbangalore