Category Archives: Interview Questions

php, mysql, ajax, linux, apache, css, JavaScript, php frameworks

How to debug PHP code?

Debugging PHP code is challenging task  for all Php developers, however some simple php debugging techniques can help you to code faster and thus save very valuable coding time.

Enable Notices on  development servers!

A notice is a type of error message which is less severe than parse, fatal and warning error messages. A notice may tell you that you have used that variable without defining initialized it! Enabling notices helps you to do following things ….

  • Warns about un-initialized variables and non-existent array indexes.
  • Identifies deprecated behavior.
  • Various non-critical issues that could potentially cause problems.
# Inside PHP.ini
error_reporting = E_ALL

# Inside httpd.conf or .htacess
php_value error_reporting 2047

# Inside a script

If you want to test that notices are turned on you can simply create a file with the following:

Here I would like to remind you that you should always ensure that the display_errors ini setting is set to ‘Off’ for any live/production sites.
Part from that PHP 5.0  introduces E_STRICT, which is primarily used to indicate functionality that has been deprecated in PHP5.
PHP 5.0 Error Reporting

# Inside PHP.ini
error_reporting = E_ALL | E_STRICT

# Inside httpd.conf or .htacess
php_value error_reporting 4095

# Inside a script

Use a Logging System

A logging system can be very useful in tracking down bugs, especially when they happen in a production environment. Such a system can also be useful in debugging during development. Users rarely report errors and often may not even notice the erroneous behavior. So it is absolutely critical to log errors to ensure that is a record of the problem.

# Inside PHP.ini
error_log = /home/vhost/logs/php_error_log
display_errors = Off
log_errors_max_len = 0

As for all the other errors, we need to make sure that they are caught and dealt with properly. Make sure the user is shown a nice error page (with a suitably cute ‘oops-back-soon’ picture) and then log, log everything in sight!
The stack current trace (see debug_backtrace() and debug_print_backtrace()).

  • The output of get_defined_vars(). However, this is only useful if you call it at the point the error occurs, not at the point the error is logged. This includes global variables.
  • Any and all information about the remote user (IP address, user agent, session data)
  • All global variable data (which includes the contents of $_COOKIES, $_SERVER etc.)
  • Any other status data which is specific to your application

Use an Integrated Development Environment and Debugger

Use a editor with inbuilt live Php debug (Like Zend Studio, phped ,Phpdesigner, eclipse etc), Editors like editpad, notepad doesn’t support live debugging and thus need to run the php file on browser for finding the errors. These editors run php scripts via command line on every save and present any errors found.

I also use a remote debugger (ZendDebugger), which ties into the IDE. The remote debugger is a PHP module that allows you to debug code on your server using the IDE on your local machine. You can set breakpoints, inspect variables, examine stack traces, profile code and all the other benefits you would expect from a debugger. And no, Zend does not sponsor me.

Use Xdebug

The Xdebug extension allows you to find stack traces and function traces in error messages, memory allocation and protect from infinite recursions happening
It also provides profiling information for PHP code and is having the capability to debug scripts interactively with a debug client.

Use PHP Frameworks

Php frameworks like Zend, codeignitor and cakephp provides a lot of functionality for setting up test cases, units and debugging. Most of the code is ran via libraries the error messages are nicely crafted and provides inbuilt details.

Unit Testing

Unit testing may not be everyone’s idea of fun, but it I is very good for developing larger projects. It can give you confidence that code is fine, as well as point out problems before your code goes into production.

Error Tracking with magic constants

PHP has four magic constants, _FILE_, _LINE_, _FUNCTION_ and _CLASS_ that can be used to backtrack to the source of the problem.

Share

How to remove duplicate entries from a table ?

Removing Duplicate entries from Mysql database table are really simple. I have found some times interviewer ask tricky questions like this. PHP-MYSQL Beginners  just be careful !

It can all be done with three manual SQL steps! No PHP Coding required, and it works with MySQL 4 and above versions.

Step One

CREATE TABLE MyNewTable as
SELECT * FROM MyOldTable WHERE 1 GROUP BY [column to remove duplicates by];

Step Two – You hate to keep old table with all the duplicate entries, so drop it!

DROP TABLE MyOldTable;
RENAME TABLE MyNewTable TO MyOldTable;
Share

What is diffrence between echo and print?

Print method can return a true/false value. This may be helpful during a script execution of somesort. If you need to output data through a function, you can use print()
instead

Echo does not return a value, but has been considered as a faster executed command. echo is not a function and, as such, it does not have a return value.
echo 30;
print (30);

Share