The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
PHP (Hypertext preprocessor)
PHP is a server scripting language, and a powerful tool for making dynamic and
interactive Web pages. PHP is an open-source, interpreted, and object-oriented
scripting language that can be executed at the server-side. PHP is well suited for
web development. Therefore, it is used to develop web applications (an application
that executes on the server and generates the dynamic page.).
PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995.
PHP 7 and PHP 8 is the latest version of PHP.
Some important points
PHP stands for Hypertext Preprocessor.
PHP is an interpreted language, i.e., there is no need for compilation.
PHP is faster than other scripting languages, for example, ASP and JSP.
PHP is a server-side scripting language, which is used to manage the
dynamic content of the website.
PHP can be embedded into HTML.
PHP is an object-oriented language.
PHP is an open-source scripting language.
PHP is simple and easy to learn language.
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
Features of PHP
Performance:
PHP script is executed much faster than those scripts which are written in other
languages such as JSP and ASP. PHP uses its own memory, so the server workload
and loading time is automatically reduced, which results in faster processing speed
and better performance.
Open Source:
PHP source code and software are freely available on the web. You can develop all
the versions of PHP according to your requirement without paying any cost. All its
components are free to download and use.
Familiarity with syntax:
PHP has easily understandable syntax. Programmers are comfortable coding with
it.
Embedded:
PHP code can be easily embedded within HTML tags and script.
Platform Independent:
PHP is available for WINDOWS, MAC, LINUX & UNIX operating system. A
PHP application developed in one OS can be easily executed in other OS also.
Database Support:
PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
Error Reporting -
PHP has predefined error reporting constants to generate an error notice or warning
at runtime. E.g., E_ERROR, E_WARNING, E_STRICT, E_PARSE.
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
Loosely Typed Language:
PHP allows us to use a variable without declaring its data type. It will be taken
automatically at the time of execution based on the type of data it contains on its
value.
Web servers Support:
PHP is compatible with almost all local servers used today like Apache, Netscape,
Microsoft IIS, etc.
Security:
PHP is a secure language to develop the website. It consists of multiple layers of
security to prevent threads and malicious attacks.
Control:
Different programming languages require long script or code, whereas PHP can
do the same work in a few lines of code. It has maximum control over the websites
like you can make changes easily whenever you want.
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
Escaping to PHP
The PHP parsing engine needs a way to differentiate PHP code from other
elements in the page. The mechanism for doing so is known as 'escaping to PHP'.
There are four ways to do this −
Canonical PHP tags
The most universally effective PHP tag style is −
<?php...?>
Short-open (SGML-style) tags
Short or short-open tags look like this −
<?...?>
Set the short_open_tag setting in your php.ini file to on. This option must be
disabled to parse XML with PHP because the same syntax is used for XML tags.
ASP-style tags
ASP-style tags mimic the tags used by Active Server Pages to delineate code
blocks. ASP-style tags look like this −
<%...%>
HTML script tags
HTML script tags look like this −
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
<script language = "PHP">...</script>
Comments in PHP
=================
single line comment = // and #
multiline comment= /* */
To print the message or o/p in PHP
==============================
PHP echo statement can be used to print the string, multi-line strings, escaping
characters, variable, array, etc.
void echo ( string $arg1 [, string $... ] )
few points about echo
echo is a statement, which is used to display the output.
echo can be used with or without parentheses: echo(), and echo.
echo does not return any value.
We can pass multiple strings separated by a comma (,) in echo.
echo is faster than the print statement.
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
Print function
PHP print statement can be used to print the string, multi-line strings, escaping
characters, variable, array, etc.
few points about print
print is a statement, used as an alternative to echo
print can be used with or without parentheses.
print always returns an integer value, which is 1.
Using print, we cannot pass multiple arguments.
print is slower than the echo statement.
Printf() function as similar to printf() function of C
PHP Variables
In PHP, a variable is declared using a $ sign followed by the variable name.
As PHP is a loosely typed language, so we do not need to declare the data types of
the variables. It automatically analyzes the values and makes conversions to its
correct datatype.
syntax: $variablename=value;
Rules for declaring PHP variable:
A variable must start with a dollar ($) sign, followed by the variable name.
It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
A variable name must start with a letter or underscore (_) character.
A PHP variable name cannot contain spaces.
PHP variables are case-sensitive, so $x and $X both are treated as different
variable.
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
Data types
It is classified into two types
1. Scalar/Simple
Integer/int : These are the whole numbers, without a decimal point either +ve or -
ve.
Integer can be in decimal (base 10), octal (base 8), and hexadecimal (base 16)
format. Decimal format is the default; octal integers are specified with a leading 0,
and hexadecimals have a leading 0x.
ex-
<?php
$x=10; //decimal
$y=010;//octal
$z=0xA;//hexa decimal
echo $x,$y,$z;
printf(" %d %o %X",$x,$y,$z);
var_dump($y);
print gettype($x);
?>
PHP has the following predefined constants for integers:
PHP_INT_MAX - The largest integer supported
PHP_INT_MIN - The smallest integer supported
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
PHP_INT_SIZE - The size of an integer in bytes
example-
<?php
echo "Maximum integer value =".PHP_INT_MAX."\n";
echo "Minimum integer value =".PHP_INT_MIN."\n";
echo "Integer size =".PHP_INT_SIZE;
$x=9223372036854775808;
echo "\n".gettype($x);
?>
o/p
Maximum integer value =9223372036854775807
Minimum integer value =-9223372036854775808
Integer size =8
double
An integer data type is a non-decimal number between -2147483648 and
2147483647 in 32 bit systems, and between -9223372036854775808 and
9223372036854775807 in 64 bit systems. A value greater (or lower) than this,
will be stored as float, because it exceeds the limit of an integer.
PHP has the following functions to check if the type of a variable is integer:
is_int()
is_integer() - alias of is_int()
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
is_long() - alias of is_int()
example
<?php
$x=10;
echo var_dump(is_int($x));
echo var_dump(is_integer($x));
echo var_dump(is_long($x));
?>
o/p
bool(true)
bool(true)
bool(true)
Input method in PHP
=================
1. compile time
2. command line
3.runtime
2. command line
$argc - It is an integer that contains total number of arguments passed from
command line including the name of program.
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
$argv - It is an array of arguments value passed from command line.
ex- Wap to add three numbers supplied through command line.
<?php
echo "total no of arguments =".$argc;
echo gettype($argc);
echo gettype($argv);
if($argc==4)
$s=0;
for($i=1;$i<$argc;$i++)
$s+=$argv[$i];
echo $s;
else
echo "insufficient no of arguments";
?>
3. Run time input
method-1
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
readline(): It is a predefined function in php which prompt user for run time
input and return the string representation of input data.
example
<?php
$x=(int)READLIne("Enter first number");
$y=(int)readline("Enter second number");
$z=$x+$y;
$avg=$z/2;
echo "Addition=$z and average=$avg";
?>
method-2:
explode()- This function breaks a string into an array.
list() - This function is used to assign a number of values to a list of variable.
example-
<?php
$x="i love india";
$arr=explode(' ',$x);
print_r($arr);
?>
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
example-
<?php
list($x,$y)=explode(';',readline('Enter two numbers'));
$z=$x+$y;
$avg=$z/2;
echo "Addition=$z and average=$avg";
?>
example-2
<?php
$x=explode(';',readline('numbers'));
for($i=0;$i<count($x);$i++)
echo $x[$i]." ";
?>
method-3
using the function fscanf()
<?php
echo "Enter three numbers";
fscanf(STDIN,"%d%d%d",$x,$y,$z);
$sum=$x+$y+$z;
echo "Result=$sum";
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
?>
method-4
<?php
$input=readline("Enter three numbers");
sscanf($input,"%d%d%d",$a,$b,$c);
$sum=$a+$b+$c;
echo "Result of addition=$sum";
?>
float/double : A real number, no with decimal point. It may be of two forms
a. scientific/exponentital form ex- 2.34E+02
b. Fixed form ex- 23.345
example:
$x=3.2345;
$y=33.23E+01;
echo " ".$x." ".$y."\n";
printf(" %.2f %g %e %f",$x,$x,$x,$x);
var_dump($x);
echo gettype($x);
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
example-2
<?php
$x=10.5;
echo PHP_FLOAT_MAX,PHP_FLOAT_MIN,-PHP_FLOAT_MAX,-
PHP_FLOAT_MIN;
echo var_dump(is_float($x));
echo var_dump(is_double($x));
$x=1.9e+400;
echo $x." ".var_dump(is_finite($x));
$x=-1.9e+400;
echo $x." ".var_dump(is_infinite($x));
?>
String : A single or set of characters enclose in either single quotes('') or double
quotes(" ").
example
$x="ranchi";
$y='jharkhand';
echo $x." ".$y."\n";
var_dump($x);
echo gettype($y);
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
boolean/bool: It has only two possible values either true or false.
PHP provides a couple of constants especially for use as Booleans: TRUE and
FALSE
$x=true;
$y=FALSE;
echo $x." ".$y."\n";
var_dump($x);
echo gettype($y);
2. Composite
array :An array stores multiple values in one single variable and value not to be
of similar type.
example
$x=array(10,20,'sam',10.5);
for($i=0;$i<4;$i++)
echo $x[$i];
var_dump($x);
echo gettype($x);
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
object : It refers to the object of a class type.
example
class test
$x=new test();
$y=new test();
var_dump($x);
echo gettype($x);
3. special type
null: NULL is a special type that only has one value: NULL.
example-
resource: The special resource type is not an actual data type.
It is the storing of a reference to functions and resources external to PHP.
A common example of using the resource data type is a database call/ file handle.
example
$x=fopen("abc","w");
var_dump($x);
echo gettype($x);
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
Operators in PHP
Operators are used to perform operations on variables and values. PHP divides the
operators in the following groups:
Arithmetic operators (+,-,*,/,%,**(Exponentiation));
<?php
$x=10;
$y=3;
echo $x**$y;
echo 2**3;
?>
Comparison operators
>,<,>=,<=,
== equal to
=== identical
!= or <> - not equal to
!== - not identical
Bitwise operators (&,|,^,~,<<,>>)
Increment/Decrement operators (++,--)
Logical operators (AND,OR,XOR,&&,||,!)
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
example
<?php
//operators
$x=true or false;// because = is higher precedence over AND
var_dump($x);
$x=true || false;
var_dump($x);
$x=false or true;// because = is higher precedence over OR
var_dump($x);
$x=false || true;
var_dump($x);
?>
Assignment operators(=,+=,-=,*=,/=,%=,&=,|=,<<=,>>=)
String operators(.,.=)
Array operators
+(union)-Union of two arrays
example
<?php
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
$x=array(10=>"a",20=>"b",30=>"c");
$y=array(20=>"b",10=>"a",30=>"c",40=>"e");
$z=array(5=>"f");
print_r($x+$y+$z);
?>
o/p:
Array
[10] => a
[20] => b
[30] => c
[40] => e
[5] => f
==(equality) :Returns true if both the arrays have the same key/value pairs
===(identity) :Returns true if both the arrays have the same key/value pairs in the
same order and of the same types
!= or <>(inequality) :Returns true if both the arrays are not equal to each other
!==(non-identity)) :Returns true if if both the arrays are not identical to each other
example
$x=array(10=>"a",20=>"b",30=>"c");
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
$y=array(20=>"b",10=>"a",30=>"c");
var_dump($x==$y);//true
var_dump($x===$y);//false
var_dump($x!=$y);//false
var_dump($x!==$y);//true
Conditional assignment operators
?: - ternary
?? - Null coalescing
syntax:
$x = expr1 ?? expr2
Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Introduced in PHP 7
example
<?php
$x=100??200;
echo $x;
$x=null??200;
echo $x;
?>
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
o/p
100200
Execution Operators : Used to execute dos or linux/unix command
` `backticks
example
<?php
echo `dir`;
echo `date`;
?>
Special operators (instanceof,clone,(type), ,(comma))
instanceof - It is used to check weather the given object is instance of particular
class
or not.
example
<?php
class test
{}
class demo
{}
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
$obj=new test();
var_dump($obj instanceof test);
var_dump($obj instanceof demo);
?>
o/p
true
false
example
<?php
class MyClass {
public $color;
public $amount;
$obj = new MyClass();
$obj->color = "red";
$obj->amount = 5;
$copy = clone $obj;
print_r($copy);
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
?>
(type) - It is used for type casting, i.e change one type into another.
example
<?php
//other type into boolean
/* var_dump((bool) ""); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "sam"); // bool(true)
var_dump((bool) 2.0); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
var_dump((bool) null);
//other type into int type casting operator is (int),(integer), or intval()
var_dump((int)true);//int(1)
var_dump((integer)false);//int(0)
var_dump((int)null);//int(0)
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
var_dump((int)10.5);//int(10)
var_dump((int)"sam");//int(0)
var_dump((int)"123");//int(123);
var_dump(intval("q123asd"));//int(0)
//other type into float type casting operator is (float),(double), or (real)
var_dump((float)true);//float(1)
var_dump((float)false);//float(0)
var_dump((float)null);//float(0)
var_dump((float)10.5);//float(10.5)
var_dump((float)"sam");//float(0)
var_dump((double)"123");//float(123);
var_dump((real)"q123asd");//float(0)
var_dump((real)"");//float(0) */
//from other type to string using (string) or strval() function
var_dump((string)true);//string(1)
var_dump((string)false);//string(0)
var_dump((string)null);//string(0)
var_dump((string)10.5);//string(4)
var_dump((string)"sam");//string(3)
var_dump((string)"123");//string(3);
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
var_dump((string)"q123asd");//string(7)
var_dump(strval(""));//string(0)
Constant in PHP
PHP constants are name or identifier that can't be changed during the execution
of the script except for magic constants, which are not really constants.
PHP constants can be defined by 2 ways:
Using define() function
syntax:
define(name, value, case-insensitive[default false])
[Note: by default defined constant is gloabal in nature and can be used in while
script]
Using const keyword
syntax:
const constant_name=value;
constant() function
This function will return the value of the constant.
echo constant("PI");
The Launcher Academy
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
example
<?php
define("PI",3.14,false);//default is false
echo PI;//o/p 3.14
echo pi;//o/p error
define("x",100,true);//default is false
echo x;//o/p 3.14
echo X;//o/p error
const k=300;
echo k;
echo constant("PI");
?>