0% found this document useful (0 votes)
3 views

Saideepthi WT Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Saideepthi WT Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

What is Web Technologies

Web technologies refer to the tools, techniques, and frameworks used to create and

manage websites, web applications, and web-based services. They are the backbone

of the internet, enabling users to interact with content, services, and applications

online.

Why Do We Use Web Technologies?

1. To Build and Maintain Websites:

○ Create static websites (e.g., portfolios) or dynamic applications (e.g.,

e-commerce platforms, social media sites).

2. Enable User Interaction:

○ Allow users to interact with websites via forms, navigation, or dynamic

content updates.

3. Facilitate Seamless Communication:

○ Enable communication between servers, databases, and browsers.

4. Enhance User Experience:

○ Deliver visually appealing, responsive, and fast-loading websites.


Different Types Of Web Development

1. Front-End Development (Client-Side)

● Languages: HTML, CSS, JavaScript

● Frameworks/Libraries: React.js, Angular, Vue.js, Bootstrap, Tailwind CSS

● Tools: Figma, Adobe XD (for UI/UX design), Chrome Developer Tools

2. Back-End Development (Server-Side)

● Languages: Python, PHP, Ruby, Java, JavaScript (Node.js), C#

● Frameworks: Django, Flask, Express.js, Ruby on Rails, ASP.NET

● Databases: MySQL, PostgreSQL, MongoDB, Firebase

3. Full-Stack Development

● Combining front-end and back-end technologies (e.g., MERN Stack: MongoDB,

Express.js, React, Node.js)


UNIT - 1

1.1 Introduction to PHP

PHP Introduction

PHP is a server-side scripting language created primarily for web development but it

is also used as a general-purpose programming language. Unlike client-side

languages like JavaScript, which are executed on the user’s browser, PHP scripts run

on the server. The results are then sent to the client’s web browser as plain HTML.

1. PHP stands for - Hypertext Preprocessor.

2. PHP was developed by "Rasmus Lerdorf".

3. PHP is a server-side scripting language designed specifically for web

development.

4. It is an open-source which means it is free to download and use.

5. It is very simple to learn and use.

6. The file extension of PHP is “.php”

7. PHP is an interpreted language, i.e. there is no need of compilation. And PHP is

naturally used for form handling and database access.


Characteristics of PHP

● PHP code is executed on the server.

● It can be integrated with many databases such as Oracle, Microsoft SQL Server,

MySQL, PostgreSQL, Sybase, and Informix.

● It is powerful to hold a content management system like WordPress and can be

used to control user access.

● (Content Management System (CMS) is a software application that allows

users to create, manage, and modify digital content on a website without

requiring extensive technical knowledge of programming or web development.)

● It supports main protocols like HTTP Basic, HTTP Digest, IMAP, FTP, and

others.

● Websites like www.facebook.com and www.yahoo.com are also built on PHP.

● One of the main reasons behind this is that PHP can be easily embedded in

HTML files and HTML codes can also be written in a PHP file.

● The thing that differentiates PHP from the client-side language like HTML is,

PHP codes are first executed on the server and then the result is returned to

the browser.

● The only information that the client or browser knows is the result returned

after executing the PHP script on the server and not the actual PHP codes

present in the PHP file.

● PHP files can support other client-side scripting languages like CSS and

JavaScript.
Working of PHP

PHP scripts are executed on the server. Here’s a typical flow of how PHP works:

● A user requests a PHP page via their web browser.

● The server processes the PHP code. The PHP interpreter parses the script,

executes the code, and generates HTML output.

● The server sends the generated HTML back to the client’s browser, which

renders the web page.

This server-side processing allows for dynamic content generation and ensures that

sensitive code is not exposed to the client.

Client-Server model (Request-Response cycle)


In the Clint-Server Model, the client sends a request to the server, then the server

receives the request and processes it along with database interactions (if required)

and generates the response in the client understandable format, and then the server

sends that response to the client.

In the above Client-Server model, the web browser can be used as a client and it

understands simple stuff like HTML, JavaScript and CSS, Apache can be used as a

server and it translates PHP script into a browser understandable format and MySQL

can be used as a database.

Features of PHP

● Dynamic Typing: PHP is dynamically typed, meaning you don’t need to

declare the data type of a variable explicitly.

● Cross-Platform: PHP runs on various platforms, making it compatible with

different operating systems.

● Database Integration: PHP provides built-in support for interacting with

databases, such as MySQL, PostgreSQL, and others.

● Server-Side Scripting: PHP scripts are executed on the server, generating

HTML that is sent to the client’s browser.

Advantages of PHP

● Open Source: PHP is an open-source language, meaning it is freely

available for anyone to use and distribute. This openness has fostered a
large and active community of developers who continuously contribute to its

growth, improvement, and feature development.

● Easy to Learn: The syntax of PHP is quite similar to C and other

programming languages. This makes PHP relatively easy to learn, especially

for developers who already have some programming experience. Beginners

find it approachable due to its straightforward syntax and extensive online

resources.

● Web Integration: PHP is designed specifically for web development and is

embedded within HTML. It seamlessly integrates with various web

technologies, facilitating the creation of dynamic and interactive web pages.

● Database Support: PHP has excellent support for various databases,

including MySQL, PostgreSQL, SQLite, and more. This makes it easy to

connect and interact with databases, a crucial aspect of many web

applications.

● Cross-Platform Compatibility: PHP is platform-independent and runs on

various operating systems, including Windows, Linux, macOS, and others.

This ensures compatibility across different environments.

● Large Community and Documentation: PHP has a vast and active

community of developers. The abundance of online resources, tutorials, and


documentation makes it easier for developers to find solutions and seek

help when needed.

● Frameworks and CMS: There are popular PHP frameworks like Laravel,

Symfony, and CodeIgniter, which provide pre-built modules and features,

aiding in rapid development. Additionally, PHP supports widely used

content management systems (CMS) like WordPress and Joomla.

● Server-Side Scripting: PHP scripts are executed on the server, reducing the

load on the client’s side. This server-side scripting capability is crucial for

generating dynamic content and performing server-related tasks.

● Community Support: The PHP community actively contributes to the

language’s development, ensuring regular updates, security patches, and

improvements.

Disadvantages of PHP

● Inconsistency: PHP has been criticized for inconsistencies in function names

and parameter orders. This can lead to confusion for developers, especially

when working with a mix of older and newer functions.

● Security Concerns: If not handled properly, PHP code may be susceptible to

security vulnerabilities, such as SQL injection and cross-site scripting (XSS).


Developers need to be cautious and follow best practices to secure PHP

applications.

● Performance: While PHP performs well for many web applications, it may

not be as fast as some compiled languages like C or Java. However,

advancements and optimizations in recent versions have improved

performance.

● Lack of Modern Features: Compared to newer languages, PHP may lack

some modern language features. However, recent versions of PHP have

introduced improvements and features to address this concern.

● Scalability Challenges: PHP can face challenges when it comes to scaling

large and complex applications. Developers may need to adopt additional

tools or frameworks to address scalability issues.

● Not Suitable for Large-Scale Applications: While PHP is suitable for small

to medium-sized projects, it might not be the best choice for extremely large

and complex applications where more structured languages might be

preferred.

● Limited Object-Oriented Programming (OOP) Support: Although PHP

supports OOP, its implementation has been criticized for not being as robust
as in some other languages. However, recent versions have introduced

improvements to enhance OOP capabilities.

1.2 Syntax for PHP

These tags are also called ‘Canonical PHP tags’. Everything outside of a pair of

opening and closing tags is ignored by the PHP parser. The open and closing tags are

called delimiters. Every PHP command ends with a semi-colon (;)


Example:

1.3 Comments in PHP

Comments are used to make code more readable by explaining the purpose of specific

code blocks. Comments are ignored by the PHP interpreter.

1.3.1 Single Line Comment

As the name suggests, these are single line or short relevant explanations that one

can add to their code. To add this, we need to begin the line with (//) or (#).

Ex: // This is single line comment


1.3.2 Multi-Line or Multiple Line Comment

It is used to accommodate multiple lines with a single tag and can be extended to

many lines as required by the user. To add this, we need to begin and end the line

with (/*…*/) ,

Ex: /* This

Is

Multiline comment */
1.4 Variables
1.4.1 Declaring Variables
A variable is a named memory location in which we can store values for the

particular program.In other words, Variable is a name of memory location and

used to hold the value.

● Variables are "containers" for storing information.

● In PHP, a variable is created using $ symbol followed by variable name.

● A variable can have a short name (like $x and $y) or a more descriptive name

($age, $carname, $total_volume).

● In PHP, The variable names are case - sensitive, so the variable name $a is

different from $A
Syntax for variable declaration:

$variablename = value;

Examples:

Variable Naming Conventions:

In PHP, it’s important to follow certain naming conventions for PHP variables to

ensure readability and maintainability:

1. Start with a Letter or Underscore: Variable names must begin with a letter

or an underscore (_), not a number.

2. Use Descriptive Names: Variable names should be descriptive of their

purpose, e.g., $userName, $totalAmount.

3. Case Sensitivity: PHP variable names are case-sensitive, meaning $name

and $Name are different variables.

4. Avoid Reserved Words: Do not use PHP reserved words or keywords as

variable names (e.g., function, class, echo).


1.4.2 Variable scope

The scope of a variable is the part of the script where the variable can be

referenced/used.

PHP has three different variable scopes:

● local

● global

● Static

1. Local Scope or Local Variable

A variable declared within a function has a LOCAL SCOPE and can only be accessed

within that function

Any declaration of a variable outside the function with the same name (as within the

function) is a completely different variable.

<?php

$num = 60;

function local_var() {

// This $num is local to this function

// The variable $num outside the function


// is a completely different
$num = 50;
echo "Variable num inside function is: $num <br><br>";
}

local_var();

// The $num outside function is a completely


// different from inside local_var()
echo "Variable num outside function is: $num";

?>

O/P:

2. Global Scope or Global Variable

The variables declared outside a function are called global variables. These

variables can be accessed directly outside a function. To get access within a


function we need to use the “global” keyword before the variable to refer to

the global variable.

Ex:

<?php

$num = 20;

// Function to demonstrate use of global variable


function global_var() {

// We have to use global keyword before


// the variable $num to access within
// the function
global $num;

echo "Variable num inside function: $num <br> <br>";


}

global_var();

echo "Variable num outside function: $num <br> <br>";

?>

o/p:
2.1 .PHP The global Keyword

The global keyword is used to access a global variable from within a function.

To do this, use the global keyword before the variables (inside the function)

Ex:

<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$y = 10;

function myTest() {
global $x, $y;
$y = $x + $y;
}

myTest(); // run function


echo $y; // output the new value for variable $y
?>

</body>
</html>

o/p:
3. The static Keyword

when a function is completed/executed, all of its variables are deleted. But sometimes

we need to store the variables even after the completion of function execution. To do

this, we use the static keywords and the variables are then called static variables

<?php

// Function to demonstrate static variables


function static_var() {

// Static Variable
static $num = 5;
$sum = 2;

$sum++;
$num++;

echo $num, "<br><br>";


echo $sum, "<br><br>";
}

// First function call


static_var();

// Second function call


static_var();
?>

o/p:

NOTE: You must have noticed that $num regularly increments even after the first

function call but $sum doesn’t. This is because $sum is not static, and its memory is

freed after the execution of the first function call.

Example for local, global and static Scope of Variables:

<?php
// Global variable
$globalVar = 10;

function testScope() {
// Local variable
$localVar = 20;
// Accessing global variable
global $globalVar;
echo "Global Variable: $globalVar <br>";
echo "Local Variable: $localVar <br>";

// Static variable
static $staticVar = 0;
$staticVar++;
echo "Static Variable: $staticVar <br> ";
}

// Calling the function multiple times


testScope();

echo "<br><br>";

testScope();

echo "<br><br>";

testScope();

o/p:
1.4.3 PHP $ and $$ Variables

The $var (single dollar) is a normal variable with the name var that stores any value

like string, integer, float, etc.

The $$var (double dollar) is a reference variable that stores the value of the $variable

inside it.

<?php
$x = "abc";
$$x = 200;
echo $x."<br/>";
echo $$x."<br/>";
echo $abc;
?>
o/p:

1.5 Data Types in PHP


PHP is a loosely typed language, which means variables do not need to be

declared with a specific data type. PHP allows eight different types of data

types.

● Predefined Data Types/Scalar Types:

○ Integer

○ Float (Double)

○ String

○ Boolean

● User-Defined Data Types/Compound Types:

○ Array

○ Objects
● Special Data Types

○ NULL

○ Resources

1.5.1 Predefined Data Types/Scalar Types:

Integer :
In PHP, Integers hold whole numbers including positive and negative numbers (... -3, -2,
-1, 0, 1, 2, 3, ...). i.e., numbers without decimal point. They can be decimal (base 10),
octal (0) or hexadecimal (0x) optionally preceded by + or - sign.

<?php
// Integer Example
$integer = 42;
echo "Integer: " . $integer . "<br>";
?>
o/p:

Rules for integer:

○ An integer can be either positive or negative.


○ An integer must not contain decimal point.
○ Integer can be decimal (base 10), octal (base 8), or hexadecimal
(base 16).
○ The range of an integer must be lie between 2,147,483,648 and
2,147,483,647 i.e., -2^31 to 2^31.

Float :

A float (floating point number) is a number with a


decimal point. It hold numbers containing fractional or
decimal part including positive and negative numbers.
<?php
// Float (Double) Example
$float = 3.14;
echo "Float: " . $float . "<br>";
?>

String :

A string is a sequence of characters which are


enclosed with single quotes or double quotes.

<?php
// String Example
$string = "Hello, World!";
echo "String: " . $string . "<br>";
?>
Boolean :
Boolean data types are used in conditional testing. Hold only two values,

either TRUE(1) or FALSE(0). Successful events will return true and

unsuccessful events return false. NULL type values are also treated as

false in Boolean.

<?php
// Boolean Example
$booleanTrue = true;
$booleanFalse = false;
echo "Boolean True: " . var_export($booleanTrue, true) . "<br>";
echo "Boolean False: " . var_export($booleanFalse, true) . "<br>";
?>
1.5.2 User-Defined Data Types/Compound Types:

Array :

An array datatype can hold more than one value at a


time.In other words, An array is collection of elements
or items.
<?php
// Array Example
$array = array(1, 2, 3, 4, 5);
echo "Array: <br>";
print_r($array);
echo "<br>";
?>
o/p:

Object :

Classes and objects are the two main aspects of object-oriented


programming.

A class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the properties
and behaviors from the class, but each object will have different values
for the properties.

Objects are created based on user defined class via the


new keyword
<?php
// Object Example
class Person {
public $name;
public $age;
function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}

function greet() {
return "Hello, my name is " . $this->name . " and I am " .
$this->age . " years old.";
}
}

$person = new Person("Alice", 30);


echo "Object: " . $person->greet() . "<br>";
?>

1.5.2 Special Data Types

Null :

In PHP, Null is a special data type that has only one


value: NULL, And it is case sensitive you must write in
capital letters.The special type of data type NULL
defined a variable with no value.
<?php
// NULL Example
$nullVar = null;
echo "NULL: " . var_export($nullVar, true) . "<br>";
?>

Resource :
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. Resource

variables hold special handles for files and database connections.

<?php
// Resource Example
$file = fopen("sample.txt", "w");
echo "Resource: " . get_resource_type($file) . "<br>";

// Clean up resource
fclose($file);
?>
Functions

A function is a block of code written in a program to perform some specific


task.

They take information as a parameter, execute a block of statements or


perform operations on these parameters and return the result.

PHP provides us with two major types of functions:

Built-in functions : PHP provides us with a huge collection of


built-in library functions. These functions are already coded and
stored in the form of functions.

Ex: strpos(),var_dump(),array(),strtoupper(),echo(),implode()

User Defined Functions: PHP allows us to create our own


customised functions called the user-defined functions.

syntax:
function function_name()

--------// code—------

calling a function: by using function name

Simple Function to Print a Greeting Message

<?php

// Define a function

function sayHello() {

echo "Hello, welcome to PHP programming!";

// Call the function

sayHello();

?>

o/p:
2. Function Parameters or Arguments in PHP

Syntax:

function function_name($parameter1, $parameter2)

executable code;

Parameters or arguments are variables passed to a function so it can


perform tasks based on the input values. This makes functions dynamic and
reusable.they accept input values during runtime.

<?php

// Define a function with parameters

function greetUser($name, $timeOfDay) {

echo "Good $timeOfDay, $name!";

}
// Call the function with arguments

greetUser("Alice", "morning"); // Pass 'Alice' and 'morning' as arguments

?>

o/p:

3. Setting Default Values for Function Parameters in PHP

In PHP, you can assign default values to function parameters. If an argument


is not provided during the function call, the parameter takes its default
value.

<?php

// function with default parameter


function defGeek($str, $num=12)

echo "$str is $num years old <br><br>";

// Calling the function

defGeek("Ram", 15);

// In this call, the default value 12

// will be considered

defGeek("Adam");

?>

o/p:
4. Returning Values from Functions
Functions can also return values to the part of program from where it is
called.

The return keyword is used to return value back to the part of program,
from where it was called.

The returning value may be of any type including the arrays and objects.

The return statement also marks the end of the function and stops the
execution after that and returns the value.

<?php

// function along with three parameters

function proGeek($num1, $num2, $num3)

$product = $num1 * $num2 * $num3;

return $product; //returning the product

}
// storing the returned value

$retValue = proGeek(2, 3, 5);

echo "The product is $retValue";

?>

o/p:

5. Parameter passing to Functions

PHP allows us two ways in which an argument can be passed into a function:

5. 1. Pass by Value
When passing by value, PHP creates a copy of the argument and passes it to
the function.

Any changes made to the parameter inside the function will not affect the
original variable outside the function.

Example: Pass by Value

<?php

// Function with pass by value

function addFive($num) {

$num = $num + 5; // Modify the local copy

echo "Inside function: $num<br><br>"; // Prints modified value inside


function

}
// Calling the function

$value = 10;

addFive($value);

// Outside the function, the original value remains unchanged

echo "Outside function: $value<br><br>"; // Prints 10

?>

o/p:

5.2. Pass by Reference


When passing by reference, the memory address of the argument is passed
to the function, not a copy.

Any changes made to the parameter inside the function will affect the
original variable outside the function.

To pass by reference, we use the ampersand (&) symbol before the


parameter in the function definition.

Example: Pass by Reference


<?php

// Function with pass by reference

function addFiveByReference(&$num) {

$num = $num + 5; // Modify the original value

echo "Inside function: $num<br><br>"; // Prints modified value inside


function

// Calling the function

$value = 10;

addFiveByReference($value);

// Outside the function, the original value has been modified


echo "Outside function: $value<br><br>"; // Prints 15

?>

o/p

Key Differences Between Pass by Value and Pass by Reference:

Aspect Pass by Value Pass by Reference

Definition A copy of the argument is The address of the original


passed to the function. variable is passed.

Effect on The original value remains The original value is modified.


Original unchanged.

How to Pass No special syntax needed. Use the & (ampersand)


symbol before the parameter.
When to When you don't want to When you need to modify
Use modify the original value. the original variable.
Functions

A function is a block of code written in a program to perform some specific


task.

They take information as a parameter, execute a block of statements or


perform operations on these parameters and return the result.
PHP provides us with two major types of functions:

Built-in functions : PHP provides us with a huge collection of


built-in library functions. These functions are already coded and
stored in the form of functions.

Ex: strpos(),var_dump(),array(),strtoupper(),echo(),implode()

User Defined Functions: PHP allows us to create our own


customised functions called the user-defined functions.

syntax:

function function_name()

--------// code—------

calling a function: by using function name

Simple Function to Print a Greeting Message

<?php

// Define a function

function sayHello() {

echo "Hello, welcome to PHP programming!";


}

// Call the function

sayHello();

?>

o/p:

2. Function Parameters or Arguments in PHP

Syntax:

function function_name($parameter1, $parameter2)

executable code;

}
Parameters or arguments are variables passed to a function so it can
perform tasks based on the input values. This makes functions dynamic and
reusable.they accept input values during runtime.

<?php

// Define a function with parameters

function greetUser($name, $timeOfDay) {

echo "Good $timeOfDay, $name!";

// Call the function with arguments

greetUser("Alice", "morning"); // Pass 'Alice' and 'morning' as arguments

?>

o/p:

3. Setting Default Values for Function Parameters in PHP


In PHP, you can assign default values to function parameters. If an argument
is not provided during the function call, the parameter takes its default
value.

<?php

// function with default parameter

function defGeek($str, $num=12)

echo "$str is $num years old <br><br>";

// Calling the function

defGeek("Ram", 15);

// In this call, the default value 12

// will be considered

defGeek("Adam");

?>
o/p:

4. Returning Values from Functions


Functions can also return values to the part of program from where it is
called.

The return keyword is used to return value back to the part of program,
from where it was called.

The returning value may be of any type including the arrays and objects.

The return statement also marks the end of the function and stops the
execution after that and returns the value.

<?php

// function along with three parameters

function proGeek($num1, $num2, $num3)

{
$product = $num1 * $num2 * $num3;

return $product; //returning the product

// storing the returned value

$retValue = proGeek(2, 3, 5);

echo "The product is $retValue";

?>

o/p:

5. Parameter passing to Functions

PHP allows us two ways in which an argument can be passed into a function:
5. 1. Pass by Value

When passing by value, PHP creates a copy of the argument and passes it to
the function.

Any changes made to the parameter inside the function will not affect the
original variable outside the function.

Example: Pass by Value

<?php

// Function with pass by value

function addFive($num) {

$num = $num + 5; // Modify the local copy


echo "Inside function: $num<br><br>"; // Prints modified value inside
function

// Calling the function

$value = 10;

addFive($value);

// Outside the function, the original value remains unchanged

echo "Outside function: $value<br><br>"; // Prints 10

?>

o/p:
5.2. Pass by Reference

When passing by reference, the memory address of the argument is passed


to the function, not a copy.

Any changes made to the parameter inside the function will affect the
original variable outside the function.

To pass by reference, we use the ampersand (&) symbol before the


parameter in the function definition.

Example: Pass by Reference


<?php

// Function with pass by reference

function addFiveByReference(&$num) {

$num = $num + 5; // Modify the original value

echo "Inside function: $num<br><br>"; // Prints modified value inside


function

// Calling the function

$value = 10;

addFiveByReference($value);
// Outside the function, the original value has been modified

echo "Outside function: $value<br><br>"; // Prints 15

?>

o/p

Key Differences Between Pass by Value and Pass by Reference:

Aspect Pass by Value Pass by Reference

Definition A copy of the argument is The address of the original


passed to the function. variable is passed.

Effect on The original value remains The original value is modified.


Original unchanged.
How to Pass No special syntax needed. Use the & (ampersand)
symbol before the parameter.

When to When you don't want to When you need to modify


Use modify the original value. the original variable.

You might also like