Professional Documents
Culture Documents
PHP Language Basics:: Dr. Satish R. Sankaye
PHP Language Basics:: Dr. Satish R. Sankaye
PHP Language Basics:: Dr. Satish R. Sankaye
• Variables are a fundamental part of any programming language. A variable is simply a container that
holds a certain value. Variables get their name because that certain value can change throughout the
echo 2 + 2; or echo 5 + 6;
• By using variables instead of numbers in your script, you make the script much more useful and flexible:
$x = 5 ; $y = 8 ; echo $x + $y;
• A variable consists of two parts: the variable’s name and the variable’s value.
Variables in PHP
Understanding Variables:
• Here are some examples of PHP variable names:
$my_first_variable $anotherVariable
$x $_123
$$sum #grade
$Roll Number $2Number
Discuss: PHP’s variable declaration is Not Necessary?
Constants
• We can also define value - containers called constants in PHP. The
values of constants, as their name implies, can never be changed.
Constants can be defined only once in a PHP program.
• Points to Remember:
• Constants differ from variables in that their names do not start with the dollar
sign, but other than that they can be named in the same way variables are.
• Its good practice to use all - uppercase names for Constants.
• We should avoid naming constants using any of PHP’s reserved words, such as
statements or function names. For example, don’t create a constant called
ECHO or SETTYPE.
Constants
• Points to Remember:
• Constant can be used/accessed from anywhere in PHP program without
regard to variable scope.
• Constant too are case - sensitive.
• Constants may only contain scalar values such as Boolean, integer, float, and
string.
• Constants are useful for any situation where you want to make sure a value
does not change throughout the running of your script.
• Common uses for constants include configuration files and storing text to
display to the user.
Constants
• Points to Remember:
• To define a constant, we use the define() function, and include inside the parentheses
the name chosen for the constant, followed by the value for the constant, as shown
here:
// MY_CONSTANT always has the string value “ 19 ”
define( “MY_CONSTANT”, “19” );
// Displays “ 19 ” (note this is a string, not an integer)
echo MY_CONSTANT;
< ?php $radius = 4; $diameter = $radius * 2;
$circumference = M_PI * $diameter; $area = M_PI * pow( $radius, 2 );
echo “This circle has... < br / > ”; echo “A radius of “ . $radius . “ < br / > ”;
echo “A diameter of “ . $diameter . “ < br / > ”; echo “A circumference of “. $circumference . “ <br/> ”;
echo “An area of “ . $area . “ < br / > ”;
?>
Understanding Data Types
• All data stored in PHP variables fall into one of eight basic categories, known as
data types. A variable’s data type determines what operations can be carried out
on the variable’s data, as well as the amount of memory needed to hold the data.
• PHP supports four scalar data types. Scalar data means data that contains only a
single value.
Scalar Data Type Description Example
Integer A whole number 15
Null May only contain null as a value, meaning the variable explicitly
Understanding Data Types
About Loose Typing:
PHP is known as a loosely - typed language. This means that it’s not particularly
clear about the type of data stored in a variable.
For example, we can initialize a variable with an integer value; add a float value to
it, thereby turning it into a float; then join it onto a string value to produce a longer
Discuss: PHP’s loose typing is both good and bad.
string.
Understanding Data Types
Testing the Type of a Variable:
We can determine the type of a variable at any time by using PHP’s gettype()
function.
To use gettype(), pass in the variable as parameter, whose type we want to test.
The function returns the variable’s type.
When we want to debug a script to pinpoint a bug that might be related to data
types, we use gettype() function.
For example, gettype($x).
Understanding Data Types
Testing the Type of a Variable:
< ?php
// Declares the $test_var variable without initializing it
$test_var;
echo gettype( $test_var ) . “ < br / > ”; // Displays “NULL”
$test_var = 15;
echo gettype( $test_var ) . “ < br / > ”; // Displays “integer”
$test_var = 8.23;
echo gettype( $test_var ) . “ < br / > ”; // Displays “double”
$test_var = “Hello, world!”;
echo gettype( $test_var ) . “ < br / > ”; // Displays “string”
?>
Understanding Data Types
Testing the Type of a Variable:
In PHP, it is often required to test a variable for a specific data type before
using. PHP’s type testing functions are as follows:
Function Description Function Description
is_int( value ) Returns true if value is an integer
is_float( value ) Returns true if value is a float
is_string( value ) Returns true if value is a string
is_bool( value ) Returns true if value is a Boolean
is_array( value ) Returns true if value is an array
is_object( value ) Returns true if value is an object
is_resource( value ) Returns true if value is a resource
is_null( value ) Returns true if value is null
Understanding Data Types
Changing a Variable’s Data Type:
In PHP settype() function is used to change the type of a variable,
while preserving the variable’s value as much as possible.
To use settype(), pass in the name of the variable we want to alter,
followed by the type to change the variable to (in quotation marks).
For example, $var = 345; settype( $var, “string” );
Understanding Data Types
Testing theType
Testing the Typeof of a Variable:
a Variable:
< ?php
< ?php
$test_var
$test_var = 8.23;= 8.23;
echo echo $test_var
$test_var . “ < br ./“><”;br / > ”; // Displays 8.23
settype( $test_var, “string” );
settype( $test_var, “string” );
echo $test_var . “ < br / > ”; // Displays “8.23”
echo $test_var . “ < br / > ”;
settype( $test_var, “integer” );
settype(
echo$test_var,
$test_var“integer”
. “ < br);/ > ”; // Displays “8”
echo settype(
$test_var$test_var,
. “ < br / > ”;“float” );
settype(
echo$test_var,
$test_var“float”
. “ <);br / > ”; // Displays “8”
echo settype(
$test_var$test_var,
. “ < br / > ”;“boolean” );
settype(
echo$test_var,
$test_var“boolean”
. “ < br );/ > ”; // Displays “1”
echo $test_var . “ < br / > ”;
?> ?>
Understanding Data Types
Changing Type by Casting:
Type casting is a technique where variable’s value is converted to specific type as
desired.
This involves placing the name of the desired data type in parentheses before the
variable’s name.
Note that the variable itself remains unaffected; this is in contrast to settype(),
which changes the variable’s type.
For example, $var = 345; echo (string) $test_var .
Understanding Data Types
Changing Type by Casting:
<?php
$test_var = 8.23;
echo $test_var . “ < br / > ”; // Displays “8.23”
echo (string) $test_var . “ < br / > ”; // Displays “8.23”
echo (int) $test_var . “ < br / > ”; // Displays “8”
echo (float) $test_var . “ < br / > ”; // Displays “8.23”
echo (boolean) $test_var . “ < br / > ”; // Displays “1”
?>
Operators and Expressions
• Operator, are used to manipulate the contents of one or more variables to
produce a new value. Thus, an operator is a symbol that manipulates one
or more values, usually producing a new value in the process.
• An expression in PHP is anything that evaluates to a value; this can be any
combination of values, variables, operators, and functions. Examples of
expressions:
• $x + $y + $z 5
• $x - $y true
• $x gettype($test_var )
• The values and variables that are used with an operator are known as
operands.
Operators Types
• Operators in PHP can be grouped into ten types, as follows:
Type Description
Perform common arithmetical operations, such as
Arithmetic addition and subtraction
Assignment Assign values to variables
Bitwise Perform operations on individual bits in an integer
Comparison Compare values in a Boolean fashion ( true or false is
returned)
Logical Boolean operators such as and , or , and not that can
be used to include or exclude
Operators Types
• Operators in PHP can be grouped into ten types, as follows:
Type Description
Incrementing / Increment or decrement a variable ’ s value
Decrementing
String Concatenates (joins together) strings (there ’ s only
one string operator)
Array Perform operations on arrays
Error Control Affect error handling
Execution Cause execution of commands as though they were
shell commands
1. Arithmetic Operators:
• In PHP, the arithmetic operators (plus, minus, and so on) work much
as you expect, enabling us to write expressions.
Operator Example Equation
+ (addition) 6+3=9
- (subtraction) 6-3=3
* (multiplication) 6 * 3 = 18
/ (division) 6/3=2
% (modulus) 6%3=0
2. Assignment Operators:
• The basic assignment operator ( = ) can be used to assign a value to a variable:
$num1 = 8.23;
• In PHP, we can also assign the values as:
$num2 = $num1 = 8.23;
• The equals sign (=) can be combined with other operators to give you a combined
assignment operator that makes it easier to write certain expressions. The
combined assignment operators (such as +=, – =, and so on) simply give you a
shorthand method for performing typical arithmetic operations, so that you don’t
have to write out the variable name multiple times. For example, you can write:
$n1 += $n2; rather than:
$n1 = $n1 + $n2;
3. Bitwise Operators:
• PHP’s bitwise operators let you manipulate these bits directly, as
shown in the following table. Each example includes both decimal
values and their binary equivalents, so you can see how the bits are
altered:
Operator Description Example
& (And) Only bits set in both values 14 & 3 = 2
are set in the result 00001110 & 00000011 = 00000010
| (Or) Bits set in either value are set 14 | 3 = 15
in the result 00001110 | 00000011 = 00001111
^ (Xor) Bits set in either value (but 14 ^ 3 = 13
not both) are set in the result 00001110 | 00000011 = 00001101
Bitwise Operators:
Operator Description Example
~ (Not) Bits set in the value are not set in ~14 = - 15
the result, and vice versa ~00001110
=11110001
< < (Shift left) Shifts all bits in the first value a 3 < < 2 = 12
number of places to the left 00000011 < < 2 = 00001100
(specified by the second value)
> > (Shift Shifts all bits in the first value a 8>>2=2
right) number of places to the right 00001000 > > 2 = 00000010
(specified by the second value)
4. Comparison Operators:
• Comparison operators compare one operand with the other in
various ways. If the comparison test is successful, the expression
evaluates to true; otherwise, it evaluates to false.
Operator Example Result
true if $x is less than $y ; false
< (less than) $x < $y
otherwise
true if $x is greater than $y ; false
> (greater than) $x > $y otherwise
< = (less than or equal to) $x < = $y true if $x is less than or equal to
$y ; false otherwise
> = (greater than or equal to) $x > = $y true if $x is greater than or equal to
$y ; false otherwise
Comparison Operators:
Operator Example Result
== (equal) $x == $y true if $x equals $y ; false otherwise
true if $x does not equal $y ; false
!= or < > (not equal) $x != $y
otherwise