PHP

Real Hosting

Real hosting is something much more than what meets the eye. It is more than a place to merely store files and have a space on the Internet. It’s more than just unlimited this and unlimited that; it involves real space, real deals, real support, and real guarantees.

What you REALLY pay for

Ever heard of the Myth of Unlimited Hosting? Aside from being technically impossible, it is highly unlikely that a web hosting company will charge you $4.95 a month if you agree to stay for 60 months. This includes unlimited disk space, a free domain name, unlimited bandwidth, unlimited databases, unlimited FTP accounts, unlimited emails, and unlimited everything else.

If you happen to break the 60 month agreement you are slapped with a termination fee and other hidden fees. This can quickly become a major hassle, especially when you receive poor support and service when you just want out of a bad hosting situation.

Often, in these situations, the hosting companies will begin to nag at you when too much space is taken up on a shared server, you’re taking up too much CPU, or taking up too much RAM on the server. This renders the promise of unlimited features as invalid and not a real guarantee and not real, honest hosting.

These types of hosting companies will go into your account and begin changing things if you have too many files on the server, if the files are taking up too much space, or too many “unrelated files”. They will disable the website and then it becomes a hassle to get your website back online. While they’re breaking promises, you’re losing traffic, clients, and/or money. Whether it is a business website or a personal blog, it is embarrassing when you have to explain to clients and friends why your website is down in the first place.

While you’re banging your head against a wall wondering why your website was suspended, they will be helping other people and not replying to your support tickets, emails, or phone calls. Some hosts will treat you like a second-class citizen if your account is suspended.

What you get with real hosting from AtomicPages

AtomicPages Real Hosting is different; we do not make promises we cannot keep. We will never claim to have unlimited features and go back on our word later on. The amount of features you pay for is the amount of features you will actually receive and nothing less; no questions asked.

This is what defines real hosting from impostors. We are honest, backup our guarantees, and have real morals. You will never hear that we are suspending your account due to too much space, taking up too many resources on the server, or any other made-up excuse. We will not impose upon your website unless it is breaking the law. AtomicPages real hosting will never leave you in the dark about your website.

Helping people with websites and delivering good quality hosting while keeping a promise is not all smoke and mirrors. There is no mystery to keeping a promise.

PHP #5: More Loops

There are a few other types of loops in addition to while loops that we learned about in Part 4.

Do-While Loop

A do-while loop is very similar to a while loop. The only difference is that the condition is evaluated after each loop iteration, rather than before. The syntax is like this:

1
2
3
do {
  // some code here
} while (condition)

Since the condition isn’t evaluated until after the loop body, the loop body is always executed at least once, whether or not the condition is true. Once it does get to the condition, if it is true, it goes back up to the top of the loop body. If it is false, the loop terminates.

1
2
3
4
5
$i = 11;
do {
   print "The number is $i<br />";
   ++$i;
} while ($i <= 10)

The above code prints:

The number is 11<br />

even though the condition is not true. The loop body always executes once.

For Loop

The for loop is a bit more complicated. The syntax looks like this:

1
2
3
for (initial-expression; condition; loop-end-expression) {
  //some code goes here
}

The initial-expression is evaluated only once, at the very beginning of the loop. The condition is then evaluated. If the condition is true, the body of the loop executes. Then the loop-end-expression is run. The process then starts over again with evaluating the condition. A for loop is generally used to do simple bounding loops, like this:

1
2
3
for ($i = 1; $i <= 10; ++$i) {
   print "The number is $i<br />"
}

As you’ll notice, the same exact thing can be done with a while loop, but many prefer the for loop as it is more compact:

1
2
3
4
5
$i = 1;
while ($i <= 10) {
   print "The number is $i<br />";
   ++$i;
}

More Looping Examples

Let’s try using a for loops to create a multiplication table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$start = 1;
$end = 10;

print '<table border="1" cellspacing="0" cellpadding="5">';
for ($y = $start - 1; $y <= $end; ++$y) {
    print "<tr>";
    for ($x = $start - 1; $x <= $end; ++$x) {
        if ($y == $start - 1) {
            print "<td><b>$x</b></td>";
           
        } else if ($x == $start - 1) {
            print "<td><b>$y</b>";
        } else {
            print "<td>"
            print $x*$y;
            print "</td>";
        }
    }
    print "</tr>";
}
print "</table>";

Notice that there is a for loop inside of another for loop. Trace the above code carefully to see how it works. This code will produce the following table:

0 1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100

PHP #4: While Loops

In programming, loops are used to execute the same lines of code multiple times. The simplest type of loop is called a while loop. The lines of code in the body of a while loop will continue executing over and over WHILE the condition is true. The syntax looks like this:

1
2
3
while (condition) {
   //run this code repeatedly while the condition is true
}

Just like with if statements, the condition is evaluated as a boolean. If the condition is true, the block of code is executed and then it starts over my evaluating the condition again. If the condition is false, the block of code is not executed and the loop is terminated. Let’s try making a loop that will print all the numbers from 1 to 10:

1
2
3
4
5
$i = 1;
while ($i <= 10) {
   print "The number is $i<br />";
   $i = $i + 1;
}

We start with a variable $i that we have set at 1. 1 is less than or equal to 10 so the loop body executes. “The number is 1<br />” is printed to the HTML, and then we add 1 to the variable $i. Now the condition is checked again, but this time $i is 2. 2 is still less than or equal to 10, so the loop body executes again. This continues and each time the number is printed and 1 is added to $i. Once $i gets to 11, the condition is no longer true, so the loop terminates.

A word about incrementing

When dealing with loops it is common to have an indexing variable, like $i in the previous example. Usually you will want $i to either increase by one or decrease by one on each iteration of the loop. In the previous example, we used

1
 $i = $i + 1;

to do this, but there is a simpler way that accomplishes exactly the same thing:

1
++$i;

This is called the pre-increment operator. There is also a pre-decrement operator that will subtract one from the variable:

1
--$i;

Important Note: A common misconception is that ++$i is the same thing as $i + 1. They are not the same. ++$i actually changes the value of $i to be 1 greater. $i + 1 keeps $i as is, but gives you the number 1 greater than $i to use as a value.

With that said, we can rewrite our loop above using the pre-increment operator:

1
2
3
4
5
$i = 1;
while ($i <= 10) {
   print "The number is $i<br />";
   ++$i;
}

This loop accomplishes exactly the same thing as the previous one.

More Looping Examples

Looping can be used to accomplish a lot of different things.

In mathematics, the factorial of a positive non-decimal whole number is the product of all positive non-decimal numbers less than or equal to it. For example:
5 Factorial = 1 * 2 * 3 * 4 * 5 = 120

We can use a while loop to calculate the factorial of a number:

1
2
3
4
5
6
7
$i = 5; //the number to calculate the factorial of
$factorial = 1;
while ($i > 0) { //once $i is no longer a positive integer, stop looping
   $factorial = $factorial * $i;  //multiply by the current number
   --$i;  //go down to the next number
}
print $factorial;

This will print the number 120, the factorial of 5. If we want 10 factorial, all we have to do is change the initial value of $i, and our code will do the rest.

Now let’s apply what we learned in PHP #2: GET and POST to ask a user what number they would like to know the factorial of. First we’ll need an html form where the user enters the number:

1
2
3
4
5
6
7
8
9
10
11
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<form method="get" action="factorial.php">
     Calculate the factorial of:<input type="text" name="factorial" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Now we’ll create a php file called factorial.php that will process the info from the form using the same method as before.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<?php
$i = $_GET['factorial']; //the number to calculate the factorial of
$factorial = 1;
while ($i > 0) { //once $i is no longer a positive integer, stop looping
   $factorial = $factorial * $i;  //multiply by the current number
   --$i;  //go down to the next number
}
print "The factorial is $factorial.";
?>
</body>
</html>

Now view the html page in your browser and you should have a functioning factorial calculator. You can download the full source code for this example here:
Factorial Example
Factorial Example
Size: 1.17 KB

PHP Video Tutorial 8: Comparison and Logical Operators

PHP Video Tutorial 7: If-Else Statements