Hi,

We are going to learn php syntax and variables on this article.

We have talked about php syntax on the first php article, the syntax looks like c,c++,java and if you know one of these languages’ syntax it will be easy to learn php syntax.

Php is a scripting language and we must say to its interpreter where the php code begins and where it ends. We use to tell the php interpreter it is a php script and it should interprets the script among these tags.


//: Double slashes means this line will not be interpreted by the interpreter, this line wont run , it is used for comment lines. Comment lines may be used to add extra information about the code, adding todo item, informing your colleagues about the code, etc.

Before starting php syntax again we should look at what are variables and how can we define them. Variable stores data in it, variable name is a symbolic name associated with a value. Php has different variable types but php dont use them to define variables’ types. Php knows type of variable according to its value.If you assign a string to a variable then you may assign an integer and again you may assign an object to the variable.

If you try to do this on C++ you have to use type casting. Php is a loosely typed language because of this feature.


Php variables have $ sign in front of them. The second character of the variable i mean the character after $ sign must be a letter or underscore. Variable stores data so we should know how to assign data to a variable, we assign data to a variable by = (equals) sign. Equals assigns from right to the left, what does it mean? It means = (equals) gets the value from its right and puts it into its left. Php uses ; sign to assign values,references to variables, run functions.

Php uses { } bracets for code blocks, code block is a piece of code which does something (multiply two numbers, checking if user has logged in etc), we write code inside { and } tags in code blocks. Code blocks are used in comparisons, functions, class methods, class declarations.

In programming languages there are operators for making some processes on variables,functions,objects, like multiplying two numbers, assigning an object as a reference, comparing two strings etc. The next article describes php operators so please try to understand the operators which are located below;

Arithmetic Operators

Operator Description Example Result
+ Addition x=2
x+2
4
Subtraction x=2
6-x
4
* Multiplication x=4
x*3
12
/ Division 8/2
16/4
4
4
% Modulus (division remainder) 7%2
10%8
10%2
1
2
0
++ Increment x=7
x++
x=8
Decrement x=7
x–
x=6

Assignment Operators

Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

Comparison Operators

Operator Description Example
== is equal to 4==8 returns false
!= is not equal 4!=8 returns true
<> is not equal 4<>8 returns true
> is greater than 4>8 returns false
< is less than 4<8 returns true
>= is greater than or equal to 4>=8 returns false
<= is less than or equal to 4<=8 returns true

Logical Operators

Operator Description Example
&& and x=6
y=3

(x < 10 && y > 1) returns true

|| or x=6
y=3

(x==4 || y==4) returns false

! not x=6
y=3

!(x==y) returns true

There are also different operators in php we try to explain them in our next article.