10 PHP Tips And Tricks For Beginners


PHP is a recursive acronym which stands for PHP: Hypertext Preprocessor. It is a server-side scripting language used to generate dynamic page content. It’s in the core code of social networks such as Facebook and content management systems such as WordPress. It’s free to download and use for a lot of different tasks, including building web content, evaluating browser data, sending and receiving cookies and even database communication. It’s widely used by professional programmers, however, it can be a bit overwhelming for beginners. That’s why we’ve prepared these 10 PHP tips and tricks aimed at newcomers and those wishing to learn something new.

1. Write it on a piece of paper

One of the best advice a beginner might get when it comes to PHP and other scripting languages is to get a piece of paper and make a wireframe for the project. This allows you to think through the mechanics of the application you’re working on before you actually start coding. Not only that but simple planning ahead such as this can help you isolate the problems and the most difficult aspects of the project beforehand, instead of realizing mid-project that the last couple of hours of work were simply not necessary.

2. Know what it is you’re supposed to be doing

One of the major and most recurring issues coders and web developers face is the prospect of pouring dozens of work hours into a project, only to be told by the client that that is not what they want. Although coders aren’t exactly known for their communication skills, having a clear understanding with the client about what the project is and how it’s supposed to be done is crucial. If you are unsure on what the client needs, how they need it and what it’s supposed to do, then don’t try to build it.

3. Turn the error reporting feature on

This is one of the very first things you should do before you start working a new project. Change the error reporting string to E_All and the only time you should turn it off would be around 10 seconds before you go into production mode. Every PHP developer should be using this feature, due to the fact that error reporting picks up every small error that has the potential to grow into a complete nightmare. And there is no better feeling when you can run an entire project in production mode without getting a single error.

4. Always use OOP

OOP or object oriented programming is a term which describes a programming method in which you use classes, or in this case objects, to decrease the use of repetitions in your code. They are basically classes which collect functions and bundle them together, making them easy to reuse them whenever you need to without actually having to rewrite any part of the actual code. In OOP, data classes, or objects, allow for easier defining of the subclasses of data objects which share some or all of the class characteristics. This greatly reduces the development time and allow for more precise coding experience.

5. Don’t forget to use functions

PHP has more than a thousand built-in functions, and it’s a real shame that a good portion of programmers doesn’t actually use them. A function represents a block of statements which can be used repeatedly thru ought the project. They don’t execute when the page loads, but rather when that specific function is called upon. Since functions are only executed when requested, you can place them at the beginning of your code with no issues. They’re not case sensitive, so you don’t have to worry about that while coding.

6. If you have to, do use a framework

Frameworks are used to streamline the development of applications by automating patterns and repetitive and common tasks often used for a specific purpose. It makes the programming work easier, by adding a structure to the code, which in turn allows for a more readable, maintainable and overall better code. Frameworks can also be used for database abstraction, form validation, input and output filtering, cookie and session handling and last but not least, an added layer of security. Not to mention that all that automation greatly speeds up the development process, which can be useful when working on a tight schedule.

7. Always protect the database

There a couple of ways you can do this, but one of the best solutions for database protection is to use the mysql_real_escape_string(). This function safeguards the strings from quotes or other functions which can be used to damage your database or inject it with a malicious code. Other ways of protecting your database include validating all the GET and POST strings, making sure that all the form data has the right value and type before being added to a database query.

8. Use an administration tool

Working with MySQL via the command line is tedious and simply put, inefficient. Knowing how you can administer your MySQL database using mysqladmin is good, but using a graphical interface such as phpMyAdmin can really speed up the database administration and overall development. Using phpMyAdmin, you can quickly create databases and their tables, export them into SQL files, optimize them, run SQL queries and even check for specific issues. Chances are, your web host probably has phpMyAdmin already installed, but if not, the installation process is pretty quick and straightforward.

9. Limit your scripts

Setting a specific time limit on PHP scripts is very important for every PHP developer. It’s only a matter of time before one of your scripts fails, and when this happens, the best course of action is to use the set_time_limit function in order to avoid the script going on an infinite loop and experiencing database connection timeouts. The set_time_limit function set a specific time limit on the time during which the script will run. The default time is set at 30 seconds and after that time has expired, the script is stopped with a fatal error.

10. Use the output buffering option

Without the use of output buffering, the HTML code is being sent to the browser in chunks while the PHP processes through the script. With output buffering, the HTML code is stored in a variable and the Brower receives it as a single piece located at the end of the script. This greatly improves the speed and overall performance of your PHP script. To enable this option, all you have to do is add the line ob_start() at the top of the file you’re working on.

The last thing you should consider is that you cannot become a good programmer just by reading. Practice makes perfect, and this is especially true in the programming business. Find a good code editor and start writing, but don’t just write code for the sake of writing, instead, try to build real things with your code. This is the single best way for an absolute beginner to learn both how to code, and how to debug the code once it becomes necessary.




Get Our Feeds Delivered To Your Inbox

Like This Post

  0 Like

AUTHOR INFO

SamCyrus

Sam Cyrus is CEO and co-founder of http://agseosydney.com.au/, a Digital Marketing agency from Sydney. Sam is also a creative writer and likes to share his insights on entrepreneurship, business, online marketing, SEO and social media. He currently collaborates with Nirmal, a Web Design Company from Sydney.

Latest posts by SamCyrus (see all)

Leave a Reply