Upload
lotlot
View
2.178
Download
0
Embed Size (px)
Citation preview
PHP: Hypertext Preprocessor
The Building Blocks
Special Data TypesResource - Reference to a third-party
resource (a database, for example)NULL - An uninitialized variable
Sample<?php$testing; // declare without assigningprint gettype( $testing ); // NULL?>
PHP’s Type Functions
var_dump() - tells you a variable's type and its contents
Sample<php$testing = 5; var_dump( $testing ); ?>
PHP’s Type Functionsgettype()function - to acquire the type of any
variable. If you place a variable between the parentheses of the function call, gettype() returns a string representing the relevant type (NULL, integer, string, double , boolean )
Sample<?php$testing = 5; print gettype( $testing ); // integer ?>
PHP’s Type Functionssettype()function - to change the type of a variable. To
use settype(), you must place the variable to change (and the type to change it to) between the parentheses and separate them by commas
Sample<?php$undecided = 3.14;settype( $undecided, double );print gettype( $undecided ); print " -- $undecided<br />"; // 3.14?>
Operators and ExpressionsOperators- are symbols that enable you to use one or more
values to produce a new value. A value that is operated on by an operator is referred to as an operand.4+3
Expression- is any combination of functions, values, and
operators that resolves to a value.4+3=9$user=you;gettype( $user);
Assignment Operator - takes the value of its right operand
and assigns it to its left operand$name = "matt";
Operators and ExpressionsArithmetic Operators - The addition operator
adds the right operand to the left operand, whereas the subtraction operator subtracts the right operand from the left. The division operator divides the left operand by the right, and the multiplication operator multiplies the left operand by the right. The modulus operator returns the remainder of the left operand divided by the right.
Example(+)Addition (-)Subtraction (/)Division (*) Multiplication (%)Modulus
Concatenation OperatorThe concatenation operator is a single period
(.). Treating both operands as strings, it appends the right operand to the left
Sample"hello"." world" is equivalent to "hello world" $centimeters = 212; print "the width is ".($centimeters/100)."
meters";
Combined Assignment Operatorconsists of a standard operator symbol
followed by an equals sign.Example$x = 4;$x = $x + 4; // $x now equals 8 can instead be written as$x = 4;$x += 4; // $x now equals 8
Comparison Operatorsperform tests on their operands. They return
the boolean value true if the test is successful and return false otherwise. This type of expression is useful in control structures, such as if and while statementsExample$x=5.1$x < 5
Logical OperatorsTo test combinations of booleans
Sampletrue || false = truetrue && false = false
Operator Precedence
When you use an operator, the PHP engine usually reads your expression from left to right.
Sample4 + 5=94 + 5 * 2 = 18 or 4 + 5 * 2 = 14 or (4 + 5) * 2 = 18
Constants Variables offer a flexible way of storing data because you can change their
values and the type of data they store at any time. If, however, you want to work with a value that you do not want to alter throughout your script's execution, you can define a constant. You must use PHP's built-in function define() to create a constant. After you have done this, the constant cannot be changed. To use the define() function, you must place the name of the constant and the value you want to give it within the call's parentheses. These values must be separated by a comma, like so:
Example
define ("CONSTANT_NAME", 42);
<?php define ("USER", "Gerald");
print "Welcome".USER;?>