Results 1 to 7 of 7

Thread: I wish to enhance my PHP, wanted to know how do we write good PHP codes?

  1. #1
    Join Date
    Mar 2012
    Posts
    41

    I wish to enhance my PHP, wanted to know how do we write good PHP codes?

    I am new to coding, just 2 years old and I hav just come across this stupendous language PHP. I am very raw at coding and I am not sure how to make good, rather better or should I say great codes in PHP. I am very much fascinated by the extent of usability php provides. I want to know more about PHP and some great tips as in, how do I go ahead with it?

  2. #2
    Join Date
    Apr 2009
    Posts
    569

    re: I wish to enhance my PHP, wanted to know how do we write good PHP codes?

    PHP is undoubtedly the best one available today. It gives ultimate solution of software development. It is highly secure and robust form of application development and it generally helps us yield the desired result in the best easiest and quickest way possible. It deals with X Forms, has features of Sessions, cookies and Http authentication. Php is capable of handling file uploads in an extremely efficient manner. These are just few details or rather features of PHP.
    What exactly do you want to know? You will have my guidance, advice and helping hand through out your journey with PHP. You can count on me for that

  3. #3
    Join Date
    Mar 2012
    Posts
    41

    re: I wish to enhance my PHP, wanted to know how do we write good PHP codes?

    Wow! It’s awesome to know so many things about the language I’ve just been introduced to. Seems like I have take a good decision. I was glad that I have your assistance in PHP. If you ask me what do I want to know about PHP, I can go on and on with questions. But currently, the most important thing that is needed is guidance to how to code in an ideal way in PHP. Some tips on that regard may help me.

  4. #4
    Join Date
    Mar 2010
    Posts
    145

    re: I wish to enhance my PHP, wanted to know how do we write good PHP codes?

    I have jotted down whatever I could think of. I would request my fellow-posters to please post any corrections or appendment you can think of. To be a very good programmer, your talent doesn’t lie in remembering functions and classes. On these things, any Tom, Dick or Harry can lay their hands on since they ar so easily accessible on internet these days. Then, who can be called a good programmer? A good programmer is no one but the one who is more efficient in solving problems logically and can deploy the task of creating a web based application in the most time efficient and cost efficient way possible and also, who’s code actually provides a huge platform for editing or extending the code as required.
    So, in short to be a good PHP programmer, you must be aware as to how to write good code with minimum effort and time used. Let me enlist how to do that:
    • The code has to be very short. A short code means placing things in a great way and using simpler solutions and keeping it available for reuse.
    • Code should be very easy to understand, maintain and extend. That is even if the original coder passes away, any junior programmer must be aware how to extend it
    • Code should be as light as possible. They should hardly occupy any place on the server and server overload should be carefully avoided

  5. #5
    Join Date
    May 2009
    Posts
    637

    re: I wish to enhance my PHP, wanted to know how do we write good PHP codes?

    You have explained it in an extremely excellent manner as to how a code should be. But let me put some lights on how such a code is to be written. Some tips on writing short, easy and light code.
    1. Use s Alternative syntax for control structures available in templates
    I would strongly recommend the use of templates at this amature stage. If you try fiddling around with the HTML output directly with your scripts you will have to work on this regard. If what you follow and understand is the concept of separating design from progam logic then you are already in the league and you are using aa template engine. Try making things more presentable by using alternate syntaxes like: foreach: / endforeach;, if: endif; for: endfor, etc
    2. Encapsulate everything possible:
    Never ever come to a stage where in you have to copy paste your own code. That is the worst thing to do. Being a coder you should be sensible enough to encapsulate maximum things possible. This is the basics of programming, but seldom anyone uses it. If any line or query in the code is used more that twice then it should be encapsulated in a method and if there’s some variation, there should always be a method which accepts argument. Encapsulation gives you the following benefits: it keeps the code too less and you don’t jave to write much then. With just a single change you can probably change all similar codes. The code becomes more inderstandable and clean
    These things actually improve the quality of code. I would explain it with an example, let me ask you one simple question. If you have to alter a thing like date which is used at multiple places and you suddenly get this enlightenment of change this date. What would you do? Sit and change all the date? If you would have created a function date_c() it would have been so easy
    3. Make use of database object:
    There are n-number of ways how you can probably do this which includes ODBC, PDO etc. you can put or do whatever with the code but please avoid the use of mysql_query() embedded directly into your code. I will seldom happen that you have to alter anything in the DB engine. It's again keeps the code short, readable and better.
    4. Use basic database functions:
    You should be efficiently making use of database functions like Creatr, Update, Delete. They call it CRUD it seems. You should be creating functions or methods which can work for you or use Active Records. Opt this over merely writing long SQL queries with I don’t know how many lines. This will save a lot of your time and get ready for other household works

  6. #6
    Join Date
    May 2009
    Posts
    511

    re: I wish to enhance my PHP, wanted to know how do we write good PHP codes?

    That was a commendable effort and an excellent piece of information, not only for the beginners but also some undisciplined adults. Even I would like to add certain stuffs relating to this. Just my part of help.
    - Debugging is extremely important:
    This would probably not make you write a great code but this tip is sure to make you a better programmer. If something doesn’t work in life, does it fix itself by thinking a lot about it or when you keep staring at it. None, right? The same is the problem with the codes, if you find a code you need to fix it. Not just sit and curse your destiny.
    Debugging is nothing but traveling reverse through th logic of your code till you find that place which is giving you the bug and is interrupting the working. Even if you use debugger or use print_r() and echo() at all suspicious places, it wont help. Reverse tracking is very important. I’ll explain how to go about it.
    o Firstly, check the current place for bug, if doesn’t give you bug you should go ahead
    o Move some two to three lines behind and execute the program, if the program still doesn’t operate then that piece o f code is fine.
    o Go more backward till you find a place from where the code is executed. That is the point where the error is possibly occurring.
    I am not trying to be rude or bold but this is the most basic quality required in any programmer. A programmer must be capable of performing these tasks. This will help you solve the toughest of problems that you might come across
    - Check your name:
    A code that uses proper variable names I said to be the best of codes. In such a code you can actually keep a track of all the happening in the program. So, a good variable name needn’t follow the Hungarian notation but it should actually correspond the task or category that the variable name belongs to. Using variable names like A, B, X, Y, $Arr etc is the most foolish thing to do. Imagine a new user using your code or rather you using it 2 years down the line you trying to extend it. You’ll be in unimaginable mess.
    - Avoid database queries:
    This is the basic of coding. But how many of us takes care of this actually. When was the last time you sat to clearing and improving your loops. how many of you all spends time in deleting not so important stuff? DB queries occupy a lot of place on server. It should be kept the lighter possible. Here are how do we reduce the use of this:
    o To avoid this, we should be using joins and left joins also, inner selects when joins and the left joins fail to work
    o We should also use some temporary tables. This helps a lot in the purpose.
    o Use collection when none of them work at all. Do not run one query at a time. Use of collection of queries will solve the issue of overloaded server

  7. #7
    Join Date
    Jun 2008
    Posts
    379

    Re: I wish to enhance my PHP, wanted to know how do we write good PHP codes?

    Let me introduce a more technical aspect of what is called as good coding. I have differentiated it into sub parts for better understanding.
    Always keep HTML, CSS, Javascript and PHP far away from each other. It does look attractive to mix some PHP codes into HTML but trust me it looks lame and nobody ever gets impressed by it, separate things to the maximum possible. Create different files and make the HTML accessible to PHP and vice-versa. Use functions and all as substitutes but not the entire code
     MVC can be considered as an awesome idea. Keep reading about it. Do not miss a dot of it’s development. It is always preferable to spate the codes. That’ll make things easier for you
     Nothing worse that magic quotes have happened in the programming world. It is nothing but a tool or rather a feature of PHP which automatically insert a \ before ‘ and” to make the SQL transactions safer. This was a total screwed up thing. Do not use it as its habituation will lead you to greater troubles
     PHP has brought about a brand new feature called ‘namespacing’ if you want to write a function named count(), hold on! PHP already has a count so it is not possible to work with it. Prefixing it is a very good way of using it though
     Be sure that your site is most of the times vulnerable to an XSS attack. I is called cross site scripting which simply states that someone else has played around with our script. Please make sure you are safe from that as such an attack does nothing but spoils the basic image of your website and reduces user’s trust over it
     You need to find a database abstraction layer in google. Download and install it as you can use it to sanitize your SQL as required. It also helps in datamigration incase of database corruption
     I don’t understand why people tend to overcover the fact that Javascript and PHP are not same but different. A site should be written solely with the help of HTML, CSS or PHP and javascript shold be acting as a cherry on the cake just to improve the existing site. It is impractical to assume that javascript is working fine and as expected since you know nothing about javascript
     Try and keep caching disabled in your AJAX library and PHP because, there is a common issue of caching with AJAX. Do not activate it unless you have a strong reason
     If a portable device is in your developer’s head do not ever try to use shell_exec(), or any other program that provokes an application to work with server. Unless and until necessary. no need of using shell script. You’ll be pleasantly surprised to see what can be done in from pure PHP cannot be done with anything else.
     Do not dare to use any other function without escapeshellarg(). It is a compulsion that I am giving you to to use it as a parameter you will be shocked to see the number of wbsites which can be wiped off easily due to stupid codes.
     File permissions is are extremely important to prevent against attacks. The second one is reserved for the owner of the file, the third is for the group file and the fourth one is for others.
     When you install and use a debugger, it takes hell lot of time but consider that as an investment since you’ll be saving much much more in the very first week. Xebug is one of the most reliable ones and I strongly recommend it. Komodo, netbeans, aptana and eclipse are also worth trying. They surely work great! I have tried
     Always turn ‘display errors’ and error_reporting() on and keep checking your logs. Incase you do have a shell access “tail -f /path/to/your.log” can be used to keep watch on your logs

Similar Threads

  1. Replies: 6
    Last Post: 21-02-2012, 04:15 PM
  2. Tips to write Good Article Headlines
    By Dhipin in forum Tips & Tweaks
    Replies: 9
    Last Post: 16-01-2012, 07:06 PM
  3. Replies: 7
    Last Post: 17-09-2011, 11:35 AM
  4. Wanted a good media player suggestion
    By AAID in forum Windows Software
    Replies: 4
    Last Post: 30-11-2010, 11:00 PM
  5. How to write HTML codes for pictures
    By softte in forum Software Development
    Replies: 5
    Last Post: 01-10-2010, 12:45 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,699,297.12322 seconds with 17 queries