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
- <?php
-
- 2+3;
-
- print("PHP!");
-
- if( 3 > 2)
- {
-
- $a = 3;
- }
- ?>
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,
- <?php
-
- $result = 2 + 5;
-
- $doubleResult = $result * 2.001;
-
- print($doubtResult);
- ?>
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
- <?php
- $name = "Zeev";
-
- print ("Hello , $name!\n");
-
- print<<<EOD
- Hello again, $name!
- EOD;
- ?>
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,
- <?php
-
- $s= "a string";
- $s{1}="_";
- print($s);
- ?>
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,
- <?php
- define("STANDARD GREETING", "Hello,WOrld!");
- print(STANDARD_GREETING);
- ?>