Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, 7 September 2011

PHP Tutorial Ch-04 PHP Operators

Next Chapter

CHAPTER-4 PHP Operators

PHP supports the following operators. Their usage is just like in any other programming language like C, C++ etc.

Arithmetic operators:-
Operator Description Example
+ Addition 9+5=14
- Subtraction 9-5=4
* Multiplication 9*5=45
/ Division 10/5=2
% Modulus(Remainder of a/b) 9%5=4
++ Increment i=4; i++ which results in i=5
-- Decrement i=4; i-- which results in i=3

Assignment Operators:-
Operator Example
= $val=5;
+= $val+=6; which results in $val=11(5+6)
-= $val-=3; which results in $val=2(5-3)
*= Similar to += and -=
/= Similar to += and -=
.= $str=”ab”; $str.=”cd”; results in $str=’abcd”
%= Similar to += and -=

Comparison Operators:-
Operator Description Example
== Equal To 5==5 is true and 5==3 is false
!= Not Equal To 5!=5 is false and 5!=3 is true
<> Not Equal To 5<>5 is false and 5<>3 is true
> Greater Than 5>3 is true and 3>5 is false
< Less Than 5<3 is false and 3<5 is true
>= Greater Than or Equal To 5>=5 is true and 5>5 is false
<= Less Than or Equal To 5<=5 is true and 5<5 is false

Logical Operators:-
Operator Description Example
&& AND true && false is false
|| OR true || false is true
! NOT NOT true is false

I am not presenting a separate example to show the use of the above mentioned operators. Their usage is pretty much obvious. But, if you don’t understand any one of them then don’t worry about it now. You will understand as you go on with the tutorial.

Next Chapter

PHP Tutorial Ch-03 Arrays in PHP



CHAPTER-3 Arrays in PHP
Arrays in PHP can be declared using any of the following ways-
  • $arrayVar=array(‘string1’, ’string2’, ’string3’);
This is a simple array declaration where we pass the array elements to the array function’s parameters. The keys of an array declared like this would start from 0. You can access the array elements as-
$arrayVar[0];
$arrayVar[1];
$arrayVar[2];

  • $arrayVar[0]=’string1’;  $arrayVar[1]=’string2’;  $arrayVar[2]=’string3’;
In this array declaration we assign the array keys manually. The indexes maybe of any primitive datatype. That is we can even have string values as the array indexes. For example-
$arrayVar[‘FirstName’]=”userName”;
$arrayVar[‘LastName’]=”lastUserName”;
is absolutely valid in PHP.

  • $arrayVar=array(‘FirstName’=>’userName’, ‘LastName’=>’lastUserName’);
The above array declaration is associative. In the array function we provide both the array key and array element. The general syntax is-
array(keyname=>element, keyname=>element, ……..);
An array declared like this will be used just like in the previous example. In fact, this example and the previous example do the same thing. In both the examples the array elements will be accessed as-
$arrayVar[‘FirstName’];
$arrayVar[‘LastName’];

Friday, 2 September 2011

PHP Tutorial Ch-02 Variables in PHP

CHAPTER-2 Variables in PHP

Consider the following script (Just look at it. Don’t copy it, from here on I advice that you read the programs, understand them and then try it on your own):-

<html>
<head>
<title>Variables Trial</title>
</head>
<body>
<?php
$var=10;
echo "Value of variable var is = ".$var;
?>
</body>
</html>

The above PHP code has 2 lines. In the first line we define a new variable var with the value 10.

In PHP, variables are declared by prefixing the variable name with a $ sign. You do not need to specify the type of variable. It is automatically set according to the type of value that is stored in it. In the above code variable $var stores a value 10 which is of integer type. So, variable $var is an integer variable. We may even assign $var a value other than integer type. Consider this code-

PHP Tutorial Ch-01 First PHP Program


CHAPTER-1 First PHP program
Although PHP scripts can be written on any text editor (not on MS WORD or similar software) I would recommend that you get a good IDE for it like Netbeans.

clip_image002
Netbeans IDE

So here’s a simple HELLO WORLD program below.
<html>
<head>
<title>
HELLO WORLD program
</title>
</head>
<body>
<?php
echo "HELLO WORLD!"; //To display text on screen
?>
</body>
</html>

Copy and paste the above code into your text editor/IDE and save it as helloWorld.php in the directory /wamp/www.

PHP Tutorial- Setting Up PHP

SETTING UP PHP:-
To run php you will need the following softwares-
  • PHP 5.3(or the latest version available on php.net)
  • Web Server (like apache, IIS etc.)
  • MySQL server(optional)
You could either install each one of the above mentioned software separately and then manually set each one of them separately or you could simply install a software called WAMP(for Windows)/LAMP(for Linux) which includes all of the above softwares.

Installing WAMP (for Windows)/LAMP (for Linux distros like Ubuntu, Fedora, Knoppix etc.)
WAMP is a combined package that includes Apache Web server, MySQL Server and PHP in addition to some utility softwares for MySQL. It is recommended that you straight away just install WAMP/LAMP to set up your computer for PHP and MySQL. Here’s a simple brief guide for installation of WAMP/LAMP on Windows/Linux. If you are not able to follow the given steps then just search for it on the internet. Its very easy so you won’t really need to refer to some other place. But, just in case you are not able to install WAMP/LAMP just search for it on the net and don’t give up.