Before you ob_start(), don’t forget ob_flush() and ob_end_clean()…

Before you ob_start(), don’t forget ob_flush() and ob_end_clean()…

Adding compression to your PHP scripts is as easy as:

// start at the beginning of your output.
output will be here, whatever it is.
// and after your output.

No, really. It is that easy.

By Mike on September 9, 2011 | PHP, Tutorials
Tags: , , ,

PHP Tutorial: Ternary Logic, (?:) shorthand if/else

So you may have been looking through a PHP script you downloaded and seen a bit of logic that you didn’t even know was possible. What you probably saw was Ternary Logic, it had a question mark and a colon (?:) in use, making it more confusing for you. Well not any more, here comes the clarifier. The easiest way to do this is take a long hand if/else and then create a ternary logic statement to match it. So let’s try that.

/* we'll set a $gender variable, typically you will already
 have your variable set elsewhere */
$gender = 'male';
/* so we'll create a basic if/else */
if ( $gender == 'male' ) {
    $title = 'Mr';
} else {
    $title = 'Mrs';
echo $title;
/* that makes sense right? */
/* we can shorten that easily, using ternary logic */
$title = ($gender == 'male' ? 'Mr' : 'Mrs');
echo $title;
/* Also instead of putting our result into a variable, we can just
as easily put it in-line with our code, if we want to */
echo  "Hello " .($gender == 'male' ? 'Mr' : 'Mrs'). ". Smith, how are you?";
/* in our case this will output: "Hello Mr. smith, how are you?" */

So as you can see the ternary logic form, if you were to read it as english, would read something like “$title gets the value of ‘Mr’ if $gender is equal to male, and ‘Mrs’ if $gender is not male.”

“Ok, that’s fine, but what if I want to return a true or false value?” Well that’s even easier! Let’s try it:

/* explicitly tell PHP to return a true or false */
$result = (1+1==2 ? true : false);
/* or imply that you want a true or false like so: */
$result = (1+1==2);
echo ($result ? '$result is true' : '$result is false');

The above second example $result will set $result to have the return value of the expression (1+1==2) which, as far as I know is true.

And there we go, a very basic example of ternary logic, practice makes you better, so practice lots. I hope you found this helpful.

By Mike on April 1, 2011 | PHP, Tutorials
Tags: , , , ,

PHP Tutorial: Arrays mapping your mind in PHP

According to the PHP manual:

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Uh…. ok, so what does that all mean? Well I’ll start with the part where it said “associates values to keys.” As you should already know all variables in PHP must have a value, whether it is NULL, empty, an array, an integer, whatever. So in the case of an array inside the array there are keys, these keys are like identifiers for the values contained in the array. Here is an example:

$variable = array('key'=>'value');

So to explain the above code: the variable named $variable is an array, it has one key=>value pair, the key is key and the value is value. So, to use this new value we would simply make a call like this:

echo $variable['key'];

The names of keys can be integers, or strings. If you do not set a key, PHP will set one for you. For example:

/* PHP will automatically set the key for the value Apples to 0 */
$fruit = array('Apples');
/* another way to assign values into an array is like this: */
$veggies[] = 'Lettuce';
$veggies[1] = 'Broccoli';
/* so $veggies contains two key=>value pairs, one is 0=>'Lettuce', 
                                    and the other is 1=>'Broccoli' 
To use these values, we can simply do an echo, like this: */
echo "I like to eat {$veggies[0]} and {$veggies[1]} with every meal.";

Now, keys don’t always have to be one or the other, you and mix and match all you’d like:

$breads[1] = 'Whole Wheat';
$breads['one'] = 'Light Rye';

So there you have it, a simple and straight-forward look at arrays in PHP. I hope you found it useful!

By Mike on March 26, 2011 | PHP, Tutorials
Tags: , , , ,