What is PHP?
PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
PHP is an open source software
PHP is free to download and use
What is a PHP File?
PHP files can contain text, HTML tags and scripts
PHP files are returned to the browser as plain HTML
PHP files have a file extension of ".php", ".php3", or ".phtml"
Where to Start?
Install Apache (or IIS) on your own server, install PHP, and MySQL
Or find a web hosting plan with PHP and MySQL support
PHP Syntax
PHP Syntax
A PHP script always starts with <?php and ends with ?>. A PHP script can be placed anywhere in the document. On servers with shorthand-support, you can start a PHP script with <? and end with ?>. For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.
<?php
?>
A PHP file must have a .php extension.
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP script that sends the text "Hello USER" back to the browser:
<html>
<body>
<?php
echo "Hello USER";
?>
</body>
</html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another. There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello USER".
Comments in PHP
In PHP, we use // to make a one-line comment or /* and */ to make a comment block:
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
PHP Variables
Do You Remember Algebra From School?
Do you remember algebra from school? x=5, y=6, z=x+y
Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11?
These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).
PHP Variables
As with algebra, PHP variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carName.
Rules for PHP variable names:
Variables in PHP starts with a $ sign, followed by the name of the variable
The variable name must begin with a letter or the underscore character
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
A variable name should not contain spaces
Variable names are case sensitive (y and Y are two different variables)
Creating (Declaring) PHP Variables
PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
$myBike="CBR1000";
After the execution of the statement above, the variable myBike will hold the value CBR1000.
Tip: If you want to create a variable without assigning it a value, then you assign it the value of null.
Let's create a variable containing a string, and a variable containing a number:
<?php
$txt="Hello USER!";
$x=24;
?>
Note: When you assign a text value to a variable, put quotes around the value.
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
PHP Variable Scope
The scope of a variable is the portion of the script in which the variable can be referenced.
PHP has four different variable scopes:
local
global
static
parameter
Local Scope
A variable declared within a PHP function is local and can only be accessed within that function. (the variable has local scope):
<?php
$a = 8; // global scope
function Test()
{
echo $a; // local scope
}
Test();
?>
The script above will not produce any output because the echo statement refers to the local scope variable $a, which has not been assigned a value within this scope.
You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
Local variables are deleted as soon as the function is completed.
Global Scope
Global scope refers to any variable that is defined outside of any function.
Global variables can be accessed from any part of the script that is not inside a function.
To access a global variable from within a function, use the global keyword:
<?php
$a = 10;
$b = 20;
function Test()
{
global $a, $b;
$b = $a + $b;
}
Test();
echo $b;
?>
The script above will output 30.
PHP also stores all global variables in an array called $GLOBALS[index]. Its index is the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
The example above can be rewritten as this:
<?php
$a = 10;
$b = 20;
function Test()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Test();
echo $b;
?>
Static Scope
hen a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted.
To do this, use the static keyword when you first declare the variable:
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function.
static $rememberMe;
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Parameters
A parameter is a local variable whose value is passed to the function by the calling code.
Parameters are declared in a parameter list as part of the function declaration:
function myTest($para1,$para2,...)
{
// function code
}
Parameters are also called arguments. We will discuss them in more detail when we talk about functions.
PHP Arrays
What is an Array?
A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.
An array is a special variable, which can store multiple values in one single variable.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
$cars1="Audi";
$cars2="M.Benz";
$cars3="BMW";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The best solution here is to use an array!
An array can hold all your variable values under a single name. And you can access the values by referring to the array name.
Each element in the array has its own index so that it can be easily accessed.
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays
Numeric Arrays
A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In the following example the index are automatically assigned (the index starts at 0):
$cars=array("Audi","M.benz","BMW","Toyota");
2. In the following example we assign the index manually:
In the following example you access the variable values by referring to the array name and index:
<?php
$cars[0]="Audi";
$cars[1]="M.benz";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Sam's cars.";
?>
The code above will output:
Audi and M.benz are Sam's cars.
Associative Arrays
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.
EXAMPLE 1:
In this example we use an array to assign ages to the different persons:
Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.
EXAMPLE:
In this example we create a multidimensional array, with automatically assigned ID keys:
Check Out http://modpe.enjin.com for modpe scripts , modpe help , modpe functions , modpe tutorials ! Everythimg ModPe ! Have fun share !! Hit That green button if I helped !
?> can be omittedecho?I thought echo is only for html not pocketmine
Rollback Post to RevisionRollBack
I can be found on Freenode IRC channels #pocketmine, #ModPEScripts, #LegendOfMCPE, #pmplugins or #BeaconMine.
I am a PocketMine-MP plugin developer. I hate it when people think that I love stupid admin positions. Being an admin is nothing compared to being a plugin developer.
I am also a main developer of BlockServer, a work-in-progress MCPE server software. You are welcome to download it, but it so far onlly spawns you in the upther (above the world). You can chat, though.
I do not own this server but I just love to put this banner here:
I can be found on Freenode IRC channels #pocketmine, #ModPEScripts, #LegendOfMCPE, #pmplugins or #BeaconMine.
I am a PocketMine-MP plugin developer. I hate it when people think that I love stupid admin positions. Being an admin is nothing compared to being a plugin developer.
I am also a main developer of BlockServer, a work-in-progress MCPE server software. You are welcome to download it, but it so far onlly spawns you in the upther (above the world). You can chat, though.
I do not own this server but I just love to put this banner here:
No, echo is for PHP. There aren't really any major functions in HTML, it's just kinda creating text and input boxes and stuff like that. echo = creates some text in the document. In HTML, to create some text, you just simply write the text.And, these tutorials are not even for PocketMine. This is for web. And most of this was just copied from the PHP website.
Check Out http://modpe.enjin.com for modpe scripts , modpe help , modpe functions , modpe tutorials ! Everythimg ModPe ! Have fun share !! Hit That green button if I helped !
No, echo is for PHP. There aren't really any major functions in HTML, it's just kinda creating text and input boxes and stuff like that. echo = creates some text in the document. In HTML, to create some text, you just simply write the text.
And, these tutorials are not even for PocketMine. This is for web. And most of this was just copied from the PHP website.
What website? Doesn't seem to be from php.net
P.S. Click here
Rollback Post to RevisionRollBack
I can be found on Freenode IRC channels #pocketmine, #ModPEScripts, #LegendOfMCPE, #pmplugins or #BeaconMine.
I am a PocketMine-MP plugin developer. I hate it when people think that I love stupid admin positions. Being an admin is nothing compared to being a plugin developer.
I am also a main developer of BlockServer, a work-in-progress MCPE server software. You are welcome to download it, but it so far onlly spawns you in the upther (above the world). You can chat, though.
I do not own this server but I just love to put this banner here:
What is PHP?
PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
PHP is an open source software
PHP is free to download and use
What is a PHP File?
PHP files can contain text, HTML tags and scripts
PHP files are returned to the browser as plain HTML
PHP files have a file extension of ".php", ".php3", or ".phtml"
Where to Start?
Install Apache (or IIS) on your own server, install PHP, and MySQL
Or find a web hosting plan with PHP and MySQL support
PHP Syntax
PHP Syntax
A PHP script always starts with <?php and ends with ?>. A PHP script can be placed anywhere in the document. On servers with shorthand-support, you can start a PHP script with <? and end with ?>. For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.
<?php
?>
A PHP file must have a .php extension.
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP script that sends the text "Hello USER" back to the browser:
<html>
<body>
<?php
echo "Hello USER";
?>
</body>
</html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another. There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello USER".
Comments in PHP
In PHP, we use // to make a one-line comment or /* and */ to make a comment block:
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
PHP Variables
Do You Remember Algebra From School?
Do you remember algebra from school? x=5, y=6, z=x+y
Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11?
These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).
PHP Variables
As with algebra, PHP variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carName.
Rules for PHP variable names:
Variables in PHP starts with a $ sign, followed by the name of the variable
The variable name must begin with a letter or the underscore character
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
A variable name should not contain spaces
Variable names are case sensitive (y and Y are two different variables)
Creating (Declaring) PHP Variables
PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
$myBike="CBR1000";
After the execution of the statement above, the variable myBike will hold the value CBR1000.
Tip: If you want to create a variable without assigning it a value, then you assign it the value of null.
Let's create a variable containing a string, and a variable containing a number:
<?php
$txt="Hello USER!";
$x=24;
?>
Note: When you assign a text value to a variable, put quotes around the value.
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
PHP Variable Scope
The scope of a variable is the portion of the script in which the variable can be referenced.
PHP has four different variable scopes:
local
global
static
parameter
Local Scope
A variable declared within a PHP function is local and can only be accessed within that function. (the variable has local scope):
<?php
$a = 8; // global scope
function Test()
{
echo $a; // local scope
}
Test();
?>
The script above will not produce any output because the echo statement refers to the local scope variable $a, which has not been assigned a value within this scope.
You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
Local variables are deleted as soon as the function is completed.
Global Scope
Global scope refers to any variable that is defined outside of any function.
Global variables can be accessed from any part of the script that is not inside a function.
To access a global variable from within a function, use the global keyword:
<?php
$a = 10;
$b = 20;
function Test()
{
global $a, $b;
$b = $a + $b;
}
Test();
echo $b;
?>
The script above will output 30.
PHP also stores all global variables in an array called $GLOBALS[index]. Its index is the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
The example above can be rewritten as this:
<?php
$a = 10;
$b = 20;
function Test()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Test();
echo $b;
?>
Static Scope
hen a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted.
To do this, use the static keyword when you first declare the variable:
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function.
static $rememberMe;
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Parameters
A parameter is a local variable whose value is passed to the function by the calling code.
Parameters are declared in a parameter list as part of the function declaration:
function myTest($para1,$para2,...)
{
// function code
}
Parameters are also called arguments. We will discuss them in more detail when we talk about functions.
PHP Arrays
What is an Array?
A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.
An array is a special variable, which can store multiple values in one single variable.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
$cars1="Audi";
$cars2="M.Benz";
$cars3="BMW";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The best solution here is to use an array!
An array can hold all your variable values under a single name. And you can access the values by referring to the array name.
Each element in the array has its own index so that it can be easily accessed.
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays
Numeric Arrays
A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In the following example the index are automatically assigned (the index starts at 0):
$cars=array("Audi","M.benz","BMW","Toyota");
2. In the following example we assign the index manually:
$cars[0]="Audi";
$cars[1]="M.benz";
$cars[2]="BMW";
$cars[3]="Toyota";
EXAMPLE:
In the following example you access the variable values by referring to the array name and index:
<?php
$cars[0]="Audi";
$cars[1]="M.benz";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Sam's cars.";
?>
The code above will output:
Audi and M.benz are Sam's cars.
Associative Arrays
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.
EXAMPLE 1:
In this example we use an array to assign ages to the different persons:
$ages = array("Sam"=>32, "John"=>30, "Devid"=>34);
EXAMPLE 2:
This example is the same as example 1, but shows a different way of creating the array:
$ages['Sam'] = "32";
$ages['John'] = "30";
$ages['Devid'] = "34";
The ID keys can be used in a script:
<?php
$ages['Sam'] = "32";
$ages['John'] = "30";
$ages['Devid'] = "34";
echo "Sam is " . $ages['Sam'] . " years old.";
?>
The code above will output:
Sam is 32 years old.
Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.
EXAMPLE:
In this example we create a multidimensional array, with automatically assigned ID keys:
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
The array above would look like this if written to the output:
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
EXAMPLE
Lets try displaying a single value from the array above:
echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";
The code above will output:
Is Megan a part of the Griffin family?
More will be added soon ! Hit that green button if this helper
?> can be omittedecho?I thought echo is only for html not pocketmine
Haha
Willing to work on Android apps or multi-platform games, see profile bio for information.
Founder and CEO of Temena Development
Twitter: @TemenaPE
Instagram: @ItsLiterallyMath
P.S. Click here