Papers Home visit my blog at http://prasannatechdotcom.blogspot.com Contact Us
 
  Your Ad Here
Your Ad Here
PHP Tutorial: Lesson - 2

In the previous lesson 1, we have discussed about the introduction of PHP, how to use comments and about variables. In this lesson we will discuss about string variable, operations on string, various string functions,  arithmetic, comparison and logical operators. Those who have good programming languages such as C, C++, Java will be able to understand the concepts easily as most of the below explanation is similar to the concepts in the above languages

String variable: A string variable contains set of characters. The string variable is assigned with double quotes as mentioned in the below example

<?php $str="Hello, World"; echo $str; ?>

In the above example $str is a string variable and the string "Hello, World" is assigned to $str variable. When the above script is executed, the output of the script will be "Hello, World"

The concatenation operator "." can be used to add two strings. For example

<?php
$str1="Hello, ";
$str2="World!";
echo $txt1 . " " . $txt2;
?>

The above script displays Hello, World!

For C Programmers who are familiar with C functions such as strlen(), strpos() etc.. can be used in PHP string operations. These functions are implemented for availability in PHP Library. Please see the complete strings function details in the help of PHP.

Arithmetic operators:

Operator Description Example Result
+ Addition $x=10
$x = $x + 20
$x = 30
- Subtraction $x = 5
$x = $x - 3
$x = 2
* Multiplication $x = 5 * 10 $x = 50
/ Division $x = 10 / 2 $x = 5
% Modulus (division remainder) 19%2 1
10%8 2
20%2 0
++ Increment $x=10 x=6
$x++
-- Decrement x=10 x=9
x--

Assignment operators

Operator Example Is similar to
= 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 1==2 returns false
!= is not equal 1!=2 returns true
<> is not equal 1<>2 returns true
> is greater than 1>2 returns false
< is less than 1<2 returns true
>= is greater than or equal to 1>=2 returns false
<= is less than or equal to 1<=2 returns true

logical operators:

Operator Description Example
&& and $x=1
$y=2
($x < 10 && $y > 1) returns true
|| or $x=10
$y=1
($x==5 || $y==5) returns false
! not $x=10
$y=1
!($x==$y) returns true

Do you feel above operators and its usage is familiar with you!

In the next lesson 3 we will discuss about Control statements in PHP

Send your feedback to feedback@prasannatech.com with Subject: PHP Tutorial

 
  Your Ad Here
 Home Checkers  Checks(Jigsaw)   NetCheckers NetChineseCheckers  NetRummy WordScramble  SnakesLadders
 CardMemory Knight's Tour Tower of Hanoi  NetBackgammon  Network Morris Snakes  Nursery Tutor NetChess
Chat ftpClient NetTicTacToe Play Fupa Online Games     Links Papers
 
Your Ad Here

Software developed by me are freeware and you are not required to pay any fees. However, if you like and appreciate, you can show your support by making a donation. It will help me to continue to develop new software, maintain already developed software and this web site.

Copyright (C), All rights reserved-2008
No warranty or guarantee not limited to these are provided with above free software. Send your comments or feedback
to  feedback@prasannatech.com. For advertising contact advertise@prasannatech.com