Category Archives: Coding & Programming

OOPS: Classes in php

OOPS Concept: Introducing Classes in php


You all know that Classes are the most important part of OOP.


A class is simply a template or blueprint that contains the various attributes and functions of an object of the class.
Class is mainly composed of three things :  name, attributes, and operations.We can also say that class is a composite data type same as we have used struct in C and C++.


Class would be the type, and an object would be the variable. eg.


Consider a class Car.
A car class will have the following attributes:

1. Color
2. Engine
3. Tyres
4. Headlights
5. Seats
6. Steering

It will also have the following function/operations
1. Power Brake
2. Blow Horn
3. Pickup and Drive etc


A class gives you only the attributes and function.

Lets think in general there are so many company manufacture the car,they can increase the function and attributes. After putting all these in a real world then a car make up,then it’s called object(real entity),the base or template or blueprint is car,so car is a class.


In real world, you’ll often find many individual objects all of the same kind. As an example, there may be thousands of other bikes, all of the same make and model. Each bike has built from the same blueprint. In object-oriented terms, we say that the bike is an instance of the class of objects known as bikes.


Delete all duplicate rows except for one in MySQL

Delete all duplicate rows except for one in MySql having no primary key.

Someone asked me to delete all rows from a table except one entry with a single query.
This is not so easy for me :), Anyway after work and research I have found the solution, I am sharing with you How you can do the same.

Let’s take an example of table tbl_employee have duplicate names data as follows


SELECT * FROM tbl_employee;
id    name
1     mahesh
2     suresh
3     john
4     john
5     john
6     mahesh


If you want to keep the row with the lowest id value then

DELETE e1 FROM tbl_employee e1, tbl_employee e2 WHERE > AND =

Or with highest id value

DELETE e1 FROM tbl_employee e1, tbl_employee e2 WHERE < AND =

Use non-standard font in web pages

Use non-standard font in web pages


Normally your website visitors can only see the fonts that are already installed on their system.

So if you use a font(non-standard) that is not installed on site visitor’s computer then his browser will show some other font that is on their computer. That’s why when you are defining a font for an element (such as <span>,<div>) you often specify multiple fonts so that if your preferred font is not available, then CSS(Cascading Style Sheets) file should use the available alternatives.


font-family: verdana, sans-serif;
Here verdana is preferred and sans-serif is alternative.


This can be really annoying if you want to use a nice font in your website.


Conventional way of using custom fonts for headings and logos creating the text in a graphic editor and then using the image file.From the perspective of SEO this is not appropriate solution,you must use text as much as possible.


Now there is a way available in CSS that lets you use custom fonts, downloadable fonts on your website. You can download the font of your preference, let’s say copse_font.ttf, and upload it to your server where your website is hosted.


Then from within your CSS file (or wherever you are defining your styles) you have to refer to that custom font in the following manner.


@font-face {
font-family: copse_font;
src: url(‘copse_font.ttf’);


font-family: copse_font; /* no .ttf */


You can get some free fonts file from here.



There is another option to use custom fonts in your site is via Google API. However, not so many fonts available so far in Google Font Directory.



You can go there by the link below.
Google Web Fonts API


PHP: Access control modifiers

PHP: Access control modifiers


One of the fundamental principles in OOP(Object Oriented Programming) is ‘encapsulation‘. The idea is that you create cleaner and better code, if you restrict access to the data structures (properties) and methods in your objects.


The access modifier is the most important feature of the OOP language and it is used to restrict the function and classes to be accessed. In PHP, there are some keywords representing these access control modifiers. These keywords will be added with PHP classes and its members (methods and variables).


You can restrict access to class properties using ‘access modifiers’. There are mainly 3 access modifiers:
– public
– private
– protected


Public: This variable or method can be used from anywhere in the script.This is the default access modifier.Properties declared as ‘public’ have no access restrictions, meaning anyone can access them.

Note: When you declare a property with the ‘var’ keyword, it is considered ‘public’.


Private: When you declare a property as ‘private’, only the same class can access the property i.e variable or function can only be used by the object it is part of; it cannot be accessed elsewhere


Protected: When a property is declared ‘protected’, only the same class and classes derived from that class can access the property(this can be done using inheritance). We can say variable or function can only be used by the object it is part of, or descendents of that class.


There are some other special keywords you can place before a class, a class function definition, or a class variable to alter the way PHP works with it – here’s are the list, along with what each of them does..


Final: This variable or function cannot be overridden in inherited classes.


Abstract: This function or class cannot be used directly – you must inherit from them first.


Difference between htmlspecialchars and htmlentities

What is the difference between htmlspecialchars and htmlentities PHP functions?



This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.


Some characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made.If you require all HTML character entities to be translated, use htmlentities() instead.


1. htmlspecialchars may be used when there is no need to encode all characters which have their HTML equivalents.


2. htmlspecialchars only takes care of <, >, single quote ‘, double quote ” and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML.


In Short, you can say


htmlspecialchars(): Convert some special characters to HTML entities (Only the most widely used)

htmlentities(): Convert ALL special characters to HTML entities



PHP: Difference between cookies and session

What is the difference between cookies and session?


HTTP is a stateless protocol.
That mean, that treats each request as an independent transaction that is unrelated to any previous request. So, how about the request we want to make frequently, like username or id? As you know, we could store our data in COOKIE. When we store data in COOKIE, the browser will send the cookie data to server for each request. We already could use SESSION for this kind of task. So, what is difference between SESSION and COOKIE?


A cookie is a text-only string that takes a place in the memory of user’s browser. If the lifetime of the cookie is set to be longer than the time user spends at that site, then this string is saved to file for future reference. User could be disabled the cookie in their browser setting.


A session is an object associated with a client connection to the server. it has the ability to carry information related to the client,session values are store in server side not in user’s machine. A session is available as long as the browser is opened. User couldn’t be disabled the session. We could store not only strings but also objects in session.

Session files are deleted automatically by php according to garbage collection settings.


1.The main difference between cookies and sessions is that cookies are stored in the user’s browser(hard disk), and sessions are not,cookies are browser dependent and sessions are not dependent on client’s browser settings


2.A cookie can keep information in the user’s browser until deleted. But Session work instead like a token allowing access and passing information while the user has their browser open.


3.The difference between sessions and cookies is that a session can hold multiple variables or objects, and you don’t have to set cookies for every variable. By default, the session data is stored in a cookie with an expiry date of zero, which means that the session only remains active as long as the browser. When you close the browser, all the stored information is lost. You can modify this behavior by changing the “session.cookie_lifetime” setting in “php.ini” from zero to whatever you want the cookie lifetime to be.


You can get the best! Once you know what each does, you can use a combination of cookies and sessions to make your site work exactly the way you want it to do.