PHP: Shorthand IF – ELSE Statement

The IF – ELSE statement in PHP has a shorthand version if you are comparing two values. A conditional operator “?:” (or ternary) operator is used to shorten this statement down to one line. The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE. In variable example:

$variable = (statement) ? "return if true" : "return if false";

Example below:

// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

The above is identical to this if/else statement

if (empty($_POST['action']))
 $action = 'default';
 $action = $_POST['action'];

Seeing as this statement is one of the most used statement while programming, it is a handy one to remember!

Counting Distinct Domains in Email List

I found this MySQL query that counts the number of emails that have the same domain name in a table. This is handy when you want to check which domain, in your list of emails in a table in the database,  is the most popular or even the top group of domains. My first guess for the list I wanted to check was that,, and would be the top four, and guess what… they were.

I editted the query a little to work on the latest version of MySQL:

SELECT DISTINCT (RIGHT(LCASE(email), LENGTH(email) – INSTR(email, ‘@’))) AS domain, COUNT(email) AS number FROM member
GROUP BY (RIGHT(LCASE(email), LENGTH(email) – INSTR(email, ‘@’)))
HAVING (((COUNT(email)) > 1))

White Hat vs Black Hat SEO

... just dug up an old article from a while back. Thought it was still helpful...

cowboysWhat is white hat and black hat SEO and where do we draw the line between them? In short, white hat is used to describe techniques that are with in the guidelines of search engines and black hat (also known as spamdexing) those that use all the unethical ways to achieve top ranks (usually short-lived).

Matt’s description: “White hat SEOs adhere to the letter of the search engine guidelines (GoogleYahooBing) and black hat SEOs will use any method they can to promote their site while trying to avoid getting banned. Gray hats are somewhere in between these two extremes.” – Matt Cutts

Well, Matt has actually answered both questions. There are a couple of gray areas between these two forms of SEO and therefore there isn’t a definite border between the two.

It is easy to describe what white hat SEO, but I thought that it would be of help for some of you if I listed black hat activities. Not that I want you to adopt them for yourself, of course, but that you should consider steering clear from them. Such actions could possibly get your site banded from some search engines. I am not going to explain all the tricks in the black book as there are too many.

  • Keyword Stuffing - well this is basically what the name says. Inserting or hiding keywords in a page to increase the density of that keyword. Search engines are wising up to this trick and can tell when keywords are being injected into the page.
  • Meta Tag Stuffing - repeating unrelated keywords in the meta tags.
  • Scraper Sites - also known as “made for Adsense” sites. They basically scrape results/information of search engine or news sites for example. This automatically updated data provides fresh content for the site. These sites are always full of adverts; that is where this type of site got its name from.
  • Hidden Links - hiding links where they can’t be seen in order to increase traffic either by search engines or unexpected clicks.
  • Mirror Sites - multiple sites all containing the same content hoping that search engines may rank some of the keywords in the URL higher in more than one of the URLs.

Some others that I have not explained are URL redirects, cloaking, blog spamming, spam blogs (splogs), referrer log spamming, doorway pages, link farms and many more…

WOW, there seems to be more black hat tactics than ethical SEO methods. My advice to you is this: stick to the search engine’s guidelines, don’t tarnish you name with shifty SEO tricks and you will do just fine. What you should be aiming to accomplish is a site with quality content or something helpful that people will enjoy and recommend to friends. Keep it real!

Avoid Using Frames

“Frames allow an author to display multiple documents in a single window that is divided into rectangular subspaces called frames. Visual browsers allow these frames to be scrolled independently of each other, and links can be loaded in a frame without changing the content of other frames.

The HTML 4 frames model has significant flaws that make frames hated by many users. Frames should only be used with great care;…” – WDG

Frames pose a number of issues and reason for you not to use them in your site’s design/layout. I always preach against using frames unless it is absolutely necessary for anyone to use them. If you are going to use frames, make use to set the doctype correctly:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "">

This will declare the document to be HTML 4.01 Frameset.

Some of the main factors/reasons for not using frames are not all SEO related. The problems with frames are mostly related to accessibility and usability.

Any browsers that do not support frames will basically skip over the frame tags and the frames would not be displayed. There is a NOFRAMES element that allows the webmaster to specify content for these browsers that fail to show frames; this only means that he/she has to put in some extra effort.

On the search engine side, a cache version of the page that is in a frame does not store that fact that it was actually meant to be in a frame. When a visitor clicks on the link to the framed page, they go straight to the page and not to the parent page that includes the framed page. The visitor will them be viewing an incomplete version of the original page. There are scripts that can help to redirect the page to the parent page, but then again not all browsers will support the script.

Iframes are less complicated than using the above framesets, but they still suffer most of the same problems.

There are also CSS issues with frames. You cannot control the look and feel of a framed document with the CSS file of the main page. Some people have claimed that it is possible using JavaScript in the CSS, but I have not seen a working example yet. It just gets too complicated!

The bottom line is… avoid using frames! For company intranets there is no SEO concern and there will be cases where frames can be useful. Think carefully before using frames for websites; there is probably a better solution out there. Let us know if you have another solution…

The Difference Between SEM and SEO

I have had many ask me what the difference between Search Engine Marketing (SEM) and Search Engine Optimisation (SEO) is, so I thought I might throw a couple of definitions out there.

I am going to start with SEO, as it is a part of SEM. SEO is the act of optimising the HTML and other content of your website for relevant, targeted key phrases in order to attain higher natural listings than competing websites. SEO provides a cheaper long term solution for increased qualified traffic and generates customer inquiries that ultimately convert to sales.

Now SEM is broader than SEO. It includes SEO and other areas to improve a sites visibility in search engine results pages, like paid listings and paid inclusions.

You can think of SEM as more expensive and quite possibly more targeted, while SEO is free (not counting your time of course) and its purpose is to obtain better free search listings.

Personally, I would start with SEO and then move onto SEM.

PHP: If Number Negative Then Make Zero

In PHP, checking if a integer is negative and if it is then setting it to zero is easy, but I was looking for something shorter (and potentially faster) than:

if ($x < 0) $x = 0;

Well, this is a very quick check and reset, but there is a function max that does this too and it works with arrays too.

$x = max(0, $x); // $x will be set to 0 if it was less than 0

The max() function returns the number with the highest value of two specified numbers.

echo max(1, 3, 5, 6, 7); // 7
echo max(array(2, 4, 5)); // 5
echo max(0, 'hello'); // 0
echo max('hello', 0); // hello
echo max(-1, 'hello'); // hello

// With multiple arrays, max compares from left to right
// so in our example: 2 == 2, but 4 < 5
$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)

// If both an array and non-array are given, the array
// is always returned as it's seen as the largest
$val = max('string', array(2, 5, 7), 42); // array(2, 5, 7)

What is SEO?

Well I guess I should start at the beginning, for those of you who are starting out and want to learn exactly what SEO really is. To begin with, SEO is an acronym for Search Engine Optimisation, and what better place to obtain the perfect meaning for SEO other than Wikipedia: Search engine optimisation is a set of methods aimed at improving the ranking of a website in search engine listings.

So the main aim in SEO is to get your website to the top or as high as possible to the top of search engine listings. There are ethical and unethical methods of achieving this, but I believe in only using ethical ways and will focus on these only. Many search engines will penalise domains for “cheating”.

I will try my hardest to blog on SEO or tips on the topic, whether it be on site architecture, page content, or finding the suitable directories. My plan is to share as much as I can with you and who knows, maybe I can learn from you too!

Footnote: In some countries, for example the United States, the spelling is Search Engine Optimization. Google's definition of SEO is an abbreviation for "search engine optimizer".