Php
PHP Hypertext Preprocessor (PHP) is an HTML-embedded server side scripting language. It has become the most common programming language used by developers for web applications. PHP can be installed using your respective package manager with these commands:
- Debian/Ubuntu
apt-get install php5-cli
- Arch Linux
sudo pacman -S php-apache
Basic Formatting
All PHP code must be wrapped in the following:
<syntaxhighlight lang="php"> <?php
?><\syntaxhighlight> |
Here is an example of a basic hello world program using PHP and HTML:
<syntaxhighlight lang="php"> |
PHP is not whitespace driven. Your spacing between tags, instructions, ect. does not affect the flow of the program.
Variables
A variable is how you store a value such as a string or integer. An example where a variable would be used is if you wanted to handle a string multiple times throughout your application. Here is an example of a variable and how it can be used:
<syntaxhighlight lang="php"> <?php $variable = "Blackhat Academy"; $variable1 = "Rules!"; echo $variable . $variable1 ?></syntaxhighlight> |
This snippet of code will display the following: Blackhat Academy Rules!
Strings
Strings have already been used within this page. A string is a series of characters. PHP supports a 256-character set. Strings can be stored in variables or standalone within an instruction.
The following is an example of a string stored in a variable and a string used with the echo function.
<syntaxhighlight lang="php"> <?php $variable = "I love Blackhat Academy!\n"; echo $variable; echo "I love BHA!"; ?></syntaxhighlight> |
\n is used for a newline. This is called an escaped character and is only used within double quotations ("").
- List of escaped characters:
\n - newline \r - carriage return \t - tab \$ - dollar sign \"" - double quote
If you do not want to interpret escaped characters, wrap your strings in single quotations (). However if you want to use a single quote within a string you have to escape the quote with a backslash
example:
<syntaxhighlight lang="php"> $string = 'I\'m a p |
Operators
Operators are used to compare variables, mathematics, and more. For example, you can set a variable equal to another variable using the '=' operator.
- List of operators:
+ - Addition - - Subtraction * - Multiplication / - Division % - Modulus
<syntaxhighlight lang="php"> $add = 1 + 1; $subtract = 5 - 2; $multiply = 6 * 5; $divide = 14/7 modulis = 5 % 2; echo "1 + 1 = ".$add." |
Comparison operators are used to evaluate true or false when comparing variables and/or values
- List of comparison operators:
== - Equal To != - Not Equal To < - Less than > - Greater than <= - Less Than or Equal To >= - Greater Than or Equal To