Home > Tutorials > PHP > Basics Syntax
Basics Syntax
By this point, you should know about HTML and CSS. It's now time to learn about PHP, which stands for PHP: Hypertext Preprocessor. That's right, PHP is a recursively named programming language!
As PHP is a programming language, with MANY built-in functions (whereas HTML and CSS have a limited number of elements and related attributes), this is not so much a tutorial as it is a start-up guide for those with previous programming experience. When you make an HTML file, it's little more than a word file. If you are to create dynamic content, however, you need problem solving skills, and a programmer's mindset, and a simple start-up guide will not help you develop those skills.
With that, let's look at some of the basic syntax used in PHP:
Start/End of File
All PHP should be enclosed in the following:
<?php
<code>
<code>
<code>
?>
End Statement Syntax
After any statement, end it with a semicolon.
Operators
Arithmetic
| Operator | Description | Example | Result |
|---|---|---|---|
| + | Addition | x=7; x+2; | x=9 |
| - | Subtraction | x=7; x-2; | x=5 |
| * | Multiplication | x=7; x*2; | x=14 |
| / | Division | x=7; x/2; | x=3.5 |
| % | Modulus (remainder division) | x=7; x%2; | x=1 |
| ++ | Increment | x=7; x++; | x=8 |
| -- | Decrement | x=7; x--; | x=6 |
Assignment
| Operator | Equivalent Statement | Example | Result |
|---|---|---|---|
| = | x=y; | x=7; | x=7 |
| += | x=x+y; | x=7; x+=2; | x=9 |
| -= | x=x-y; | x=7; x-=2; | x=5 |
| *= | x=x*y; | x=7; x*=2; | x=14 |
| /= | x=x/y; | x=7; x/=7; | x=1 |
| %= | x=x%y; | x=7; x%=3; | x=1 |
| .= | x=x.y; | x="foo"; x.="bar"; | x="foobar" |
Comparison
| Operator | Description | Example | Result |
|---|---|---|---|
| == | is equal to | 7==6 | false |
| != | is not equal to | 7!=6 | true |
| <> | is not equal to | 7<>6 | true |
| > | is greater than | 7>6 | true |
| < | is less than | 7<6 | false |
| >= | is greater than or equal to | 7>=6 | true |
| <= | is less than or equal to | 7<=6 | false |
Logical
| Operator | Description | Example | Result |
|---|---|---|---|
| && | AND | x=7; y=3; (x>y && y<3); | false |
| || | OR | x=7; y=3; (x>y || y<3); | true |
| ! | NOT | x=false; !(x) | true |
Functions
There are over 700 built in functions for PHP. We will get to those later on in the tutorial, but there may come a time when you need to create your own function. A function takes on the following syntax:
function <function name($var1, $var2, $var 3...)>
{
<code>
<code>
<code>
return <return value>;
}
If a function does not contain a return statement (or it is never executed), the null value is returned by default.
Comment on this page:
Comments
No one has commented on this lesson yet. You can be the first!