HTTP Basics
1. HTTP stands for Hypertext transfer protocol.
2. A protocol is a set of rules that govern the way
two or more computer communicate with one
another.
3. It is used to transfer data across the web.It is
connectionless and stateless application layer
protocol.
4. It is based on client-server architecture where
web browser acts as clients and web server acts
as a server.
5. The client also called HTTP client and server
also called HTTP server.
6. The client sends HTTP request message also
called HTTP request and server returns
response message also called as HTTP
response.
HTTP Message
HTTP Message is used to show how data is exchanged
between the client and the server. It is based on client-
server architecture. An HTTP client is a program that
establishes a connection to a server to send one or more
HTTP request messages. An HTTP server is a program
that accepts connections to serve HTTP requests by
sending an HTTP response messages.
Messages are simple and line oriented sequence
of character. There are two types of HTTP message
request message and response message.
HTTP message sent from web clients to web
server are called request message and message from
server to clients are called response message.
Request Line Status Line
Headers Headers
A Blank Line A Blank Line
Body Body
Request Message Response Message
1. HTTP Request Message
An HTTP request message is a message sent by a client to
a server to initiate an action. The request message
contains a request-line, HTTP headers, and optionally, a
message body:
1. Request Line: An HTTP request starts with a request
line, which includes the following:
Method: This indicates the action to be performed on
the resource. Common methods include GET (retrieve
a resource), POST (submit data to the server), PUT
(update a resource), DELETE (remove a resource), and
others.
Request URL: The URL of the resource being
requested.
HTTP Version: The version of the HTTP protocol
being used.
1. Request Headers: Following the request line are
headers, which provide additional information about
the request. This can include details about the client’s
browser, the types of responses that the client will
accept, cookies, and more.
2. Blank Line: A blank line indicates the end of the
headers section.
3. Request Body (Optional): Not all requests have a
body. Bodies are typically included in POST or PUT
requests, where you’re sending data to the server (like
form inputs or file uploads). The body contains the data
being sent.
2. HTTP Response Message
An HTTP response is what a server sends back to the
client after receiving and processing an HTTP
request. It is a key part of the data exchange in web
communications, essentially being the server’s
answer to the client’s request.
1. Status Line: This is the first line of the response and
includes:
HTTP Version: Indicates the HTTP protocol version
used (e.g., HTTP/1.1).
Status Code: A three-digit number that indicates the
outcome of the request. Common status codes include
200 (OK, request succeeded), 404 (Not Found, the
resource can’t be found), 500 (Internal Server Error),
etc.
Status Text: A brief, human-readable explanation of
the status code (e.g., OK, Not Found, Internal Server
Error).
1. Response Headers: These are key-value pairs
providing additional information about the response.
They can include details such as the server type,
content type, content length, caching policies, set
cookies, and other metadata.
2. Blank Line: A blank line signifies the end of the
header section.
3. Response Body: This part of the response contains the
actual data or resource that the client requested. For
example, it could be an HTML file, JSON data, an
image, etc. In some cases, particularly when the
response indicates an error (like a 404), the body might
contain a message explaining the error.
EgHTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56
GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed
PHP BASICS
1. PHP (Hypertext Preprocessor) is a versatile and
widely used server-side scripting language for
creating dynamic and interactive web
applications.
2. A script is a set of programming instructions
that is interpreted at runtime.
3. A server side scripts are interpreted on the
server while client side scripts are interpreted by
the client.
4. Php is an open source,interpreted and object
oriented scripting language.
5. It contains text,html,css,js and PHP code and the
result is returned to the browser as plain HTML.
6. The latest version of php is 8.3 was released on
NOV 23,2023.
7. It is integrated with databases including
MySQL,PostgreSQL,Oracle,Microsoft SQL
server.
Advantages of PHP :
1. The most important advantage of PHP is that it’s open-
source and free from cost. It can be downloaded
anywhere and is readily available to use for events or
web applications.
2. It is platform-independent. PHP-based applications can
run on any OS like UNIX, Linux, Windows, etc.
3. Applications can easily be loaded which are based on
PHP and connected to the database. It’s mainly used
due to its faster rate of loading over slow internet
speed than other programming language.
4. It has less learning curve because it is simple and
straightforward to use. Someone familiar with C
programming can easily work on PHP.
5. It is more stable for a few years with the assistance of
providing continuous support to various versions.
6. It helps in reusing an equivalent code and not got have
to write lengthy code and sophisticated structure for
events of web applications.
7. It helps in managing code easily.
8. It has powerful library support to use various function
modules for data representation.
9. PHP’s built-in database connection modules help in
connecting databases easily reducing trouble and time
for the development of web applications and content-
based sites.
10. The popularity of PHP gave rise to various
communities of developers, a fraction of which may be
potential candidates for hire.
11. Flexibility makes PHP ready to effectively combine
with many other programming languages.
Disadvantages of PHP :
1. It is not that secure due to its open-source, because the
ASCII text file is often easily available.
2. It is not suitable for giant content-based web
applications.
3. It has a weak type, which can cause incorrect data and
knowledge to users.
4. PHP frameworks got to learn to use PHP built-in
functionalities to avoid writing additional code.
5. Using more features of PHP framework and tools
cause poor performance of online applications.
6. PHP doesn’t allow change or modification in the core
behavior of online applications.
7. The PHP frameworks aren’t equivalent in behavior so
does their performance and features.
8. While PHP may be a powerful tool supported by an
outsized community and plentiful reference
documentation, there are easier programming
languages for web apps.
9. It is widely believed by the developers that PHP
features a poor quality of handling errors. PHP lacks
debugging tools, which are needed to look for errors
and warnings. PHP has less number of debugging tools
in comparison to other programming languages.
10. It’s highly tough to manage because it’s not
competent modular. It already imitates the features of
the Java language.
Uses of PHP
Features of PHP
Basic Syntax of PHP
You can run php code on any web browser. PHP script
is executed on the server, and the plain HTML result is
sent back to the browser.
Basic Syntax of PHP
PHP code is start with <?php and ends with ?>
Every PHP statements end with a semicolon (;).
PHP code save with .php extension.
PHP contain some HTML tag and PHP code.
You can place PHP code any where in your document.
PHP Syntax
<?php
// PHP code goes here
?>
PHP files save with .php extension and it contain some
HTML and PHP code.
PHP Syntax
<!DOCTYPE html>
<html>
<body>
<h1>This is my first PHP code</h1>
<?php
echo"Hello World!";
?>
</body>
</html>
Case sensitivity
Variable name is case sensitive
All variables names are case sensitive, these include
normal variables and superglobals such as
$_GET,$_POST,$_REQUEST,$_COOKIE,$_SESSI
ON,$_GLOBALS etc.
<?php
$abc = 'abcd';
echo $abc; //Output 'abcd'
echo $aBc; //No output
echo $ABC; //No output
Constant name by default is case sensitive, usually
use UPPER CASE for constant name
<?php
define("ABC","Hello World");
echo ABC; //Output Hello World
echo abc; //Output abc
Function name, method name or class name are case
insensitive, but we recommend that you use the same
name as you define them.
Function name
<?php
function show(){
echo "Hello World";
}
show(); //Output Hello World
SHOW(); //Output Hello World
Statements and semicolons
PHP requires instructions to be terminated with a
semicolon at the end of each statement. The closing
tag of a block of PHP code automatically implies a
semicolon; you do not need to have a semicolon
terminating the last line of a PHP block.
Eg<?php
echo “hello”;
$a=1;
$b=$a+$b;
echo $b
?>
Whitespaces and line breaks
If there is more than one white space in a string; then
browser collapses all those white spaces to a single
white space. Hence to add more than one white space
in between words or characters we can use html
entity
- indicates non breakable white space, which adds a
single space.
We can use any number of times to add more white
spaces.
Example Code:
<?php
echo "Hello World!";
?>
Line break in PHP
To add line break we can use html br tag within a
string.
htmlbr tag moves the cursor to Nextline so that from
there on Nextcontent can be placed or written.
Example Code:
<?php
echo "Hello World <br/>Hello World <br/>Hello
World <br/><br/>";
Comments
PHP comments can be used to describe any line of code
so that other developer can understand the code easily. It
can also be used to hide any code.
PHP supports single line and multi line comments. These
comments are similar to C/C++ and Perl style (Unix shell
style) comments.
PHP Single Line Comments
There are two ways to use single line comments in PHP.
o // (C++ style single line comment)
o # (Unix Shell style single line comment)
<?php
// this is C++ style single line comment
# this is Unix Shell style single line comment
echo "Welcome to PHP single line comments";
?>
PHP Multi Line Comments
In PHP, we can comments multiple lines also. To do so,
we need to enclose all lines within /* */. Let's see a simple
example of PHP multiple line comment.
<?php
/*
Anything placed
within comment
will not be displayed
on the browser;
*/
echo "Welcome to PHP multi line comment";
?>
Literals
A literal is a data value in php that appears directly in a
program.
Eg “hello”
100
True
Null
Identifiers
An identifier is simply a name. In PHP, identifiers are
used to name variables, functions, constants, and classes.
The first character of an identifier must be either an
ASCII letter (uppercase or lowercase), the underscore
character (_), or any of the characters between ASCII
0x7F and ASCII 0xFF.
1. Variable names-it starts with a dollar
sign($),variable name is case sensitive.
Eg $a
$bill_amt
$GrossSal
$_score
2. Function names-its not case sensitive
Egadd(),Add(),ADD(),ADd()
3. Class names-its not case sensitive
Eg person or Person is same.
Constants
Constants variables are used to store information which is
like variables. The main difference between constants and
variables is that constant value cannot be changed in the
running program, where in variables value can change.
Constant is case sensitive,constant identifier are
always uppercase.
Constants Syntax :
mixed constant ( string $name )
Return the value of the constant indicated by name.
if you need to take the value of a constant , will not
know its name. example it is stored in a variable or
returned by a function.
The boolean , integer , float and string can be contained
in constants.which is possible to define constants as a
resource.
In php use define() function to create constant.It defines
constant at runtime
Syntax - define(“constant_name”,value)
Const keyword also defines constant at compile time,it is
always case sensitive.
Eg<?php
const MESSAGE=”hello”;
echo MESSAGE;
?>
Keywords
In PHP there are certain words that’s reserved for a
special use. We cannot use these words when naming our
variables, constants, arrays, functions, interfaces and
classes.
These keywords have special meaning and is only to be
used in special contexts. While there is nothing strictly
stopping us from prefixing a keyword with a $ sign, it is
conventionally considered bad practice, and it will make
code difficult and slow to read.
Egbreak,case,catch,class,do,for,while,new,or,ifelse,elseif,
public,static etc.