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

OperatorDescriptionExampleResult
+Additionx=7;
x+2;
x=9
-Subtractionx=7;
x-2;
x=5
*Multiplicationx=7;
x*2;
x=14
/Divisionx=7;
x/2;
x=3.5
%Modulus (remainder division)x=7;
x%2;
x=1
++Incrementx=7;
x++;
x=8
--Decrementx=7;
x--;
x=6

Assignment

OperatorEquivalent StatementExampleResult
=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

OperatorDescriptionExampleResult
==is equal to7==6false
!=is not equal to7!=6true
<>is not equal to7<>6true
>is greater than7>6true
<is less than7<6false
>=is greater than or equal to7>=6true
<=is less than or equal to7<=6false

Logical

OperatorDescriptionExampleResult
&&ANDx=7;
y=3;
(x>y && y<3);
false
||ORx=7;
y=3;
(x>y || y<3);
true
!NOTx=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.

Next>>

Comment on this page:

Your information Name:
Email:
Hide Email?
Enter the 5 characters shown.
Request another image.
Prove your human:
Comments Overall page rating:

Comments

No one has commented on this lesson yet. You can be the first!