Archive for February, 2009

 

How DataTypes are Specified in PHP?

PHP Data Types

Every language has different types of variable – and PHP is no exception. Data types specify what kind of data, such as numbers and characters, can be stored and manipulated within a program. The language supports a wide variety of data types, including simple numeric, character, string and Boolean types, and more complex arrays and objects. Here’s a quick list of the basic ones, with examples:

Boolean: The simplest variable type in PHP, a Boolean variable, simply specifies a true or false value.

  • <?php$auth = true;?>
  • Integer: An integer is a plain-vanilla whole number like 75, -95, 2000 or 1.<?php$age = 99;

    ?>

  • Floating-point: A floating-point number is typically a fractional number such as 12.5 or 3.141592653589. Floating point numbers may be specified using either decimal or scientific notation.<?php$temperature = 56.89;

    ?>

  • String: A string is a sequence of characters, like “hello” or “abracadabra”. String values may be enclosed in either double quotes (“”) or single quotes(”). (Quotation marks within the string itself can be “escaped” with a backslash (\) character.) String values enclosed in double quotes are automatically parsed for special characters and variable names; if these are found, they are replaced with the appropriate value. Here’s an example:<?php$identity = ‘James Bond’;
    $car = ‘BMW’;

    // this would contain the string “James Bond drives a BMW”
    $sentence = “$identity drives a $car”;
    echo $sentence;

    ?>


In addition to the four core data types, there are four other special types:

  • Null
  • Array
  • Object
  • Resources
 
 
 

Types of Operators in PHP

There are Following types of operators in PHP

-Arithmetic Operators – For Arithmetic Calculation

-Assignment Operator – For Assigning Values

-Comparison Operators-For Comparing values

- Logical Operators – For decision Making

Arithmetic Operators


Operator

Description

Example

Result

+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
Decrement x=5
x–
x=4

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 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=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==5 || y==5) returns false

! not

x=6
y=3

!(x==y) returns true

 
 
 

How to Assign Values to Varaible in PHP ?

A variable can be thought of as a programming construct used to store both numeric and non-numeric data; the contents of a variable can be altered during program execution. Finally, variables can be compared with each other, and you – the programmer – can write code that performs specific actions on the basis of this comparison.

PHP supports a number of different variable types: integers, floating point numbers, strings and arrays. In many languages, it’s essential to specify the variable type before using it: for example, a variable may need to be specified as type integer or type array.

Every variable has a name. In PHP, a variable name is preceded by a dollar ($) symbol and must begin with a letter or underscore, optionally followed by more letters, numbers and/or underscores. For example, $popeye, $one and $INCOME are all valid PHP variable names, while $123 and $48hrs are invalid.

Note that variable names in PHP are case sensitive, so $me is different from $Me or $ME.

Here’s a simple example that demonstrates PHP’s variables:

<html>
<head></head>
<body>

Agent: So who do you think you are, anyhow?
<br />

<?php
// define variables
$name = ‘Neo’;
$rank = ‘Anomaly’;
$serialNumber = 1;

// print output
echo “Neo: I am <b>$name</b>, the <b>$rank</b>. You can call me by my serial number, <b>$serialNumber</b>.”;
?>

</body>
</html>

Here, the variables $name, $rank and $serialNumber are first defined with string and numeric values, and then substituted in the echo() function call. The echo() function, along with the print() function, is commonly used to print data to the standard output device (here, the browser). Notice that I’ve included HTML tags within the call to echo(), and those have been rendered by the browser in its output. You can do this too. Really.

Asssing Values to Varaible
To assign a value to a variable, you use the assignment operator: the = symbol. This is used to assign a value (the right side of the equation) to a variable (the left side). The value being assigned need not always be fixed; it could also be another variable, an expression, or even an expression involving other variables, as below:

<?php

$age = $dob + 15;

?>

Interestingly, you can also perform more than one assignment at a time. Consider the following example, which assigns three variables the same value simultaneously:

<?php

$angle1 = $angle2 = $angle3 = 60;

?>