PHP Unit I
PHP Unit I
WWW: WWW stands for World Wide Web and is commonly known as the Web.
The WWW was started by CERN in 1989. WWW is defined as the collection of
different websites around the world, containing different information shared via
local servers.
Web pages are linked together using hyperlinks which are HTML-formatted and,
also referred to as hypertext, these are the fundamental units of the Internet and
are accessed through Hypertext Transfer Protocol(HTTP).
Website: A site or website is a central location of various web pages that are all
related and can be accessed by visiting the home page of the website using a
browser.
2. Web ba s e d applications
3. Mobile applications
Web server: Web Server: Web server is a program which processes the network
requests of the users and serves them with files that create web pages. This
exchange takes place using Hypertext Transfer Protocol (HTTP).
Basically, web servers are computers used to store HTTP files which makes a
website and when a client requests a certain website, it delivers the requested
website to the client. For example, you want to open Facebook on your laptop
and enter the URL in the search bar of google. Now, the laptop will send an HTTP
request to view the facebook webpage to another computer known as the
webserver. This computer (webserver) contains all the files (usually in HTTP
format) which make up the website like text, images, gif files, etc. After
processing the request, the webserver will send the requested website-related
files to your computer and then you can reach the website.
INTRODUCTION TO PHP
PHP is server-side scripting language implemented by Rasmus Lerdorf in
1994 using C and PERL languages. He implemented PHP 1.0 totrack total
number of visitors in his server. PHP stands for Personal Home Page. It
also has an alias name Hypertext Preprocessor.
What is PHP?
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP is a widely used, open-source scripting language
PHP scripts are executed on the server
Faster: It is faster than other scripting language e.g. asp and jsp.
Interpreted: It is an interpreted language, i.e. there is no need for
compilation.
Open Source: Open source means you no need to pay for use php, you
can free download and use.
Case Sensitive: PHP is case sensitive scripting language at time of
variable declaration. In PHP, all keywords (e.g. if, else, while, echo,
etc.), classes, functions, and user-defined functions are NOT case-
sensitive
Simplicity: PHP provides a lot of pre-define functions to secure your
data. It is also compatible with many third-party applications, and PHP
can easily integrate with other.In PHP script there is no need to include
libraries like c, special compilation directives like Java, PHP engine
starts execution from (<?) escape sequence and end with a closing
escape sequence (<?). In PHP script, there is no need to write main
function. And also you can work with PHP without creating a class.
Efficiency: PHP 4.0 introduced resource allocation mechanisms and
more pronounced support for object-oriented programming, in
addition to session management features. Eliminating unnecessary
memory allocation.
Platform Independent: PHP code will be run on every platform,
Linux, Unix, Mac OS X, Windows.
Cross Database: It is integrated with a number of
popular databases, including MySQL, Oracle, Sybase, Informix,
and Microsoft SQL Server.
Security: It supports different types of security functionalities to apply
security to applications like one-way encryption, two-way encryption,
authentication etc.
Flexibility: You can say that PHP is a very flexible language because
of PHP is an embedded language you can embed PHP scripts with
HTML, JAVA SCRIPT, WML, XML, and many others. You can run your
PHP script any device like mobile Phone, tabs, laptops, PC and other
because of PHP script execute on the server then after sending to the
Using php you can create login page for your user.
Using php you can add, delete, modify elements within
yourdatabase thru PHP.
Using PHP, you can restrict users to access some pages of your
website.
It can encrypt data.
PHP performs system functions, i.e. from files on a system it can
create, open, read, write, and close them.
It can handle forms, i.e. gather data from files, save data to a file.
INSTALLATION
1. Double click on downloaded file
2. Click on the Next button
echo $b;
?>
Output: In the above code we defined a variable which named $a. But we call
another variable i.e. $b, which is not defined. So there will be a notice error
produced but execution of the script does not stop, you will see a message Notice
Error! Like in the followingimage:
WARNING:
Warning errors will not stop execution of the script. The main reason for
warning errors are to include a missing file or using the incorrect number of
Prof. Vittal Tubachi
9538626511
parameters in a function.
Example
<?php
echo "Warning Error!!"; include ("Welcome.php");
?>
Output: In the above code we include a welcome.php file, however the
welcome.php file does not exist in the directory. So there will bea warning
error produced but that does not stop the execution of the script i.e. you will
see a message Warning Error !!.
FATAL ERROR:
Fatal errors are caused when PHP understands what you've written;
however what you're asking it to do can't be done. Fatal errors stop the
execution of the script. If you are trying to access the undefined
functions, then the output is a fatal error.
Example
<?php
function fun1()
{
echo "Uday Kumar";
}
fun2();
echo "Fatal Error !!";
?>
Output: In the above code we defined a function fun1 but we call another
function fun2 i.e. func2 is not defined. So a fatal error will be produced that
stops the execution of the script. Like as in the following image.
PARSE ERROR:
The parse error occurs if there is a syntax mistake in the script; the output
is Parse errors. A parse error stops the execution of the script. There are
many reasons for the occurrenceof parse errors in PHP. The common reasons
for parse errors are as follows:
Common reason of syntax errors are:
a. Unclosed quotes
b. Missing or Extra parentheses
c. Unclosed braces
d. Missing semicolon
Example
<?php
echo "C#"; echo "PHP"echo "C";
?>
Output: In the above code we missed the semicolon in the second line. When
that happens there will be a parse or syntax error which stops execution of
the script, as in the following image:
echo $text."\n";
echo $num1."+".$num2."=";echo $num1 + $num2;
?>
Output: Hello, World!
10+20=30
The (.) operator in the above code can be used to concatenate two strings in
PHP and the “\n” is used for a new line and is also known as line-break.
print statement
The PHP print statement is similar to the echo statement and can be
used alternative to echo at many times.
It is also language construct and so we may not use parenthesis: print
or print ().
The main difference between the print and echo statement is that print
statement can have only one argument at a time and thus can print a
single string. Also, print statement always returns a value 1.
Like echo, print statement can also be used to print strings and
variables.
Displaying String of Text: We can display strings with print statement
in the same way we did with echo statements. The only difference is we
can not display multiple strings separated by comma(,) with a single
print statement. Below example shows how to display strings with the
help of PHP print statement:-
<?php
print "Hello, world!";
?>
VARIABLES:
Variable is an identifier which holds data or another one variable and
whose value can be changed at the execution time of script.
Syntax: $variablename=value;
VARIABLE SCOPES
Scope of a variable is defined as its extent in program within whichit
can be accessed, i.e. the scope of a variable is the portion of the program with
in which it is visible or can be accessed.
Depending on the scopes, PHP has three variable scopes:
1. Local variables: The variables declared within a function are called local
variables to that function and has its scope only in that particular function. In
simple words it cannot be accessed outside that function. Any declaration of
a variable outside the function with same name as that of the one within
the function is a complete different variable.
Example:
<?php
$num = 60; function local_var()
{
$num = 50;
echo "local num = $num \n";
}
local_var();
echo "Variable num outside local_var() is $num \n";
?>
function global_var()
{
global $num;
echo "Variable num inside function: $num \n";
}
global_var();
echo "Variable num outside function: $num \n";
?>
Output: Variable num inside function: 20 Variable num outside function :
20
3. Static variable: It is the characteristic of PHP to delete the variable,
ones it completes its execution, and the memory is freed. But sometimes we
need to store the variables even after the completion of function execution.
To do this we use static keyword, and the variables are then called as static
variables.
Example:
<?php
function static_var()
{
static $num = 5;
$sum = 2;
$sum++;
$num++;
echo $num "\n";echo $sum "\n";
}
static_var();
static_var();
?>
Output: 6 3
7 3
Example:
<?php
function myTest()
{
static $x = 0;echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
Output: 0 1 2
CONSTANTS:
Constants are name or identifier that can't be changed during the
execution of the script. In php constants are define in two ways.
1. Using define() function
2. Using const keyword
In php declare constants follow same rule variable declaration.
Constant start with letter or underscore only
Create a PHP Constant
Create constant in php by using define () function.
Syntax: define((name, value, case-insensitive)
DATATYPES
PHP data types are used to hold different types of data or values. PHP
supports the following data types: String, Integer, Float, Boolean, Array,
Object, NULL, and Resource.
Integer:
Integers hold only whole numbers including positive and negative
numbers, i.e., numbers without fractional part or decimal point.
They can be decimal (base 10), octal (base 8) or hexadecimal (base
16).
The default base is decimal (base 10).
The octal integers can be declared with leading 0 and
Prof. Vittal Tubachi
9538626511
Example:
<?php
// decimal base integers
$deci1 = 50;
$deci2 = 654;
$sum = $deci1 + $deci2;echo $sum;
?>
Output: 704
Float(or)double:
It can hold numbers containing fractional or decimal part including
positive and negative numbers.
By default, the variables add a minimum number of decimalplaces.
Example:
<?php
$val1 = 50.85;
$val2 = 654.26;
$sum = $val1 + $val2;echo $sum;
?>
Output: 705.11
String:
It can hold letters or any alphabets, even numbers are included.
These are written within double quotes during declaration.
The strings can also be written within single quotes, but it will be
treated differently while printing variables.
Example:
<?php
$name = "Krishna";
echo "The name of the Geek is $name \n";echo 'The name of the geek is
$name';
?>
Output: The name of the Geek is Krishna The name of the geek is
$name
NULL:
These are special types of variables that can hold only one valuei.e.,
NULL.
We follow the convention of writing it in capital form, but its case
sensitive.
Example:
<?php
$nm = NULL;
echo $nm; // This will give no output
?>
Boolean:
Hold only two values, either TRUE or FALSE.
Successful events will return true and unsuccessful events return
false.
NULL type values are also treated as false in Boolean.
If a string is empty then it is also considered as false in booleandata
type.
Example:
<?php
if(TRUE)
echo "This condition is TRUE";if(FALSE)
echo "This condition is not TRUE";
?>
Output: This condition is TRUE
This condition is not TRUE
Arrays:
Array is a compound data-type which can store multiple values ofsame
data type.
<?php
$intArray = array( 10, 20 , 30);
echo "First Element: $intArray[0]\n"; echo "Second Element: $intArray[1]\n";
echo "Third Element: $intArray[2]\n";
?>
Output: First Element: 10
Second Element: 20
Third Element: 30
Objects:
Objects are defined as instances of user defined classes that can
hold both values and functions.
Resources:
Resources in PHP are not an exact data type. These are basically used
to store references to some function call or to external PHP resources. For
example, consider a database call. This is an external resource.
COMMENTING PHP CODE:
A comment is the portion of a program that exists only for the human
reader and stripped out before displaying the programs result. There are two
commenting formats in PHP −
1. Single-line comments − They are generally used for short explanations
or notes relevant to the local code. Here are the examples of single line
comments.
<?php
# This is a comment, and
# This is the second line of the comment
// This is a comment too. Each style comments onlyprint "An example with
single line comments";
?>
<?php
$x = 10;
$y = 4;
echo($x + $y); // 0utputs: 14
<?php
$x = 10;
echo $x; // Outputs: 10
$x = 20;
$x += 30;
echo $x; // Outputs: 50
$x = 50;
$x -= 20;
echo $x; // Outputs: 30
$x = 5;
$x *= 25;
echo $x; // Outputs: 125
$x = 50;
$x /= 10;
echo $x; // Outputs: 5
$x = 100;
$x %= 15;
echo $x; // Outputs: 10
?>
PHP Comparison Operators: The comparison operators are used tocompare
two values in a Boolean fashion.
Operator Name Example Result
== Equal $x == $y True if $x is equal to $y
<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z); // Outputs: boolean true
?>
<?php
$x = 10;
echo ++$x; // Outputs: 11echo $x; // Outputs: 11
$x = 10;
echo $x++; // Outputs: 10echo $x; // Outputs: 11
$x = 10;
echo --$x; // Outputs: 9echo $x; // Outputs: 9
$x = 10;
echo $x--; // Outputs: 10echo $x; // Outputs: 9
?>
<?php
$x = 50;
$y = 30;
if ($x == 50 and $y == 30)
echo "and Success \n";
if ($x == 50 or $y == 20)
echo "or Success \n";
if ($x == 50 xor $y == 20)
echo "xor Success \n";
if ($x == 50 && $y == 30)
echo "&& Success \n";
if ($x == 50 || $y == 20)
echo "|| Success \n";
if (!$z)
echo "! Success \n";
?>
String Operators: This operator is used for the concatenation of 2 or more
strings using the concatenation operator (‘.’). We can also use the concatenating
assignment operator (‘.=’) to append the argument on the right side to the
argument on the left side
Operator Name Syntax Operation
. Concatenation $x.$y Concatenated $x and $y
.= Concatenation and $x.=$y First concatenates then
<?php
$x = "Geeks";
$y = "for";
$z = "Geeks!!!";
echo $x . $y . $z, "\n";
$x .= $y . $z;
echo $x;
?>