WarDrome Blog

Browser Game School: Getting started with PHP

Getting Started with PHP

WarDrome (like many other games or sites) is programmed with PHP.

PHP was created in 1994 by Rasmus Ledorf and is primarily designed to provide a language that allows to generate dynamic content, interacting with the user actions.

At that time the html language was the owner of the web, but its limitations and its shortcomings have led various community to move to an alternative way of programming, PHP is one of those who initially was housed in a small CGI library and called "Personal Home Page". With the development of technology and the increasing success, PHP engine was rewritten by the same Ledorf and two other employees named Andi Gutmans and Zeev Suraski that after the release of Version 3, was called Zend (the initials of their names).

PHP is an interpreted language, which means that unlike Java does not need to be compiled in order to be interpreted by the web server will do, in fact, you just need to upload files to the web server.

PHP is open source, and is released (currently) with a proprietary license, PHP License 3:01 .

Leaving aside the small and boring theoretical part, we go to the pratical part.To let the PHP engine work properly we need the following components:

  • Apache HTTP Server : Apache HTTP Server is the Web server that will receive our HTTP requests and allow to host our files html/php through the appropriate directory.
  • PHP : PHP (as it is easy to understand) is the engine that will allow us to interpret our code in our PHP files.

After installing these two components, let's go to the root of our Apache server (for Windows operating systems will be c:\Program Files\Apache2\htdocs for Linux is /var/www, consult the official documentation for more information) and let's get started with the practive.

Variables and constants:

Like any other programming language PHP is also the possibility to use variables. The variables are simply containers to store any data including:

Integer

As you surely know, an integer is a number without commas.

Example:

$integer = 145;

Note that the whole should not be enclosed in quotation marks " " or ' '

Strings

A string represents a set of characters (not necessarily alphabetical). Strings can be declared in several ways including double quotes ("Content") or single quotes ('content') or by using the syntax HEREDOC . Let's see some examples:

$string1 = "My String 1";
$string2 = 'My string 2';
$string3 =
<<

These three instructions produce the same result, and then you're probably wondering what purpose use three ways to view the same results. We start with an example where, for example, you have to concatenate 2 variables inside one.

$string1 = "Number 1";
$string2 = "Number 2";
$string3 = "I join the two strings string1 and $ string2 $";
echo $string3 // This will produce the result: "I join the two strings the number 1 and number 2"

This result is possible because the contents of the third string is contained within double quotes (""). However, if we wish to print directly the $string2 without display the contents of the $string2 variable, you can use single quotes:

$string1 = "Number 1";
$string2 = "Number 2";
$string3 = 'I print only the text and $ string2 $ string1';
echo $string3 // This will produce the result: 'I print only the text $string1 and $string2

As regards, however, the syntax HEREDOC is the equivalent of a double quotes, with the difference that you can write in multiple lines like this:

$string1 =
<<

Float

A float datatype is a number with variable comma and can be used, for example, when you want to specify a price or any number with decimal places. For example:

 $float = 12.34;

Array

For arrays, there is a fairly long speech about it. Just think of an array as a container with different "drawers" and each drawer has a key (key) and a value associated with the key (value). Example:

$office_things = array("books", "feathers", "pencils");

In this example we have an array called $office_things and will have a pair of key values, the keys will be incremental and will start from 0 (it is good to remember that the indices in an array starts from 0, not 1) and values ​​are those mentioned above. To see the result of our example we can write:

$office_things = array("books", "feathers", "pencils");
var_dump($office_things);
exit;

The result will be:

array
  0 => string 'books' (length = 5)
  1 => string 'pens' (length = 5)
  2 => string 'pencils' (length = 6)

Another way to insert a given data in an array is as follows:

$office_things = array ();
$office_things[] = "books";
$office_things[] = "pen";
$office_things[] = "pencils";
var_dump ($office_things) // print the same result as the above
exit;

This method is especially useful when you want to add certain types of data in several places in the code, such as iterations, control structures, etc. ..

Boolean

A Boolean data type (or bool) is nothing but a data type that can have two values: true or false . Its use is particularly useful when you want to make checks of a given value in a specific part of a code...now we see a practical example:

$string = "My string";
if (is_string($string)) {
   echo "I'm a string";
}

In this simple example we use the function is_string () as you can well imagine that checks if the supplied parameter ($string in this case) is a string, then returns true if so, false if not. In the event that we would carry out an inverse, and check if the variable $string is not a String we can do:

$string = "My string";
if (is_string($string)) {
   echo "This is not a string!"
}

In this case the result will never be printed on the screen because $string is a string. The exclamation point before a Boolean statement is a sign of denial and is, in fact, to do a reverse control. Control over Boolean values ​​can also be made as follows.

$bool = true;
if ($bool) {
  echo "The value is true";
else {
  echo "The value is false";
}

In this example, the result that is going to be printed to the screen is "The value is true" because the variable $bool is set to true, but if we just set it to false see the result reversed.

At this point I would say that our beginners guide may be coming to an end, further studies will be analyzed in the next post in this blog (including the if statement that we used in this example but wasn't explained).

A big thank you, finally, to the admins (Ricardo and Laura) for allowing me to spread my knowledge to you all, I hope the article has not been too boring and long.

Ok that's all guys, all my best greets to you all, bye!

Cristian (MadCris)

Un pensiero su “Browser Game School: Getting started with PHP

  1. Pingback: PHP Guide: the if statement | WarDrome Blog

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Optionally add an image (JPEG only)

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close