Archive for category Tips & Tutorials

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.
<?php
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)

, , ,

No Comments

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:

<?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!

,

No Comments

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 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;

, ,

No Comments

Twitter for Business

As Twitter becomes more and more popular, more businesses are using it as a marketing tool. This social marketing tool can potentially reach millions of people… depending on the topic of course. Twitter recently starting culling spamming accounts, so make sure that you are not spamming but rather providing interest and informative info.

Twitter has just launched a Twitter 101 guide for business. The guide includes case studies, a lingo list, tips and a list of other useful resources.

“So what does Twitter do for businesses?

Twitter is a communications platform that helps businesses and their customers do a number of useful things. As a business, you can use it to quickly share information with people interested in your company, gather real-time market intelligence and feedback, and build relationships with customers, partners and other people who care about your company. As an individual user, you can use Twitter to tell a company (or anyone else) that you’ve had a great–or disappointing–experience with their business, offer product ideas, and learn about great offers.” – Twitter Guide 101

Out of all the useful Twitter apps, the one that I like the most (and I don’t have an iPhone) is TweetDeck! A must try as it also intergrates with Facebook status updates!

Hit me (or follow me) on http://twitter.com/chrisburd … expecting some comments on my surname being Burd! :)

, ,

No Comments

Browser Screenshots

Now and then you may have a client that users a combination of OS and browser that you cannot test a site design on. I found this great resource that provides screenshots from browsers on different platforms.

Browsershots makes screenshots of your web design in different browsers. It is a free open-source online service created by Johann C. Rocholl. When you submit your web address, it will be added to the job queue. A number of distributed computers will open your website in their browser. Then they will make screenshots and upload them to the central server here. – Browsershots Site

This save having to get a Mac and a Linux box just to test all the browser versions and how they render the site. Of course this doesn’t allow you to actually test how the site operates on these browser, but at least you can see if something is out of place.

, ,

No Comments