Our Sponsors

Php Basics

Table of Contents.

Syntax of php

The php code block starts with <?php and ends with ?>. In some some servers when shorhand is enables one can start the php code with <? but for compatibility its always recommended to start a php block of scripts with <?php

<?php
echo "Hello World";
?>

 The php files contains normal html tags like and php code comes in between. Here is an example of php file.

<html>
<head>
</head>
<body>
<?php
$title = $_GET["title"];
echo $title;
?>
</body>
</html>


  Each of the php statement ends with a semicolon ; . It indicates the end of an instruction.

Return To Top

Echo and Print

  The two commands "echo" and "print" are used to print or output strings. Both echo and print are used to output strings but they have some basic differences. It is convinient to out put html codes using 'print' since the string is enclosed in single quotes '. as shown below

print 'Hello World';

But if you use echo to print html code you need to escape the double quotes since the strings are enclosed by " or double quotes.

echo "<a href=\"http://www.google.com\">Google</a>";

the same code can be outputted using print as

print '<a href="http://google.com">Google</>';

But if you are calling and printing values of variables you need to use echo which is more convinient. The following example illustrates how easy it is to print values from variables using echo. You can to concatenate strings in case of print statement using '.'

<?php
$title = "Hello. THis is a php text";
echo "Value of  variable title = $title";
print 'Value of  variable title ='.$title;
?php>


Return To Top

Variables in Php

You can store data,strings,numbers and arrays in variables.
You can access these values again and again over all your script and change the values.
variables in php starts with $ sign.
You declare a varialble like

$variable = values;

There is no need to declare the type of variables in php.
It converts the data according to how it is declared.
The below example describes how variables are declared in php

<?php
$string-a = "how are you?";
$numbers = 80;
?>


Rules for Naming the Variables

  • The variable name can only contain alphabets,numbers and underscore. It can contain a-z,A-Z,0-9 and _.
  • The variable name must start with an alphabet or _
  • The variable name cannot contain any spaces and if there are two words in string one must use _ to connect them like $my_variable

Return To Top

Operators in Php

Operators in php are used to perform arithmetic and logical operations on variables.
Here are the operators listed by groups

Arithmetic Operators

Operator Description Example Output
+ Addition $x=2
$x+3
5
- Subraction $x=5
$x-3
2
* Multiplication $x=5
$x*2
10
/ division $x=10
$x/2
5
% Remainder in Division or Modulu $x=15
$x%14
1
-- Decrement $x=5
$x--
x = 4
++ Increment $x=1
$x++
x = 2

Logical Operators
Operator Description Example Output
&& and x=5
y=8
x<6&&y>7
returns True
|| or x=5 , y =8

(x<6||y<6)
Returns true
(x==6||y==9)
Returns False
! Not x=5
y=5
!(x==y)
Returns false

Comparision Operators
Operator Description Example Output
> Greater than x=5
y=6
x>y Returns false
< less than x=6
y=5
x<y Returns false
== is equal to x= 6
y = 6
(x==y) Returns true
>= greater than or equal to x=5
y=6
x>=y returns false
<= Less Than or equal to x=6
y=9
x<=y returns true
!= is not equal to x=5
y=6
x!=y returns true

Assignment Operators
These operators are used to assing values or change values of a variable.
Operator Description Example
+= x+=y   increments x value by y x+=5. Increses x by 5
-= x-= y decreases x value by y x-=5 decreases x by 5
*= x*=y multiplies x with y x*=5 now x value is 5x
/= x/=y divides x by y x/=5 now x values is x/5
%= x%=y x value is remainder of division x/y x%=5 x values is remainder of division x/5
= x=y x values is same as y x=5 x values is 5
.= Concatenates strings.
x.=y


Return To Top