PHP Language Basics:: Dr. Satish R. Sankaye

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

PHP Language Basics:

Dr. Satish R. Sankaye


PHP Language Basics: Syllabus

• Building blocks of PHP:


• Variables,
• Data Types,
• Operators
• Expressions
• Constant.
PHP Language Basics:
• The fundamental concepts of PHP — its building blocks –
• Variables, which let you store and manipulate data in your
scripts
• Constants, which are useful for storing data that doesn’t
change in your script.
• Data types , including which types are available in PHP, and
how to test for and change type.
• PHP ’s available operators , which you can use to manipulate
information
Variables in PHP

• 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

execution of the script.

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

Naming and Creating Variables


• Variable names must begin with a dollar sign ( $ ).
• The first character after the dollar sign must be a letter or an underscore.
• The remaining characters in the name may be letters, numbers, or
underscores without a fixed limit.
• Variable names are case - sensitive ( $Variable and $variable are two distinct
variables), so it’s worth sticking to one variable naming method — for
example, always using lowercase — to avoid mistakes.
• It’s also worth pointing out that variable names longer than 30 characters
are somewhat impractical.
Variables in PHP

Naming and Creating Variables


• Creating a variable in PHP is known as declaring it.
• In PHP when script first sees a variable’s name, it automatically creates
the variable at that point, and there is no provision to declare the
variables exclusively.
• If you don’t initialize a variable in PHP, it’s given the default value of
null. An example of declaring and initializing a variable:
$my_first_variable = 3;
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

Float A floating - point number 8.23

String A series of characters “Hello, world!”

Boolean Represents either true or false true


Understanding Data Types
• PHP supports two compound types. Compound data is data that can contain
more than one value. The following table describes PHP’s compound types:
Compound Data Type Description
Array An ordered map (contains names or numbers mapped to values)
Object A type that may contain properties and methods

• PHP also supports two special data types:


Special Data types Type Description
Resource Contains a reference to an external resource, such as a file or database

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.

It converts a variable’s data type automatically, depending on the context in which


the variable is used.

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

=== (identical) $x === $y true if $x equals $y and they are of the


same type; false otherwise
true if $x does not equal $y or they are
!== (not identical) $x !== $y
not of the same type; false otherwise
Comparison Operators:
<?php
$x = 23;
echo "$x < 24 : ".( $x < 24 ) . " <br/> ";
echo "$x < '24' : ".( $x < "24" ) . " <br/> ";
echo "$x == 23 : ".( $x == 23 ) . " <br/> ";
echo "$x === 23 : ".( $x === 23 ) . " <br/> ";
echo "$x === '23' : ".( $x === "23 " ) . " <br/> ";
echo "$x >= 23 : ".( $x >= 23 ) . " <br/> ";
echo "$x >= 24 : ".( $x >= 24 ) . " <br/> ";
?>
5. Logical Operators:
• PHP ’ s logical operators work on Boolean values.
• Boolean value is either true or false.
• PHP considers the following values to be false :
• The literal value false
• The integer zero ( 0 )
• The float zero ( 0.0 )
• An empty string ( “ “ )
• The string zero ( “0” )
• An array with zero elements
• The special type null (including any unset variables)
• A SimpleXML object that is created from an empty XML tag.
Logical Operators:
Operator Example Result
true if both $x and $y evaluate to true ; false
&& (and) $x && $y otherwise
true if both $x and $y evaluate to true ; false
and $x and $y otherwise
true if either $x or $y evaluates to true ; false
|| (or) $x || $y otherwise
true if either $x or $y evaluates to true ; false
or $x or $y otherwise
true if $x or $y (but not both) evaluates to true ;
Xor $x xor $y false
otherwise
! (not) ! $x true if $x is false ; false if $x is true
6. Increment/Decrement Operators:
To add or subtract the value 1 (one) over and over the increment and decrement special
operators are used to perform this task. They are written as two plus signs or two minus
signs, respectively, preceding or following a variable name.
 

++$x; // Adds one to $x and then returns the result


$x++; // Returns $x and then adds one to it
– - $x; // Subtracts one from $x and then returns the result
$x – - ; // Returns $x and then subtracts one from it
  For example:
$x = 5;
echo ++$x; // Displays “6” (and $x now contains 6)
$x = 5;
echo $x++; // Displays “5” (and $x now contains 6)
7. String Operators:
PHP has only one string operator, and that’s the concatenation
operator, . (dot). This operator simply takes two string values, and
joins the right - hand string onto the left - hand one to make a
longer string. We can also concatenate more than two strings at
once too. 
For example:
echo “Hello, ” . “World”; // Displays “Hello, World”
Furthermore, the values concatenate don’t have to be string; PHP’s
automatic type conversion helps non - string values, such as integers
and floats, converted to strings at the time they’re concatenated
Understanding Operator Precedence
Find the Output of :
<?php
echo 3 + 4 * 5;
?>

You might also like