Wassup Sam !? Happy to have you as the new community moderator

This thread was looking bare so I decided to write a few quick tips, haha

I’ll start with one of my favourites… the shorthand if/else statements, otherwise known as ternary operators.
Why write something in 20 lines when you can do it in much less ?
This is where ternary operators come in handy.
In it’s most basic form it’s written as “condition ? value if true : value if false” simples! (reference to british advert)
Here are a few examples :
Show an administration panel if admin is logged in, otherwise show a link to login page.
$sidePanel = $user->level == 'admin' ? file_get_contents('sidePanel.php') : '<a href="login.php">Admin login</a>';
Show a message with either a user’s name or “guest” depending if that person is logged in or not.
$welcome = 'Hi '.($user->logged_in ? $user->name : 'Guest').'!!!';
Adds an s to the number of hours a post has been posted if it’s above 1.
$timePosted = 'Posted '.$hour.' hour'.($hour != 1 ? 's' : '').' ago';
Redirect users that aren’t logged in to the login page.
header('Location: '.($user->logged_in ? 'index.php' : 'login.php')); exit;
They can also be nested within one another like those babushka doll thingies

(also too many of these and the code can become hard to read).
$welcome = 'Hi there! You'.($user->age > 18 ? '\'re underage, therefore I\'m afraid you can\'t view this website.' : ($user->sex == 'male' ? ' handsome man!' : ' sexy minx!'));
This displays “Hi there! You’re underage, therefore I’m afraid you can’t view this website.” if you’re under 18, “Hi there! You handsome man!” if you’re a man over 18 and “Hi there! You sexy minx!” if you’re a woman above 18. Why? You may ask… I don’t know, it’s just an example, lol
Error Reporting, useful for site development.
error_reporting($testing ? E_STRICT : 0);
This is something I’ve personally never used but it’s good to know, I guess.
isset() can be used instead of strlen to check a string’s minimum length, in other words both of these are interchangeable :
if (strlen($string)>7) {
// do something if the string's at least eight characters long.
}
if (isset($string[7])) {
// do something if the string's at least eight characters long.
}
When you pass strings as an array each letter or character in the string is an element in that array.
So basically checking to see if he 7th element in this array exists you know if the string is at least 8 chars long (arrays start from 0, so $string7] is the 8th character in $string).
isset() is supposedly faster than strlen() as it’s a language construct and not a function, but I doubt it makes a huge difference.
Another useful part of php is, of course, the ol’ shorthand operators.
I think these are best explained by examples…
On the left is how you would write these normally and on the right is their shorthand form… $v1 and $v2 are just two mundane variables.
Arithmetic Operators :
Addition : $v1 = $v1 + $v2 $v1 += $v2
Subtraction : $v1 = $v1 - $v2 $v1 -= $v2
Multiplication : $v1 = $v1 * $v2 $v1 *= $v2
Division : $v1 = $v1 / $v2 $v1 /= $v2
Modulus : $v1 = $v1 % $v2 $v1 %= $v2
Bitwise Operators :
Bitwise And : $v1 = $v1 & $v2 $v1 &= $v2
Bitwise Or : $v1 = $v1 | $v2 $v1 |= $v2
Bitwise Xor : $v1 = $v1 ^ $v2 $v1 ^= $v2
Left shift : $v1 = $v1 << $v2 $v1 <<= $v2
Right shift : $v1 = $v1 >> $v2 $v1 >>= $v2
And lastly string Operators :
Concatenate : $v1 = $v1 . $v2 $v1 .= $v2
If you’re a php developer it’s likely you know all these already but it’s nice to have them all written out in one place.
Anyway I’ll stop there as this post is long enough as it is, I hope these are useful to someone
