PHP/Basic Code
From r00tedvw.com wiki
(Difference between revisions)
(→If/Else) |
|||
(13 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
==Basics== | ==Basics== | ||
+ | ;.php | ||
+ | :all PHP pages end with the .php extension, this tells the browser there is php code in the file. | ||
+ | ;// | ||
+ | :comments in PHP are defined with a double-forward slash "//" | ||
+ | :unlike most other comments which have delimiters both before and after comments, PHP does not, the "//" is defined at the beginning of the comment and the rest of the line is then included as part of the comment. | ||
===delimiters - start & stop=== | ===delimiters - start & stop=== | ||
;<?php | ;<?php | ||
Line 9: | Line 14: | ||
:just like with scripting, '''echo''' can be used to output text | :just like with scripting, '''echo''' can be used to output text | ||
<nowiki>ie. | <nowiki>ie. | ||
− | + | <p> | |
− | + | <?php | |
− | + | echo "My first line of PHP!"; | |
− | + | ?> | |
− | + | </p></nowiki> | |
+ | ===Arithmetic=== | ||
+ | php is capable of doing basic arithmetic, take the following for example using "echo" | ||
+ | <nowiki> | ||
+ | <?php | ||
+ | echo 17 * 123; | ||
+ | ?> | ||
+ | Outputs: 2091</nowiki> | ||
+ | |||
+ | ==Operators== | ||
+ | ===concatenation=== | ||
+ | :joins strings together | ||
+ | <nowiki> | ||
+ | <?php | ||
+ | echo "Hello," . " " . "world" . "!"; | ||
+ | ?> | ||
+ | ---- | ||
+ | Outputs: Hello, world!</nowiki> | ||
+ | Each string above is defined between double quotes. The "dots" are between them and combine them together to form 1 long string. | ||
+ | |||
+ | ==Variables== | ||
+ | Variables in php are stored the same way as bash scripting, the variable name being defined by starting with a "$" | ||
+ | <nowiki>ie. | ||
+ | <?php | ||
+ | $myName = "r00tedvw"; | ||
+ | echo $myName; | ||
+ | ?></nowiki> | ||
+ | |||
+ | ==Comparisons== | ||
+ | ;> | ||
+ | :Greater than | ||
+ | ;< | ||
+ | :Less than | ||
+ | ;<= | ||
+ | :Less than or equal to | ||
+ | ;>= | ||
+ | :Greater than or equal to | ||
+ | ;== | ||
+ | Equal to | ||
+ | ;!= | ||
+ | :Not equal to | ||
+ | |||
+ | ==If/Else== | ||
+ | example: | ||
+ | <nowiki> | ||
+ | <?php | ||
+ | if (this condition is true) { | ||
+ | // do this code | ||
+ | } | ||
+ | else { | ||
+ | // do this code instead | ||
+ | } | ||
+ | ?> | ||
+ | |||
+ | <?php | ||
+ | // Write your if/elseif/else statement here! | ||
+ | $myAge = 34; | ||
+ | if ($myAge < 30) { | ||
+ | echo "The condition is true"; | ||
+ | } | ||
+ | else { | ||
+ | echo "The condition is false"; | ||
+ | } | ||
+ | ?> | ||
+ | </nowiki> |
Latest revision as of 19:54, 27 December 2015
Contents |
[edit] Basics
- .php
- all PHP pages end with the .php extension, this tells the browser there is php code in the file.
- //
- comments in PHP are defined with a double-forward slash "//"
- unlike most other comments which have delimiters both before and after comments, PHP does not, the "//" is defined at the beginning of the comment and the rest of the line is then included as part of the comment.
[edit] delimiters - start & stop
- <?php
- all php code begins with this.
- ?>
- all php code ends with this
[edit] output text
- echo
- just like with scripting, echo can be used to output text
ie. <p> <?php echo "My first line of PHP!"; ?> </p>
[edit] Arithmetic
php is capable of doing basic arithmetic, take the following for example using "echo"
<?php echo 17 * 123; ?> Outputs: 2091
[edit] Operators
[edit] concatenation
- joins strings together
<?php echo "Hello," . " " . "world" . "!"; ?> ---- Outputs: Hello, world!
Each string above is defined between double quotes. The "dots" are between them and combine them together to form 1 long string.
[edit] Variables
Variables in php are stored the same way as bash scripting, the variable name being defined by starting with a "$"
ie. <?php $myName = "r00tedvw"; echo $myName; ?>
[edit] Comparisons
- >
- Greater than
- <
- Less than
- <=
- Less than or equal to
- >=
- Greater than or equal to
- ==
Equal to
- !=
- Not equal to
[edit] If/Else
example:
<?php if (this condition is true) { // do this code } else { // do this code instead } ?> <?php // Write your if/elseif/else statement here! $myAge = 34; if ($myAge < 30) { echo "The condition is true"; } else { echo "The condition is false"; } ?>