INTRODUCTION
TO PHP
1Monica Deshmane(H.V.Desai College, Pune)
Language Basics:PHPData types
Java is strongly typed once you've created a
variable, you can't change the type of data stored in
it.
PHP, in contrast, is loosely typed you can change a
variable's data type at any time.
PHP provides eight types of values.
Four are scalar,
two are compound types,
and two are special types.
(Building blocks of
PHP)
Language Basics:PHPData types (Building blocks of PHP)
1. Scalar types:
Integers
Floating-point numbers
Strings
Booleans
2. Compound types:
Arrays
Objects
3. Special types:
Resource
NULL
Data types
1. ScalarData types
Integers are whole numbers, like 1, 12, and 256.
The range of acceptable values varies according to the
details of your platform but typically extends from -
2,147,483,648 to +2,147,483,647.
Integer literals can be written in decimal, octal, or
hexadecimal.
Decimal values are represented by a sequence of digits,
without leading zeros. The sequence may begin with a plus (+)
or minus (-) sign. If there is no sign, positive is assumed.
e.g. 1998, -641, +33
is_int( ) function (or its is_integer( ) alias) to test whether a
value is an integer:
if (is_int($x)) { // $x is an integer }
Integer
1. ScalarData types
Floating-point numbers are also called real numbers.
They are numbers that have a fractional component.
PHP floating-point numbers are equivalent to the double
data type.
PHP recognizes two types of floating point numbers.
i. The first is a simple numeric literal with a decimal
point. e.g. 3.14, 0.017, -7.1
ii. The second is a floating-point number written in
scientific notation as [number]E[exponent], e.g.
1.75E-2.
Use the is_float( ) function (or its is_real( ) alias) to test
whether a value is a floating point number:
if (is_float($x)) { // $x is a floating-point number }
Floating-
Point
Numbers
1. ScalarData types
String
A string is a text literal of an arbitrary length.
A string is indicated by being enclosed in quotes,
either double or single quotes.
Variables are expanded within double quotes,
while within single quotes they are not:
e.g.
$name = “Smith";
echo "Hi, $namen";
echo 'Hi, $name';
O/P:
Hi, Smith
1. ScalarData types
StringA single-quoted string only recognizes  to get a literal
backslash and ' to get a literal single quote:
e.g.
$dos_path = 'C:WINDOWSSYSTEM';
$publisher = 'Tim O'Reilly';
echo "$dos_path $publishern";
Output: C:WINDOWSSYSTEM Tim O'Reilly
To test whether two strings are equal, use the ==
comparison operator:
e.g. if ($a == $b) { echo "a and b are equal" }
is_string( ) function to test whether a value is a string: if
(is_string($x)) { // $x is a string }
Escape sequences applied.
1. ScalarData types
BooleanA boolean value represents a "truth value"—it says
whether something is true or not. Truth and falseness
determine the outcome of conditional code such as:
if ($alive) { ... }
In PHP, the following values are false:
The integer -> 0
The floating-point value-> 0.0
The empty string-> ("") and the string "0"
An array ->with zero elements
An object ->with no values or functions
The NULL value
is_bool( ) function to test whether a value is a boolean: if
(is_bool($x)) { // $x is a boolean }
2. Compound Data types
ArraysAn array is a variable that holds a group of values. By
referring to their index position we can access the
individual elements.
The position is either specified numerically or by name.
An array with a numeric index is commonly called an
indexed array while one that has named positions is called
an associative array.
In PHP, all arrays are associative, but you can still use a
numeric index to access them.
Referencing array elements:
$arrayName[index];
2. Compound Data types
Arraysis_array( ) function to test whether a value is an array:
if (is_array($x)) { // $x is an array }
indexed array
$item[0] = “Pen"; $item[1] = “Pencil";
$item[2] = “Notebook";
associative array
$book[‘C’] = “Richie”; $book[‘CPP’] = “Bjarne”;
$book[‘Java’] = “Sun”;
The array( ) construct creates an array:
$item = array(‘Pen’, ‘Pencil’, ‘Notebook’);
$book = array(‘C’ => ‘Richie’ , ‘CPP’ => ‘Bjarne’,
‘Java’ => ‘Sun’);
2. Compound Data types
Objects
PHP supports object-oriented programming (OOP).
Classes are the unit of object-oriented design.
A class is a definition of a structure that contains
properties (variables) and methods (functions).
 Classes are defined with the class keyword:
<?php
class Book
{ var $name = '';
function name ($newname = NULL)
{ if (! is_null($newname))
{ $this->name = $newname; }
return $this->name;
}
}
2. Compound Data types
Objects
Once a class is defined, any number of objects
can be made from it with the new keyword,
and the properties and methods can be accessed
with the -> construct:
Ex. $b1 = new Book;
$b1->name('C');
printf("Book1, %sn", $b1->name);
$b2 = new Book;
$b2->name('CPP');
printf("Book2 %sn", $b2->name);
Output?
Book1 C Book2 CPP
is_object( ) function to test whether a value is an object:
if (is_object($x)) { // $x is an object }
3. Special Data types
Resources
A resource is a special variable,
holding a reference to an external resource.
Resources are files,pictures,reasult of query
but except php code..
Database connect function gives you something
by which to identify that connection
when you call the query and close functions
that is called as a resource.
Resources are really integers under the surface.
Their main benefit is that they're garbage collected
when no longer in use.
is_resource( ) function to test whether a value is a resource:
if (is_resource($x)) { // $x is a resource }
$res=database_connect();
3. Special Data types
NULLThere's only one value of the NULL data type.
the case-insensitive keyword NULL.
The NULL value represents a variable that has no value.
$aleph = "beta"; $aleph = null; // variable's value is gone.
is_null( ) function to test whether a value is NULL-for instance,
to see whether a variable has a value:
if (is_null($x)) { // $x is NULL }
Q.PHP does not require explicit variable declaration (T/F) ?
Q.What is type juggling?
Scope of variable :VariableScope
The scope of a variable is the context within which it is defined.
e.g.
$a = 3;
function f( )
{ $a += 2; echo $a;
}
f( );
echo $a;
Output is 2
3
Here variable outside f() and inside f() is different.
Local
Scope
Scope of variable :VariableScope
If you want a variable in the global scope to be accessible
from within a function, you can use the global keyword.
Its syntax is: global var1, var2, ...
$a = 3;
function f( )
{ global $a; $a += 2; }
f( );
echo $a;
Output: 5
Include the global keyword in a function before any uses
of the global variable or variables you want to access.
Q.global variables are manipulated by function?(T)
Global
Scope
Scope of variable :VariableScope
A static variable retains its value between calls to a function but
is visible only within that function. You declare a variable static
with the static keyword.
<?php
function change_value()
{
static $i=0;
$i++;
echo "Static variable is : $i";
}
$i=10;
change_value();
change_value();
echo "Global Variable is $i”;?>
Static
Scope
Operators
1. Arithmetic Operators:
+, -, *, /, %, -
2. String Concatenation Operator:
. (dot) is used for concatination of String
e.g.
$n = 5;
$s = ‘There are ‘.$n. ‘items’;
3. Autoincrement and Autodecrement Operators:
++ and - -
4. Comparision Operators:
• Equality (==)
Operators
• Identical ( ===)
 If both operands are equal and are of the same data
type then this operator returns true and false
otherwise.
$i = 0;
$f = “0”;
if ($i === $f)
{
echo "they are equal";
}
else
{
echo "they are not equal";
}
Operators
• Bitwise operator
 ~,&,|,^,>>,<<
• Logical operator
 And,or,xor,!
• Assignment opertor =
• Ternary/conditional operator (? :)
• New(object creation)
• Miscellaneous operators-
 Error sppression(@)
To generate or inhibit error
 Execution operator( `…`) backtick
To excute shell cmd in php
Q.@ is used for executing query?T/F
Date & Time function
Date(“format”,$Timestamp)
Ex.
<?php
$t=date(“Y/M/D”);Echo $t;
?>
m-month ex.2
M-mar
D-Fri
d-date ex.21
W-week no.start from 0 (Sunday)
Time :-
$t=time(); //to display current time
Extra Topics to learn from book
•Garbage Collection
•Type casting
•Basic HTML Tags
Flow Control Structures
1. If , if- else ,If- else-if
2. Switch
3. While
4. Do-while
5. For
Flow Control Structures
6. Foreach
7. Declare(directive)
Ex.declare(ticks=2)
8. Exit & return
9. Break & continue
Explanation https://www.tutorialspoint.com
24Monica Deshmane(H.V.Desai College, Pune)
End Of Chapter

Chap1introppt2php(finally done)

  • 1.
  • 2.
    Language Basics:PHPData types Javais strongly typed once you've created a variable, you can't change the type of data stored in it. PHP, in contrast, is loosely typed you can change a variable's data type at any time. PHP provides eight types of values. Four are scalar, two are compound types, and two are special types. (Building blocks of PHP)
  • 3.
    Language Basics:PHPData types(Building blocks of PHP) 1. Scalar types: Integers Floating-point numbers Strings Booleans 2. Compound types: Arrays Objects 3. Special types: Resource NULL Data types
  • 4.
    1. ScalarData types Integersare whole numbers, like 1, 12, and 256. The range of acceptable values varies according to the details of your platform but typically extends from - 2,147,483,648 to +2,147,483,647. Integer literals can be written in decimal, octal, or hexadecimal. Decimal values are represented by a sequence of digits, without leading zeros. The sequence may begin with a plus (+) or minus (-) sign. If there is no sign, positive is assumed. e.g. 1998, -641, +33 is_int( ) function (or its is_integer( ) alias) to test whether a value is an integer: if (is_int($x)) { // $x is an integer } Integer
  • 5.
    1. ScalarData types Floating-pointnumbers are also called real numbers. They are numbers that have a fractional component. PHP floating-point numbers are equivalent to the double data type. PHP recognizes two types of floating point numbers. i. The first is a simple numeric literal with a decimal point. e.g. 3.14, 0.017, -7.1 ii. The second is a floating-point number written in scientific notation as [number]E[exponent], e.g. 1.75E-2. Use the is_float( ) function (or its is_real( ) alias) to test whether a value is a floating point number: if (is_float($x)) { // $x is a floating-point number } Floating- Point Numbers
  • 6.
    1. ScalarData types String Astring is a text literal of an arbitrary length. A string is indicated by being enclosed in quotes, either double or single quotes. Variables are expanded within double quotes, while within single quotes they are not: e.g. $name = “Smith"; echo "Hi, $namen"; echo 'Hi, $name'; O/P: Hi, Smith
  • 7.
    1. ScalarData types StringAsingle-quoted string only recognizes to get a literal backslash and ' to get a literal single quote: e.g. $dos_path = 'C:WINDOWSSYSTEM'; $publisher = 'Tim O'Reilly'; echo "$dos_path $publishern"; Output: C:WINDOWSSYSTEM Tim O'Reilly To test whether two strings are equal, use the == comparison operator: e.g. if ($a == $b) { echo "a and b are equal" } is_string( ) function to test whether a value is a string: if (is_string($x)) { // $x is a string } Escape sequences applied.
  • 8.
    1. ScalarData types BooleanAboolean value represents a "truth value"—it says whether something is true or not. Truth and falseness determine the outcome of conditional code such as: if ($alive) { ... } In PHP, the following values are false: The integer -> 0 The floating-point value-> 0.0 The empty string-> ("") and the string "0" An array ->with zero elements An object ->with no values or functions The NULL value is_bool( ) function to test whether a value is a boolean: if (is_bool($x)) { // $x is a boolean }
  • 9.
    2. Compound Datatypes ArraysAn array is a variable that holds a group of values. By referring to their index position we can access the individual elements. The position is either specified numerically or by name. An array with a numeric index is commonly called an indexed array while one that has named positions is called an associative array. In PHP, all arrays are associative, but you can still use a numeric index to access them. Referencing array elements: $arrayName[index];
  • 10.
    2. Compound Datatypes Arraysis_array( ) function to test whether a value is an array: if (is_array($x)) { // $x is an array } indexed array $item[0] = “Pen"; $item[1] = “Pencil"; $item[2] = “Notebook"; associative array $book[‘C’] = “Richie”; $book[‘CPP’] = “Bjarne”; $book[‘Java’] = “Sun”; The array( ) construct creates an array: $item = array(‘Pen’, ‘Pencil’, ‘Notebook’); $book = array(‘C’ => ‘Richie’ , ‘CPP’ => ‘Bjarne’, ‘Java’ => ‘Sun’);
  • 11.
    2. Compound Datatypes Objects PHP supports object-oriented programming (OOP). Classes are the unit of object-oriented design. A class is a definition of a structure that contains properties (variables) and methods (functions).  Classes are defined with the class keyword: <?php class Book { var $name = ''; function name ($newname = NULL) { if (! is_null($newname)) { $this->name = $newname; } return $this->name; } }
  • 12.
    2. Compound Datatypes Objects Once a class is defined, any number of objects can be made from it with the new keyword, and the properties and methods can be accessed with the -> construct: Ex. $b1 = new Book; $b1->name('C'); printf("Book1, %sn", $b1->name); $b2 = new Book; $b2->name('CPP'); printf("Book2 %sn", $b2->name); Output? Book1 C Book2 CPP is_object( ) function to test whether a value is an object: if (is_object($x)) { // $x is an object }
  • 13.
    3. Special Datatypes Resources A resource is a special variable, holding a reference to an external resource. Resources are files,pictures,reasult of query but except php code.. Database connect function gives you something by which to identify that connection when you call the query and close functions that is called as a resource. Resources are really integers under the surface. Their main benefit is that they're garbage collected when no longer in use. is_resource( ) function to test whether a value is a resource: if (is_resource($x)) { // $x is a resource } $res=database_connect();
  • 14.
    3. Special Datatypes NULLThere's only one value of the NULL data type. the case-insensitive keyword NULL. The NULL value represents a variable that has no value. $aleph = "beta"; $aleph = null; // variable's value is gone. is_null( ) function to test whether a value is NULL-for instance, to see whether a variable has a value: if (is_null($x)) { // $x is NULL } Q.PHP does not require explicit variable declaration (T/F) ? Q.What is type juggling?
  • 15.
    Scope of variable:VariableScope The scope of a variable is the context within which it is defined. e.g. $a = 3; function f( ) { $a += 2; echo $a; } f( ); echo $a; Output is 2 3 Here variable outside f() and inside f() is different. Local Scope
  • 16.
    Scope of variable:VariableScope If you want a variable in the global scope to be accessible from within a function, you can use the global keyword. Its syntax is: global var1, var2, ... $a = 3; function f( ) { global $a; $a += 2; } f( ); echo $a; Output: 5 Include the global keyword in a function before any uses of the global variable or variables you want to access. Q.global variables are manipulated by function?(T) Global Scope
  • 17.
    Scope of variable:VariableScope A static variable retains its value between calls to a function but is visible only within that function. You declare a variable static with the static keyword. <?php function change_value() { static $i=0; $i++; echo "Static variable is : $i"; } $i=10; change_value(); change_value(); echo "Global Variable is $i”;?> Static Scope
  • 18.
    Operators 1. Arithmetic Operators: +,-, *, /, %, - 2. String Concatenation Operator: . (dot) is used for concatination of String e.g. $n = 5; $s = ‘There are ‘.$n. ‘items’; 3. Autoincrement and Autodecrement Operators: ++ and - - 4. Comparision Operators: • Equality (==)
  • 19.
    Operators • Identical (===)  If both operands are equal and are of the same data type then this operator returns true and false otherwise. $i = 0; $f = “0”; if ($i === $f) { echo "they are equal"; } else { echo "they are not equal"; }
  • 20.
    Operators • Bitwise operator ~,&,|,^,>>,<< • Logical operator  And,or,xor,! • Assignment opertor = • Ternary/conditional operator (? :) • New(object creation) • Miscellaneous operators-  Error sppression(@) To generate or inhibit error  Execution operator( `…`) backtick To excute shell cmd in php Q.@ is used for executing query?T/F
  • 21.
    Date & Timefunction Date(“format”,$Timestamp) Ex. <?php $t=date(“Y/M/D”);Echo $t; ?> m-month ex.2 M-mar D-Fri d-date ex.21 W-week no.start from 0 (Sunday) Time :- $t=time(); //to display current time Extra Topics to learn from book •Garbage Collection •Type casting •Basic HTML Tags
  • 22.
    Flow Control Structures 1.If , if- else ,If- else-if 2. Switch 3. While 4. Do-while 5. For
  • 23.
    Flow Control Structures 6.Foreach 7. Declare(directive) Ex.declare(ticks=2) 8. Exit & return 9. Break & continue Explanation https://www.tutorialspoint.com
  • 24.