Monday, February 18, 2008

Ternary Operators

A ternary operator can be used as a slightly faster, cleaner way to write simple if/else statements.

Let's take a basic if/else statement...

if ($cake == "fresh") {
print "Yum yum! This cake is tasty.";
} else {
print "Yuck! This cake tastes awful!";
}

The above statement works like this:

If $cake is fresh, print "Yum yum! This cake is tasty.". If it's not fresh, print "Yuck! This cake tastes awful!".

Using a ternary operator we can rewrite this statement in a much simpler form.

We would write it like:

$message = ($cake = "fresh") ? "Yum yum! This cake is tasty." : "Yuck! This cake tastes awful!";
print $message;

In case you're not sure how that works, there are 3 parts to the ternary assignment:

$variable = condition ? if true : if false

The part to the left of the ? is the condition we're testing.
The part between the ? and the : is what happens if the condition is true.
The part after the : is what happens if the condition is false.

Hopefully that makes sense and you can now start saving a bit of time by using ternary operators in your PHP scripting!

Sunday, February 17, 2008

PHP Session

PHP Sessions


A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions.

It is important to ponder if the sessions' temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database.

Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage.

Note:If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug.


Starting a PHP Session


Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent.

Below is a simple script that you should place at the beginning of your PHP code to start up a PHP session.

PHP Code:
session_start(); // start up your PHP session!
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
?>
The first time you run this script on a freshly opened browser the if statement will fail because no session variable views would have been stored yet. However, if you were to refresh the page the if statement would be true and the counter would increment by one. Each time you reran this script you would see an increase in view by one.

Destroying your Session


Although a session's data is temporary and does not require that you explicitly clean after yourself, you may wish to delete some data for your various tasks.

Imagine that you were running an online business and a user used your website to buy your goods. The user has just completed a transaction on your website and you now want to remove everything from their shopping cart.

unset($_SESSION['cart']);

?>
You can also completely destroy the session entirely by calling the session_destroy function.

session_destroy();
?>

Destroy will reset your session, so don't call that function unless you are entirely
comfortable losing all your stored session data!