php notes
php notes
Office NO- 17/18, 2nd Floor, City Center Opposite – Gossner College, Club
Road, Ranchi, Contact No. 8877155769, 7903154392.
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.
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
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.
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.
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.
PHP is compatible with almost all local servers used today like Apache, Netscape,
Security:
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
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'.
<?php...?>
<?...?>
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 −
<%...%>
Comments in PHP
=================
multiline comment= /* */
==============================
PHP echo statement can be used to print the string, multi-line strings, escaping
characters, variable, array, etc.
Print function
PHP print statement can be used to print the string, multi-line strings, escaping
characters, variable, array, etc.
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;
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,
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);
?>
example-
<?php
$x=9223372036854775808;
echo "\n".gettype($x);
?>
o/p
Integer size =8
double
PHP has the following functions to check if the type of a variable is integer:
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)
=================
1. compile time
2. command line
3.runtime
2. command line
<?php
echo gettype($argc);
echo gettype($argv);
if($argc==4)
$s=0;
for($i=1;$i<$argc;$i++)
$s+=$argv[$i];
echo $s;
else
?>
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
example
<?php
$z=$x+$y;
$avg=$z/2;
?>
method-2:
example-
<?php
$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
$z=$x+$y;
$avg=$z/2;
?>
example-2
<?php
$x=explode(';',readline('numbers'));
for($i=0;$i<count($x);$i++)
?>
method-3
<?php
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
sscanf($input,"%d%d%d",$a,$b,$c);
$sum=$a+$b+$c;
?>
example:
$x=3.2345;
$y=33.23E+01;
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;
$x=-1.9e+400;
?>
example
$x="ranchi";
$y='jharkhand';
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.
PHP provides a couple of constants especially for use as Booleans: TRUE and
FALSE
$x=true;
$y=FALSE;
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.
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-
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:
<?php
$x=10;
$y=3;
echo $x**$y;
echo 2**3;
?>
Comparison operators
>,<,>=,<=,
== equal to
=== identical
example
<?php
//operators
var_dump($x);
$x=true || false;
var_dump($x);
var_dump($x);
$x=false || true;
var_dump($x);
?>
Assignment operators(=,+=,-=,*=,/=,%=,&=,|=,<<=,>>=)
String operators(.,.=)
Array operators
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
?: - ternary
?? - Null coalescing
syntax:
$x = expr1 ?? 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
` `backticks
example
<?php
echo `dir`;
echo `date`;
?>
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();
?>
o/p
true
false
example
<?php
class MyClass {
public $color;
public $amount;
$obj->color = "red";
$obj->amount = 5;
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
var_dump((bool) null);
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)
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) */
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.
syntax:
[Note: by default defined constant is gloabal in nature and can be used in while
script]
syntax:
const constant_name=value;
constant() function
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
define("x",100,true);//default is false
const k=300;
echo k;
echo constant("PI");
?>