Tuesday, April 7, 2009

Operators in PHP

Just like any other programing language , PHP comes with its own set of operators. they might look similar to behave in same way as any java or c# operator. But we will look into the operators as they might be some variations. There are different types of operators in PHP : 
  1. Arithematic operators
  2. Assignment operators
  3. comparison operators 
  4. Logical operators 
Arithermatic operators : Throughout the history , the first things we learned we learned were alphabets and numbers. Then we have added ages to the life, subtracted the shortcomings ,and multipliedz our experience in life. All these operations in PHP are carried with a rich set of arithematic tools it has to offer. Below is a table which lists the various PHP operators : 

Operator

Description

Example

Result

+

Addition

$iknowjavaAdd=2
$iknowjavaAdd+20

22

-

Subtraction

$iknowjavaSub=29
$50-
iknowjavaAdd

21

*

Multiplication

$iknowjavaMultiply=54

$iknowjavaMultiply*2

108

/

Division

15/5
5/2

3
2.5

%

Modulus (division remainder)

5%2
10%8
10%2

1
2
0

++

Increment

$iknowjavaInc=59

$iknowjavaMultiply++

x=60

--

Decrement

$iknowjavaDec=90

$iknowjavaDec--

x=89


----------------------------------------------------------------------------------------------------------------------------------------
Assignment operators 
----------------------------------------------------------------------------------------------------------------------------------------
Assignment operators : Many times we have looked into assignement operators. This is a new beginning for many. If i want to put a value of 5 in a variable $iknowmyjava, then I will have to find a way to do it. I will use "=" sign . 
Such type of operations are handled by assignment operators. A few PHP operators are listed below : 

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


----------------------------------------------------------------------------------------------------------------------------------------
Comparison operators 
----------------------------------------------------------------------------------------------------------------------------------------

Operator

Description

Example

==

is equal to

5==8 returns false

!=

is not equal

15!=18 returns true

> 

is greater than

15>18 returns false

< 

is less than

51<81>

>=

is greater than or equal to

51>=81 returns false

<=

is less than or equal to

51<=81 returns true


----------------------------------------------------------------------------------------------------------------------------------------
Logical operators 
----------------------------------------------------------------------------------------------------------------------------------------

Operator

Description

E$xample

&&

and

$x=6
$y=3

($x <> 1) returns true

||

or

$x=6
$y=3

($x==5 || $y==5) returns false

!

not

$x=6
$y=3

!($x==$y) returns true

Monday, April 6, 2009

Data Storage in PHP

Data is stored in variables in any programing language. The variables declared in PHP are a bit different than other programing languages. Each variable name starts with "$" sign. If I like to name a variable as iknowPHP variable , it will throw an error. 
The way to declare a variable in PHP is simple though.
$iknowjava = 5;
$iknowmyjava ="Deepak";

Few points : 
  1.  Variable name can start with $ only
  2. Variable name can have only alphabets or numbers or underscore or combinatiion of these three. Spaces in variable names are not permitted
 Once done with the variables , PHP will itself decide on what variable it is , whether its a string variable , a char or a number , depending on the value it has. 

So we will move to next important part in PHP , the various operators.

Writing the first Code

Now we are well set to make the first program. Or the web application. Lets start. 
Go to Location :
C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs 

Or find the folder named htdocs in Apache installation folder. 

Open the text editor / Textpad. 
This can be done by right clicking in any folder --> New --> Text File.

Now write the following code in the the notepad: 

Save this File as iknowPHP.php in the HTMLDOCS folder in Apache. 
Make sure you have not saved it as iknowPHP.php.txt  . 
Run the script . How to do that :

Running the first script: 
Open IE and type :
If the page says  : It works , then we are right into running the first script. Just append the address with iknowPHP.php and hit enter
http://localhost/iknowPHP.php

The script should run. In case the explorer asks to save the file , then please go through installation steps once more for correct installation. 
Do visit www.iknowjava.com for more codes in PHP / JAVA and C#