There are a lot of other tips and tricks that you should look at, and a few of them are covered below, a couple of which are transferrable between languages.
2. Introduction
• In previous articles we've touched on some major sections such
as MySQLi, Classes and cURL. There are a lot of other tips and
tricks that you should look at, and a few of them are covered
below, a couple of which are transferrable between languages.
Ternary Operators
• A ternary operator is a different way to layout a simple if
statement. Let's take a look at one and then break it down. We'll
just use a simple check to see if a GET parameter is set and not
empty, if it is we will default to the word John, otherwise use the
value that was set as the
3. GET parameter.
• $name = (!empty($_GET['name'])? $_GET['name'] : 'John');
This might look complex to begin with but there are three
sections to a ternary operator:
The conditional !empty($_GET['name'])
The true result $_GET['name']
The false result 'John'
The ternary operator will return either the true or false
conditional, and therefore it will be assigned to $name. If we
write this as a normal if statement we'd have one of the
following:
if(!empty($_GET['name'])){ $name = $_GET['name']; }else{ $name
= 'John'; } // Or $name = 'John'; if(!empty($_GET['name']))
{ $name = $_GET['name']; }
The two above and the ternary operator will all end up with
$name having something assigned to it.
4. Nesting
• While not advisable, it is possible to nest ternary operators within
each other much like you would with standard if statements, or
to form a long string of if...else if...else if...else statements.
• Ternary operators can be very helpful and clean up code, but
don't over complicate them otherwise your code might become
inundated with large amounts of complex rediculousness that
will get you hunted down by a violent psychopath.
5. Class Autoloading
• To make use of a class file, you have to ensure that the class
you're using is available within the page that you're using it. In
most cases this will be as easy as you just including the PHP file
that contains the class.
• But what if we have a very large number of classes that we
could potentially use within one or many sections of our code?
Well, we could include all of them within a common header file,
or only include the ones that we know that we're going to use on
that page. However, we then need to remember to include new
classes whenever we want to use a different one etc.
• So instead of including classes that might not get used, or we
might remove later, we can employ class autoloading. When we
autoload classes we can write what will happen - this might be
to just throw an error to let us know that we've not included the
class file, or, as in most cases, we make the function include the
file that we need.
6. The Code
• Here we'll be using the PHP function spl_autoload_register(), we'll only
be interested in the first parameter, but there are two others that you
can define if you wanted the function to behave differently. I'll jump
straight into how to use it:
• function loadMyClass($class){ echo 'We are loading class: ' . $class;
include_once('classes/' . $class . '.inc.php'); echo 'Class loaded.'; }
spl_autoload_register('loadMyClass');
This will pass the name of the class that we're trying to load as the first
parameter of the function that we specified. As of PHP 5.3.0 we are
able to use anonymous functions, basically a function thas has no
name:
• spl_autoload_register(function ($class){ echo 'We are loading class: ' .
$class; include_once('classes/' . $class . '.inc.php'); echo 'Class
loaded.'; });
• Which will work exactly the same as the method of implementation
above. So now in future, we don't need to include the class files that
we'll be using as this function will run if the class is undefined, include
the class and then the instantiation will be called.
7. Dynamic Variables
• Sometimes we want to be able to dynmically define a variable, or refer
to a variable based on a user's input. Let's start with this code:
• $myname = 'Michael'; $yourname = 'Ben'; if($_GET['whatname'] ==
'your'){ echo $yourname; }else if($_GET['whatname'] == 'my'){ echo
$myname; }else{ echo 'No Name'; }
To use the value of a variable to then form another variable we use
double dollar signs $$ so for example we could do $who = 'myname';
then echo $$who. This will echo out the value of $myname.
• Break it down like this (exemplary code, not actual working code):
$who = 'myname'; $$who = $'myname'; $$who = $myname; echo $
$who; // would return 'Michael'
In reference to the code above we could look to use something along
the lines of the following for very dynamically defined variables:
• $who = $_GET['whatname'] . 'name'; if(isset($$who)){ echo $$who; }
else{ echo 'No Name'; }
As with ternary operators, you should use these loosely, as in most
situations there's not much need for them, but you will most probably
find some crazy-ninja somewhere who uses this notation.