tools:php
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tools:php [2024/06/03 03:33] – [Switch] Humphrey Boa-Gart | tools:php [2025/10/12 10:53] (current) – Humphrey Boa-Gart | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | {{wst> | + | # |
- | + | ||
- | ====== PHP ====== | + | |
- | + | ||
- | PHP is a server side language, meaning you can't see the source code in your browser, because all of the stuff is done on the server. To start, get some web hosting (easy) or start your own server (not as easy), and create a .php file. Having a decent knowledge of [[tools:html|HTML]] is highly recommended. If you've programmed in another web language such as ASP, or have knowledge of application | + | |
- | + | ||
- | ===== HELLO WORLD ===== | + | |
- | + | ||
- | Yeah... This is the ancient tradition of beginners starting out in any language. Here you go. | + | |
- | + | ||
- | <?php | + | |
- | echo "LOL HAI THER AMIDOINITRITE?"; | + | |
- | print(" | + | |
- | ?> | + | |
- | + | ||
- | Let's go over some rulez. (The word rules does not have a ' | + | |
- | + | ||
- | All PHP code starts with <?php and ends with ?>. These are called delimiters. echo is a statement that basically prints out whatever is in the double quotes. It isn't a function. (printf() is though.) Since echo is a statement, much like require/ | + | |
- | ==== Hello World, Again (with HTML) ==== | + | |
- | + | ||
- | We saw how echo can output text. But it can also output HTML code and JavaScript. Example here. | + | |
- | + | ||
- | <?php | + | |
- | echo "< | + | |
- | ?> | + | |
- | + | ||
- | This' | + | |
- | ===== Variables ===== | + | |
- | + | ||
- | Now we'll learn a little about variables. PHP is great because, unlike other languages, you don't need to specify what type of variable you are declaring (integer, Boolean, float, string, etc). PHP automatically identifies the type for you. Anyway, here we go. | + | |
- | + | ||
- | <?php | + | |
- | $lolvar = 6; | + | |
- | $loldecimal = 3.12; | + | |
- | $lolstring = "This is a string."; | + | |
- | echo $lolvar; | + | |
- | echo $loldecimal; | + | |
- | echo $lolstring; | + | |
- | ?> | + | |
- | + | ||
- | Some things to note: | + | |
- | + | ||
- | ALL VARIABLES START WITH THE DOLLAR SIGN ($) NO EXCEPTIONS When echoing a variable you don't need the double quotes You can also concatenate, | + | |
- | + | ||
- | <?php | + | |
- | $lol = " | + | |
- | $wut = " | + | |
- | $randomdigit = 2; | + | |
- | echo "Did you just use the meme " . $lol . " " . $wut . " " .$randomdigit . " times?"; | + | |
- | ?> | + | |
- | + | ||
- | This' | + | |
- | ==== Constants ==== | + | |
- | + | ||
- | Constants are like variables, except they do not begin with a dollar sign and cannot be changed or deleted once defined. It is usual practice to name constants in all capitals (much like macros in C/C++). You define constants with the define(); function: | + | |
- | + | ||
- | <?php | + | |
- | define(" | + | |
- | echo MYCONSTANT; | + | |
- | ?> | + | |
- | + | ||
- | This sends out " | + | |
- | ===== Predefined variables ===== | + | |
- | + | ||
- | Always $_, ex: $_COOKIE, $_SERVER, $_GET, $_REQUEST, and $_POST Some can only be used before any output is sent | + | |
- | + | ||
- | <?php | + | |
- | $ip = $_SERVER[' | + | |
- | $ref = $_SERVER[' | + | |
- | echo $ip." Was referred by " | + | |
- | $UserAgent = $_SERVER[HTTP_USER_AGENT]; | + | |
- | $Software = $_SERVER[' | + | |
- | echo "Their useragent was " | + | |
- | echo "Your server software is " | + | |
- | ?> | + | |
- | + | ||
- | Will output something like: | + | |
- | + | ||
- | 0.0.0.0 Was referred by ----- Their useragent was Mozilla/5.0 Your server software is Apache/ | + | |
- | + | ||
- | (Depending on your server environment.) | + | |
- | ==== Types of variables ==== | + | |
- | + | ||
- | The datatype a variable uses is usually implicit, meaning PHP will determine what type of variable it should use based on its contents. However, these are some of the datatypes at your disposal. | + | |
- | + | ||
- | Boolean ex: $a = true; #has only 2 possible values, true or false | + | |
- | Integer ex: $b = 4; #holds whole numbers | + | |
- | String ex: $c = " | + | |
- | Float ex: $d = 3.5252524; #holds numbers with decimals | + | |
- | Array ex: $e = array(5, 3, 1); #holds a list of any other type of data, be it an integer, string, boolean, etc. | + | |
- | Null ex: $f = NULL; #only holds null (nothing) | + | |
- | ===== Operators ===== | + | |
- | + | ||
- | ==== Arithmetic ==== | + | |
- | + | ||
- | + Addition | + | |
- | - Subtraction | + | |
- | * Multiplication | + | |
- | / Division | + | |
- | % Division with remainder | + | |
- | ==== Assignment ==== | + | |
- | + | ||
- | += ex: n+=7, or N equals N+7 | + | |
- | -= | + | |
- | *= | + | |
- | /= | + | |
- | %= | + | |
- | .= | + | |
- | = | + | |
- | ==== Logical ==== | + | |
- | + | ||
- | && , AND ex: if (($a>0) && ($a< | + | |
- | || , OR ex: if (($a==0)||($a==1)){ | + | |
- | ! , NOT ex: if ($a != 2) { | + | |
- | ==== Comparison ==== | + | |
- | + | ||
- | < Less than | + | |
- | > Greater than | + | |
- | == Equal to | + | |
- | != Not equal | + | |
- | === Identical | + | |
- | !== Not identical | + | |
- | <= Less than or equal to | + | |
- | >= Greater than or equal to | + | |
- | ==== Conditional statements and loops ==== | + | |
- | + | ||
- | Now you have a basic understanding of printing things, now it's time to start something else. We will explore loops and conditional statements. Loops will pretty much do something a certain amount of times and conditional statements control what the program does if a condition is fulfilled or not. | + | |
- | ==== Conditional statements ==== | + | |
- | + | ||
- | Well, it's basically just if and switch, but the former is pretty useful. You have to know this one. | + | |
- | ==== If ==== | + | |
- | + | ||
- | This essentially checks whether or not a condition is true. | + | |
- | + | ||
- | <?php | + | |
- | $willdo = true; | + | |
- | if($willdo == true){ | + | |
- | echo " | + | |
- | } | + | |
- | ?> | + | |
- | + | ||
- | An else statement is an addition to an if statement which allows for another condition. If there' | + | |
- | + | ||
- | <?php | + | |
- | $wontdo = false; | + | |
- | if($wontdo == true){ | + | |
- | echo " | + | |
- | } else { | + | |
- | echo " | + | |
- | } | + | |
- | ?> | + | |
- | + | ||
- | Else if statements allow for multiple conditions, if and only if the first condition before it was false. | + | |
- | + | ||
- | <?php | + | |
- | $maydo = 99; | + | |
- | $maynotdo = false; | + | |
- | if($maynotdo == true){ | + | |
- | echo " | + | |
- | } elseif ($maydo == 99){ | + | |
- | echo "did anyway"; | + | |
- | } else { | + | |
- | echo " | + | |
- | } | + | |
- | ?> | + | |
- | + | ||
- | Also: | + | |
- | + | ||
- | - Conditions in parentheses just have to be true. You don't have to use an == or != if it's a boolean value (true/ | + | |
- | - There are logical operators like and (&& | + | |
- | - a ! behind it means it's false, so if $dick is true, !$dick is false, and vice versa if $dick was false. | + | |
- | ==== Switch ==== | + | |
- | + | ||
- | A substitute to a whole bunch of elseifs. It tests if a given value is equal to something. Cases can be any value. There' | + | |
- | + | ||
- | <?php | + | |
- | switch ($value) { | + | |
- | case false: | + | |
- | echo "value is 0"; | + | |
- | | + | |
- | case 17: | + | |
- | echo "value is 1"; | + | |
- | | + | |
- | case " | + | |
- | echo "value is pigdog"; | + | |
- | | + | |
- | case NULL: | + | |
- | echo "value is null"; | + | |
- | | + | |
- | default: | + | |
- | echo "value is something else other than 0, 1, pigdog, or null."; | + | |
- | | + | |
- | } | + | |
- | ?> | + | |
- | ===== Loops ===== | + | |
- | + | ||
- | ==== For loops ==== | + | |
- | + | ||
- | ==== Foreach loops ==== | + | |
- | + | ||
- | ==== While loops ==== | + | |
- | + | ||
- | ==== Do-while loops ==== | + | |
- | + | ||
- | ==== Simple Backdoor in PHP ==== | + | |
- | + | ||
- | ==== Email flooder in PHP ==== | + | |
- | + | ||
- | ===== Image bugs ===== | + | |
- | + | ||
- | {{tag> | + |
tools/php.1717385625.txt.gz · Last modified: (external edit)
Find this page online at: https://bestpoint.institute/tools/php
Find this page online at: https://bestpoint.institute/tools/php