How To Develop Your Very Own PHP Game
Tutorial on making a PHP game, from scratch :)

Step 4: PHP

By justImagine
Now time to start with PHP. First of all, I must warn you NOT to freak out if you faced an error, so don't be like me. Second, let's roll.
PHP is a kind of interactivity-creating browser-based language. This is what we'll use in the game making in this blog.
PHP codes are easy to notice. They start with <?php and ends with ?>. You'll also notice a lot of $ signs in the codes. The words after $ like $username is called variable, while the capital letters after $_ with parameters in box brackets like $_GET["name"] is called superglobal. The example variable we can call as 'username variable' while the superglobal is 'GET superglobal' (this superglobal had things to do with data sent through GET method from the form as explained previously on Step 2).

Here is an example of PHP code:
<?php
$name = "Alfi";
print("Welcome, " . $name . ".");
?>


Save it as php_try_1.php (note that the file format is .php).Those codes will create this outcome if you have a PHP supporting server or works with a file hosting that supports PHP:

Welcome, Alfi.

Dull, huh? Don't be mistaken, the print(""); function is pretty useful, we'll use pretty much of it later. Notice that the variable making ($username = "Alfi";) had "" marks to start and end characters (this differentiates characters like words and functions like print()) and ends with a ';' and so as the function. DON'T MISS THAT SIGN. Without it, the parser (the syntax checker on the server) would send an error message about Unexpected $end on *file_address* on line x.
Talking back to our first script, the print() functions works as an order to display characters on the monitor. For an instance, check our previous code:

print("Welcome, " . $name . ".");

It'll print the words 'Welcome, ' (with a space). Then the " sign after the space after the coma sign ends the words to be printed. Then there's a space and then a dot and another space, it works as a connector for an instant print through another function — in this case, through a variable. Then another connector, then the codes "." prints the '.' (dot) sign.
OK, maybe I explained a little too fast, so take a deep breath.
*Inhales deeply.*
*Exhales.*
OK, back to business.
The earlier functions prints the words 'Welcome, ' and then the content of the variable $name, and then a full stop mark.
Because we are printing a variable (and not a function), you could also make the codes written like this:

<?php
$name = "Alfi";
print("Welcome, $name.");
?>


It would have the same outcome. Now change the value of the $name into "David". It'll print this:

Welcome, David.

So, it's an example that a variable IS a variable — it can have various content. This is useful for game data keeps, and some interactions.

Try this too to see what PHP is capable of. Edit the source files of php_try_1.php and change the earlier codes into these:

<?php
$name = "Norbert";
print("Welcome, $name. Right now it's " . date("d M Y") . ".");
?>


It'll have an outcome like this (e.g. it was 6th of June, 2010):

Welcome, Norbert. Right now it's 06 Jun 2010.

Notice that the print function used connectors to print the date() function. Nice results eh? The date() function checks the time on server. While the "d M Y" indicates the format of the time writing. In this case, it uses a small 'd' which means date in two digits (06), a capital 'M' which means month in three letters format (Jun), and a capital 'Y' which means the current server year in four-digits format (2010). There will be a list for the indicators for this function later, but be patient because we had a lot to talk about — and the multiplayer version of our RPG requires MySQL database, which would get you some extra stuff to think about.

However, that's for now. In the next part, we'll talk about making the content of the variable through an interface with the HTML codes we knew and using some superglobals.
 

0 comments so far.

Something to say?