Archive for September, 2009
PHP Shorthand IF – ELSE Statement
Posted by Chris in Tips & Tutorials on September 14th, 2009
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:
<?php
// 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';
} else {
$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
Posted by Chris in Tips & Tutorials on September 4th, 2009
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 aol.com, yahoo.com, hotmail.com and gmail.com 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)) ORDER BY number DESC;


