• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Starting with PHP. ( From Newbie to Pre-Advanced )

slavi

#define SLAVI32 _WIN32
Senator
Joined
Sep 9, 2015
Messages
681
Solutions
11
Reaction score
560
GitHub
slavidodo
First of all if you need information of PHP, read the basics.
Some tips if you faced problems.

-- Check the manual of PHP
-- If you didn't solve, make support thread here in otland.
-- If didn't get help at all, ask stackoverflow

After this tutorial, you will be able to write codes related to tibiaserver.


So let's start.

Step 1: Installing PHP, ( Apache or Nginx)
--------------------------------------------------------------------
1 - For windows users, for easiest approach install Uniform Server, alternatively you can install Apache manually.
If you installed uniform, go to directory where you installed it, then open folder "www", remove all files inside and create a file named "index.php"
2 - For Linux users, this is a great tutorial, if you want to use nginx, here
The directory of "www/public_html" is mentioned in the tutorials, open it and chown it, then remove everything, and make index.php
Step 2: learning all basics.
--------------------------------------------------------------------
PHP language is case-insensitive so doesn't matter you write `eChO` or `EcHo`

The PHP grammar can be written only inside <?php ?>
PHP:
<?php
// Php Code here
?>

To write a comment you can use "//" for line comment and "/* */ for long comment.

PHP:
// Comment here
No Comment here

/* Comment here
Also comment here
As well comment here :p */


Learning basic functions.
PHP:
/* echo : outputs all parameters */
echo "PHP"; // PHP
echo "PHP", " is ", " fun."; // PHP is fun

/* var_dump : outputs structure information. */
var_dump("PHP"); // string(3) "PHP"
// This function is commonly used for objects/arrays ( will learn them soon )
*/
PHP types :
To declare a variable we use : "$"
Colons ";" are used to end lines.
PHP:
$string = "String"; // String variable.
$integer = 19; // Integer variable
$float = 19.1; // Float(double) Variable, ...
$bool = true; // boolean(true/false)
Before moving to string/integer functions we will get some information about "Arrays"
PHP:
$m = array(); // Empty array
$s = []; // Aswell empty array.
// So there is two ways to declare an array.
$m = array(1, 2, 3);
$m = array("a" => 1, "b" => 2); // As you see, i can call variables and re-declare them.

/* How to call an index in the array? */
$array = [1, 2 ,3]; // What is this? Yah, an array.
var_dump(  $m[0] ); // We talked before about var_dump;
// The result will be : int(1)

/* How to add index to array */
$array = array(1, 2, 3);
$array[] = 4; // This is a way;
array_push($array, 5); // This is another way;

var_dump($array);
/* result :
array(5) {
   [0]=> int(1)
   [1]=> int(2)
   [2]=> int(3)
   [3]=> int(4)
   [4]=> int(5)
}

[x] mean index number.
*/

Before moving to Next step!
[Question] : What is difference between variable x and y in this code ?
PHP:
$x = "15";
$y = 15;
var_dump($x, $y);
The difference is that variable x is a string, but variable y is an integer.
Step 2: Deep in types.
--------------------------------------------------------------------

String.

1) strlen($string), returns the length of a string.
PHP:
$x = "15";
var_dump(strlen($x)); // returns int(2)
2) Replacing texts in string.
There is two functions :
str_replace($tofind, $toreplace, $string) : case sensitive
PHP:
// Test to see result yourself.
$string = "I am not learning PHP";
var_dump ( str_replace("not learning", "learning", "$string") );
// This will return string(17) "I am learning PHP"
// Try to test this again.
var_dump ( str_replace("not Learning", "learning", "$string") );
// This will return string(21) "I am not learning PHP";
// yeah when we used capital L, it didn't replace anything.

str_ireplace(...same as str_replace..) : case insensitive
PHP:
$string = "I am not learning PHP";
var_dump ( str_ireplace("not learning", "learning", "$string") );
var_dump ( str_ireplace("not Learning", "learning", "$string") );
// Both will return string(17) "I am learning PHP"

3) Lowercase or Uppercase string.
strtolower($string)
strtoupper($string)
PHP:
$string = "HelLo PeoPle";

var_dump( strtolower($string) );
var_dump( strtoupper($string) );
I guess you have now a good background at functions.
To learn more and more functions go to W3schools or PHP.net.

Math ( applies to integers/floats ).

1) sin, cos, tan ($number)
as well asin, acos, atan($number) : arc *($number)
PHP:
// I guess you already know them from calculator.
$l = sin(30); // 0.5
$t = asin($l); // 30
2) rand($min, $max) : random number from min to max.
You can leave $min, $max empty.
PHP:
$s = rand(10, 20); // well i can't guess the result ;p
$h = rand();
3) round($float)
Refer to manual of these function to learn more about it.
PHP:
$l = round(9.15); // 9
$a = round(9.95) // 10
4) pow($base, $exp)
PHP:
$a = pow(3, 2); // 3 ^ 2 = 3 * 3 = 9
$l = pow(2, 3); // 2 ^ 3 = 2 * 2 * 2 = 8

[30-04-2016, 10:55 CEST]
Step 3: Conditions and loops
--------------------------------------------------------------------
Conditional statements are used to perform different actions based on different conditions.
The if statement ::
PHP:
/* if ...  */
if (condition) {
    // code to execute if condition is true
}

/* if ... else ... */
if (condition) {
   // code to execute if condition is true
} else {
  // code to execute if condition is false
}

/* if ... else ... elseif ...*/
if (condition1) {
   // code to execute if this condition1 is true;
} elseif (condition2) {
  // code to execute if this condition2 is true and condition1 is false;
} else {
   // code to execute if all conditions are false;
}

Example!
PHP:
$x = 20;
$h = 15
$y = 18;
if ($x > $y) {
    echo "x is more than y";
} elseif ($h > $y) {
    echo "h is more than y but x is less than y";
} else {
    echo "x and h are less than y"
}

if ($x == 20) {
     echo "x is equal to 20";
}

The switch statement ::
PHP:
switch (something) {
    case label1 :
        // code to execute if something==label1
    case label2 :
        // code to execute if something==label2
    default :
        // code to execute if something doesn't meet any of cases!
}

Can you execute same code on 2 conditions in if/switch statement?
Yes.
PHP:
// in if you can use (&&) which means and, (||) which means or. 
if ($x == $a || $x == $b) {
    ....
}
// in switch statement
switch (something) {
    case label1 : case label2 :
        // dosomething when (something == label1 || something == label2)
    default :
        ...
}

So after this point don't forget that ";" ends lines, "{ }" are used in functions, classes, ....

I will write more as soon as i am available.
Regards <3 .
 
Last edited:
Step 4: Functions And Objects
--------------------------------------------------------------------

Simple Function.
PHP:
function Sum($x, $y)
{
    return $y + $x;
}

$h = Sum(15, 20); // 35

// More complicated functions comes once you learn objects.

Creating objects.
PHP:
// So this how you create an oject.
class someclass {
}

1) Constructor.
I suppose if you have took a course before in C/C++ you would easily understand this.
PHP:
class Account
{
    public function __construct($id)
    {
        $this->id = $id; // $this means this object which we will create, -> ( refer to ), id ( variable name )
    }

    public function getId() // function doesn't accept arguments. if you noticed.
    {
        return $this->id; // returns the id registered from the construction.
    }
}

// Use "new" keyword to define it's a new object, since it's not function.
$acc1 = new Account(15);

// Now we have made object $acc1 let's try some things.
var_dump($acc1);

echo PHP_EOL; // PHP_EOL means new line ( use it instead of "<br>" )

echo $acc1->getId();

/* result :
object(Account)#1 (1) {
    ["id"]=> int(15)
}
15
*/

2 ) Visibility
PHP:
class Account
{
    public function __construct($id)
    {
        $this->id = $id;
    }
    // This function is invisible and you can it only in the class.
    private function getPrivateId() // using "private"
    {
        return $this->id;
    }

    // This function is visible where you can call it outside the class
    public function getPublicId() // using "public"
    {
        return $this->getPrivateId(); // It's inside the class, so i can call the function.
    }

    // This function is visible, however it's private, and you can't call it outside.
    protected function getProtectedId()
    {
        return this->getPrivateId();
    }
}

$acc = new Account(15);

echo $acc->getPrivateId(); // Error
echo $acc->getProtectedId(); // Error
echo $acc->getPublicId(); // Fine

So you say now, Can I use the visibility on variables.
- Yeah Sure.
PHP:
class someclass {
     public function __construct() { }

     private $private_variable = 15;
}

Things to know.
Construction methods, don't return anything!
PHP:
class someclass {
    public function __construct() { return "hi"; } // wrong
    public function __construct() {  } // fine.
}
 
Last edited:
Great Start for a tutorial xD atleast for a newbie in php like me but
Code:
var_dump
Same as
Code:
echo
Because i didn't really catch what does var_dump mean
 
Great Start for a tutorial xD atleast for a newbie in php like me but
Code:
var_dump
Same as
Code:
echo
Because i didn't really catch what does var_dump mean
Read carefully, echo is not var_dump
PHP:
echo "Hi"; // Hi
var_dump("Hi"); // string("Hi")
 
Actually php is case sensitive,
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>
   </head>

   <body>

      <?php
        $a = 3;
         echo $A; // will generate an error
      ?>

   </body>
</html>
Code:
PHP Notice: Undefined variable: A in /web/com/1462049630_131405/main.php on line 10

Also its not good programming practice to use whatever capitalization you feel like it when it comes to reserved words, if the reserved word is all lowercase then use it in all lowercase.

Lets call things what they are, believe it or not it is important :)
; is called a semicolon, this is a colon :

PHP:
// this is called in an in-line comment, because everything from the // til the end of the line is commented out

/* This is called a block comment, because everything within the it is commented out, this type
of comment can span multiple lines or be hidden in between executable code */
 
Last edited:
Actually php is case sensitive,
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>
   </head>

   <body>

      <?php
        $a = 3;
         echo $A; // will generate an error
      ?>

   </body>
</html>
Code:
PHP Notice: Undefined variable: A in /web/com/1462049630_131405/main.php on line 10

Also its not good programming practice to use whatever capitalization you feel like it when it comes to reserved words, if the reserved word is all lowercase then use it in all lowercase.

Lets call things what they are, believe it or not it is important :)
; is called a semicolon, this is a colon :

PHP:
// this is called in an in-line comment, because everything from the // til the end of the line is commented out

/* This is called a block comment, because everything within the it is commented out, this type
of comment can span multiple lines or be hidden in between executable code */
Actually, variables, function, class names ( you specify ) are not in the list we call case sensitive, but it's good point to tell. what I meant is ( built-in )
 
Actually, variables, function, class names ( you specify ) are not in the list we call case sensitive, but it's good point to tell. what I meant is ( built-in )
I've always written the code the exact way it was defined, but you are right, the case sensitivity doesn't matter when it comes to class/methods.
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>    
   </head>
 
   <body>
   <?php
      class a {
          public function __construct(){}
        
          public function hi(){
              return "Hi, I am class a";
          }
      }
   ?>
    
      <?php
        $a = new A();
         echo $a->Hi();
      ?>
 
   </body>
</html>
Code:
Hi, I am class a

However I still thinks its bad to not write things as they are defined :p
 
Good job.

You didn't talk about conditions and loops yet, in my opinion you should leave classes for later, but thats my opinion.
 
Thanks for this great guys, I'll edit tomorrow to add "If, for, while" and so on before classes,
 
Well if we are going to discuss classes what about static classes?
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>    
   </head>
 
   <body>
   <?php
        class a{
          
            public static function hi(){
                return 'Hello there';
            }
        }
   ?>
    
      <?php
         echo a::hi();
      ?>
 
   </body>
</html>
Code:
Hello there

And the differences between this & self.

Of course there is inheritance as well, but its best to start out with the basics, like what variables are, what an array is, then move into data types, iterators, post/pre fix operators, functions, returning a value, classes should really be the last thing on the list.
 
Well if we are going to discuss classes what about static classes?
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>  
   </head>

   <body>
   <?php
        class a{
        
            public static function hi(){
                return 'Hello there';
            }
        }
   ?>
  
      <?php
         echo a::hi();
      ?>

   </body>
</html>
Code:
Hello there

And the differences between this & self.

Of course there is inheritance as well, but its best to start out with the basics, like what variables are, what an array is, then move into data types, iterators, post/pre fix operators, functions, returning a value, classes should really be the last thing on the list.
I was going to show "extended classes" but i am so tired :D
Sorry for that mistake :p i will do my best tomorrow to add everything.
 
Well if we are going to discuss classes what about static classes?
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>   
   </head>

   <body>
   <?php
        class a{
         
            public static function hi(){
                return 'Hello there';
            }
        }
   ?>
   
      <?php
         echo a::hi();
      ?>

   </body>
</html>
Code:
Hello there

And the differences between this & self.

Of course there is inheritance as well, but its best to start out with the basics, like what variables are, what an array is, then move into data types, iterators, post/pre fix operators, functions, returning a value, classes should really be the last thing on the list.
What about anonymous classes? :D (PHP 7.0+)
PHP:
class Account
{
    protected $account = [
        'id'    => 1,
        'name'  => 902132,
        'email' => '[email protected]'
    ];

    public function players()
    {
        return new class() extends Account {
            public function all()
            {
                // Get all players that belongs to account.
                return $this->account['id'];
            }
        };
    }
}

$account = new Account;

var_dump($account->players()->all());
 
Back
Top