Data Types And Variables PHP

Introduction

 
A statement is a piece of code that instructs PHP to do something. A statement may compute a value and store it in memory. There are different types of statements in PHP. Function calls, variable assignment, loops, and conditional statements.
 
Simple statement
  1. <?php  
  2.    //an expression statement   
  3.    2+3;  
  4.    //another expression statement  
  5.    print("PHP!");  
  6.    // a control statement   
  7.    if( 3 > 2)  
  8.    {  
  9.      //an assignment statement  
  10.       $a = 3;  
  11.    }  
  12. ?>  
The first statement is the addition of two numbers. It produces the output. The second prints a string to the browser. The third decides whether to execute a block of code based on an expression. PHP includes many types of statements. Some are simple, stand by themselves, and compare well with functions. 
 

Data types

 
PHP has eight diffrent types of values or datatypes. The first five are basic: integers, floating-point, string, booleans, null. Two other are composed of the basic types or composite types. They include arrays and objects. Additionally the resource type denotes a non-native type such as open file or a databse connection.
 
Integers
 
Integers are whole numbers. The range of integers in PHP is equivalent to the range of the long datatype of C language. PHP allows us to write integers in three ways: decimal, octal, hexadecimal. Decimal digits are the ordinary base -10 numbers. Octals are base- 8 numbers consisting of a sequence of digits from 0 to 7 prefixed by a leading zero. Hexa-decimal are 16-base numbers begin with 0x, followed by a sequence of digits or letters from A to F.
 
Floating Numbers
 
Floating point numbers represent numeric values with decimal digits, which are equivalent to the range of the double data type of the C language. It is also called real numbers or doubles. The range and accuracy of real number varies from one platform to another.
 
String
 
Web applications usually move text around more often than they make complex mathematical calculations. PHP intercepts characters inside single quotes as is: Each character between quotes becomes one character in the string. \"- Double quotes,\\- Backslash character, \n- New line, \r- Carriage return, \t- Horizontal tab.
 
Boolean and Null
 
It contains only two values, true and false. The control statements use boolean values to decide whether to execute blocks of code and the comparision operator. 
Null is a special type that stands for the lack of value. It is typically used to initialize and reset variables or to check whether or not a variable is initialized. Null constant is used to unset a variable. 
 

Variables

 
Variable in PHP gives access to memory storage in a part of a computer called RAM, or random access memory. RAM is a volatile memory for storing information. Every use of a variable in PHP begins with $, followed by letters, numbers, or underscores. After the $ the first character must be either a letter or an underscore.
 
Valid Variable assignments
 
$myString = "leon"; 
$myInteger = 1;
$myDouble = 123.456;
 
Using a variable in a computation,
  1. <?php  
  2.    //create variable  
  3.    $result = 2 + 5;  
  4.    //create another variable  
  5.    $doubleResult = $result * 2.001;  
  6.    //print the second variable  
  7.    print($doubtResult);  
  8. ?>  
Variable can be written inside a string surrounded by double quotes, and its value appears in its place. This even works with arrays and objects.
 
Embedded variables
  1. <?php  
  2.    $name = "Zeev";  
  3.    //Greet Zeev  
  4.    print ("Hello , $name!\n");  
  5.    //Greet Zeev again  
  6.    print<<<EOD  
  7.    Hello again, $name!  
  8.    EOD;  
  9. ?>  

String offset

 
If a variable contains a string, it may refer to each character using curly braces. PHP numbers each character starting with zero. To refer to the seventh character in the s variable ,type $s{6} = 'X'.PHP uses only the first character of the value on the right hand side to replace the specified character. If the variable on the left hand side is not the string it remained unchanged.
 
Referencing a single character,
  1. <?php  
  2.    //replace space with underscore   
  3.    $s"a string";  
  4.    $s{1}="_";  
  5.    print($s);  
  6. ?>  
PHP used square brackets to refer to string offsets. Due to ambiguity with the access notation for array, this syntax is now deprecated.
 

Constants

 
Constants are similar to variables, but they may be set only once. Some of them are created automatically by PHP. I will create others with the define function. It is customary to name constants exclusively with capital letters. PHP creates many constants upon startup.
 
Using a constant,
  1. <?php  
  2.    define("STANDARD GREETING""Hello,WOrld!");  
  3.    print(STANDARD_GREETING);  
  4. ?>