Tuesday, September 14, 2010

Tips for PHP Beginners

Here, I am discussing about the common tips & traps of PHP Programming. I took many interviews & found that most of the interviewee having more than 3 year of experience don't have knowledge of even basics. I am here trying to clarify some issues:
  1. Difference between require & require_once
    require is used to include a file in a given file path & it halts the script if specified file is missing. require_once is same as require with one addition, PHP now checks if a specified file has already been included, if so, file will not be included again.
  2. Difference between include & include_once
    Both include & include_once has similar behaviour as require & require_once with one exception i.e. these both emits warning & don't halt execution of script.
  3. Is Ajax Server Side Scripting Language or Client Side?
    Ajax is definitely client side scripting language because we use Ajax Javascript to communicate with the server side PHP Script. We can't do anything just like database manipulations or other server side things using Ajax. We can only pass a request to a url/script & that script returns a response, on the basis of that response, We process on client side browser.
  4. Meaning of display_error and error_reporting constructs?
    Construct error_reporting is used for interpreter to tell which type of errors should be reported and display_error is used to decide if error to be displayed as a output of script/on-screen. We can also has option to log errors if we don't want to display the errors on screen but want to check.(useful for production environment)


  5. Difference between method overloading & method overriding?
    Method overloading feature in any OOPs language provide the functionality of having two or more methods with same name but different type signature in the same class or inherited class. But, Overriding is the functionality to override the parent class method of same name & signature in derived class.  Overriding can't be done in same class. So, to duplicate a method with same name & signature but different definitions, we use method overriding.

  6. Session Vs. Cookies
    Actually, Why we need both or either? These both construct enable you to share data on very next scripts without using database as a temporary memory.
    Session is a server side technique. In this technique, Server allocates some memory space(i.e. in the form of files or database) and save some data in that space.
    To maintain uniqueness, server assign a unique key to every session space & store that key/id to the client in the form of cookie. But, if cookie is disabled by the browser then, we need to pass the session id in the URL to tell the server, for which session space, browser is asking for. But, if cookie is enabled, browser does this job behind the scene. We don't need to pass the session id in the URL if cookie is enabled.

    Cookie is a file that can have some data on client side browser. Every cookie has a life time which we can set at the time of creation of cookie.

    Conclusion: Cookie can have some insecure data & is limited due to client side restrictions but, session don't have any limitation of data size & can contain secure data i.e. login authentication. Cookies might be enabled/disabled but we can enable session inside our PHP Script as we needed. So, If you are using cookies, you need special care to handle them. Some one might corrupt them or want to hack something if you are taking some useful values from cookies in your script.