I’ve been actively coding the past few months and this is a quick preview of my latest progress.
If Then Else for PHP
One of the most important syntax’s in programming is the If-Then-Else statement, it’s pretty much what the transistor does in hardware in the computer processor, it has a If then else system as well.
In PHP There are a few ways to write a If Then Else syntax:
Lets set a default value for the examples first:
|
1 |
$a = 10; |
If-Then-Else Syntax 1:
|
1 2 3 4 5 |
If ($a < 10) {
echo 'A is lesser than 10';
} else {
echo 'A is greater than 10';
} |
If-Then-Else Syntax 2:
|
1 2 3 4 5 |
If ($a < 10):
echo 'A is lesser than 10';
else:
echo 'A is greater than 10';
end if; |
If-Then-Else Syntax 3 (Shorthand):
|
1 |
echo ($a < 10 ? 'A is lesser than 10' : 'A is greater than 10'); |
In syntax’s 1 and 2 you can add a secondary condition called “else if” or “elseif”, this condition allows you to enter a secondary question to the if statement.
If-Then-Else Syntax 1 (Updated with ElseIf):
|
1 2 3 4 5 6 7 |
If ($a < 5) {
echo 'A is lesser than 5';
} elseif ($a < 10) {
echo 'A is lesser than 10, but equal or greater than 5';
} else {
echo 'A is greater than 10';
} |
If-Then-Else Syntax 2 (Updated with ElseIf):
|
1 2 3 4 5 6 7 |
If ($a < 5):
echo 'A is lesser than 5';
elseif ($a < 10):
echo 'A is lesser than 10, but equal or greater than 5';
else:
echo 'A is greater than 10';
end if; |
If-Then-Else Syntax 3 (Shorthand, Updated with ElseIf):
|
1 |
echo ($a < 5 ? 'A is lesser than 10' : ($a < 10 ? 'A is lesser than 10, but equal or greater than 5' : 'A is greater than 10')); |
Regular mode using either syntax 1 or 2 is fairly easy you just add in a secondary statement called elseif while with Shorthand you need to add in a whole other if else statement into the correct position, the Shorthand works like this:
(condition ? true : false)
So for a secondary statement if that condition registers as false as it does with an elseif statement, it really is a secondary if statement if the first one didn’t pass. This is the way to make a shorthand if elseif statement, you replace the false statement on the previous example such as this:
(condition1 ? true : (condition2 ? true : false))
I usually always used the Syntax type 1 but recently I’ve gotten fond of the Shorthand method, one good example is for generating selection menus from a MySQL/MariaDB database:
|
1 2 3 4 5 6 |
$data = @mysql_query('SELECT * FROM table ORDER BY name');
if (mysql_errno() == 0 && mysql_num_rows($data) > 0) {
while ($row = mysql_fetch_array($data, MYSQL_ASSOC)) {
echo '<option value="' . $row['id'] . '" ' . ($a == $row['id'] ? ' selected="selected"' : '') . '>' . $row['name'] . '</option>' . "\n";
}
} |
This example does a MySQL connection request and a loop through the data and if the $a value matches the $row['id'] for the current row it will output the following data:
|
1 |
<option value="x" selected="selected">Name</option> |
Otherwise you get this:
|
1 |
<option value="x">Name</option> |
These are the basics of If Then Else in PHP today.
My MMO Review
The last 10 years or so I’ve been playing many different MMO’s (Massively Multiplayer Online). And many had some small good things, and then some odd limitations and other things. I will bring the games up in the order I played them and list their strengths and weaknesses, this isn’t a review about the graphics but more about how the games performed on hardware of the day when they are/were current. This mass-review I write as a coder in mind, pointing out things a coder would think about, at least one like me
My big bang theory
So welcome to my first Generic jibberish post.
First of all I’m not a physicist but I sometimes wondered about this and I had many theories for many years that people just recently have accepted as common knowledge, and the Big bang theory (no not the TV-series, though that’s great!) is one of those theories I feel is illogical in its current form.
Optimization
Optimizing you code is important, why? Mostly because of speed, both transfer speed and loading. So how do you go about improving the speed on your websites? There are a couple of tools that will aid you in this:
- YSlow by Yahoo
- Page-speed by Google
Personally i prefer YSlow these days, I used to use Google Page-speed before, but it’s not quite as good as YSlow is. They both work virtually the same way but YSlow incorporates more real world usage where as Page-speed is more optimizing to the level where it’s not really faster anymore.
Hello world!
As this is my first blog here, and WordPress defaults to Hello world for the topic, so I figured I’d let it stay as that is the common first program everyone writes in any programming language that they learn. I’ve been coding PHP since about year 1999 to 2000 somewhere and HTML since before Windows 95 hit the market.