Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "Php"

From NetSec
Jump to: navigation, search
(Operators: same)
(Redirected page to PHP)
 
Line 1: Line 1:
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:
+
#REDIRECT [[PHP]]
 
+
*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:
+
 
+
{{code|text=<syntaxhighlight lang="php">
+
<?php
+
# Code goes here
+
?></syntaxhighlight>}}
+
 
+
Here is an example of a basic hello world program using PHP and HTML:
+
{{code|text=<syntaxhighlight lang="php">
+
<html>
+
<head>
+
<title> My First PHP Program <title>
+
</head>
+
<body>
+
<?php
+
echo "Blackhat Academy Rules!";
+
?>
+
</body>
+
</html>
+
</syntaxhighlight>}}
+
 
+
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:
+
 
+
{{code|text=<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.
+
 
+
{{code|text=<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:
+
{{code|text=<syntaxhighlight lang="php">
+
$string = 'I\'m a p|4n3t h4x0r!';
+
</syntaxhighlight>}}
+
 
+
== 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
+
 
+
{{code|text=<syntaxhighlight lang="php">
+
$add = 1 + 1;
+
$subtract = 5 - 2;
+
$multiply = 6 * 5;
+
$divide = 14/7
+
modulis = 5 % 2;
+
 
+
echo "1 + 1 = ".$add."<br/>";
+
echo "5 - 2 =".$subtract."<br/>";
+
echo "6 * 5 =".$multiply."<br/>";
+
echo "14 / 7 =".$divide."<br/>";
+
echo "5 % 2 =".$modulus;
+
</syntaxhighlight>}}
+
 
+
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
+

Latest revision as of 06:58, 5 June 2012

Redirect to: