CSS !important Declarations

Recently I came across lot of code using CSS !important declaration. I had known that this declaration existed but I didn’t knew what it was and had never used it. So than I did my research on it and decided to write a post about it. In this article I will discuss what it is, how it is used, when to use it, etc

What is !important?

The !important is a key word which is added to the end of the CSS value to give it more weight. In CSS the rules are applied to elements in a cascading manner.

  1. Find all the declarations that apply to the element in question.
  2. The order in which the styles are applied based on importance and origin. The weight of style sheets are, from least weight to highest weight, first user agent styles, than user, than author style sheets, !important author styles and the height weight is for the !important user styles.
  3. The more specific a selector is, the higher weight it will get. e.g., a style on “div.main p” will have a higher precedence than one just on the “p” tag.
  4. The rules work in top to bottom order. Rules that are defined later in the document tree have higher precedence than those defined earlier. Also, if rules are imported from style sheet then they are considered before the rules which are directly in the style sheet.

The !important is used to force the style to an element and not be overridden by the cascading CSS. When the !important is used that rule gets more weight than it naturally has.

Read more »

PHP double quotes vs single quotes

Strings in PHP can be specified in four different ways: single quoted, double quoted, heredoc syntax and (since PHP 5.3.0) nowdoc syntax, the first two of them being by far the most frequently used.

It is important to know the difference between using single quotes and double quotes. In this post we will see the difference between them and which should be used when.

Read more »

Sticky Header and Footer using CSS

In some sites we want to keep the Header and Footer always visible and fixed to top and bottom of the screen respectively. In this article I will show how we can implement the sticky (or fixed) header and footer using only HTML and CSS.

To create a layout with sticky header and footer can be easily done using the CSS property position:fixed. In my example, I will be displaying a sticky header and a sticky footer.

Read more »

Get search query string from search engines using PHP

I have often come across situations when I would like to implement some functionality based on what the user searched for in a search engine like Google, Bing etc.

The search query string is normally passed as GET variables ‘q’ or ‘query’. The function below will return the search query string.

// Function returns the query string (q or query parameters) from the referrer
function get_search_query()
{
	$ref_keywords = '';
 
	// Get the referrer to the page
	$referrer = $_SERVER['HTTP_REFERER'];
	if (!empty($referrer))
	{
		//Parse the referrer URL
		$parts_url = parse_url($referrer);

		// Check if a query string exists
		$query = isset($parts_url['query']) ? $parts_url['query'] : '';
		if($query)
		{
			// Convert the query string into array
			parse_str($query, $parts_query);
			// Check if the parameters 'q' or 'query' exists, and if exists that is our search query terms.
			$ref_keywords = isset($parts_query['q']) ? $parts_query['q'] : (isset($parts_query['query']) ? $parts_query['query'] : '' );
		}
	}
	return $ref_keywords;
}

Note: Google has recently stopped sending the search query terms for all logged-in users. So, if a user is logged-in Google and searches for something and then follow a link to your site, you won’t be able to get the query terms that they searched for. The Making search more secure blog post by Google, explains this is more details.

PHP isset() vs empty() vs is_null()

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

Read more »