WEB TECHONOLGY (320C5C)
UNIT I :
Introducing PHP – Basic development Concepts – Creating first PHP Scripts – Using
Variable and Operators – Storing Data in variable – Understanding Data types – Setting
and Checking variables Data types – Using Constants – Manipulating Variables with
Operators.
UNIT II:
Controlling Program Flow: Writing Simple Conditional Statements - Writing More
ComplexConditional Statements – Repeating Action with Loops – Working with String and
NumericFunctions.
UNIT III:
Working with Arrays: Storing Data in Arrays – Processing Arrays with Loops and
Iterations –Using Arrays with Forms - Working with Array Functions – Working with
Dates and Times.
UNIT IV:
Using Functions and Classes: Creating User-Defined Functions - Creating Classes –
UsingAdvanced OOP Concepts. Working with Files and Directories: Reading FilesWriting
FilesProcessing Directories.
UNIT V:
Working with Database and SQL : Introducing Database and SQL- Using MySQLAdding
andmodifying Data-Handling Errors – Using SQLite Extension and PDO Extension.
IntroductionXML - Simple XML and DOM Extension.
Introducing PHP:
PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web
development. Developers can create dynamic and interactive websites by embedding PHP code
into HTML. PHP can handle data processing, session management, form handling, and database
integration. The latest version of PHP is PHP 8.4, released in 2024.
Basic development Concepts:
PHP is a server-side scripting language that generates dynamic content on the server and
interacts with databases, forms, and sessions.
PHP supports easy interaction with databases like MySQL, enabling efficient data
handling.
Example:
<?php
echo "Hello, World!";
?>
Output
Hello, World!
In this example
<?php and ?> are the PHP tags that embed PHP code into an HTML document.
echo is a PHP function used to output text to the browser.
"Hello, World!" is the text that will be displayed when the PHP code is executed.
A PHP file is:
A file containing PHP code, used to create dynamic web pages.
It contains HTML, CSS, JavaScript, and PHP code together.
Runs on a web server with PHP installed (e.g., Apache, Nginx), and the output is sent to
the browser as plain HTML.
It is saved with a .php extension.
EXAMPLE:
<?php
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "3");
?>
OUTPUT:
8
Key Features of PHP
Client-Side Scripting: PHP can generate dynamic HTML that is sent to the browser.
While it runs on the server, the content it generates can be displayed in the client’s
browser.
Database Handling: PHP easily connects to databases like MySQL, PostgreSQL, and
SQLite, allowing for efficient data management and storage in web applications.
Simple Learning Curve: PHP’s syntax is easy to learn, especially for those with basic
knowledge of HTML and programming concepts.
Rich Ecosystem: PHP offers many libraries and frameworks like Laravel and Symfony,
which make development faster and more efficient.
Scalability: PHP is highly scalable, allowing developers to create websites that can grow
in terms of traffic and functionality.
Applications of PHP
Web Development: PHP is widely used for creating dynamic websites and web
applications by generating interactive content, handling user inputs, and interacting with
databases.
Command-Line Scripting: PHP can be used to write command-line scripts for automating
repetitive tasks, such as data processing or system monitoring.
Game Development: PHP is used for backend logic in browser-based multiplayer games,
handling user authentication, scores, and game state.
Real-Time Applications: PHP supports real-time applications like chat systems and live
updates, typically using AJAX or WebSockets.
File Management: PHP handles file uploads and creates file management systems for
organizing and downloading files.
Limitations of PHP
Security Risks: If not used properly, PHP can be vulnerable to attacks like SQL
injection, XSS, and CSRF. Developers must follow security best practices.
Not Ideal for Command-Line: While PHP can be run from the command line, it’s
mainly designed for web development, so its performance for general-purpose tasks may
not be as efficient as other languages.
FEATURE:
*Primary Use
Server-side scripting for web development
*Development Speed
Fast for web projects, but limited outside of that
*Hosting
Widely supported on most shared hosting services
*Package Management
Composer for managing dependencies
*Ideal For
Web applications, CMS systems (e.g., WordPress)
Basic development concepts encompass the fundamental ideas and processes related to growth,
progress, and improvement, often in the context of human well-being, societal advancement, or
product creation.
1. Human Development:
Maturation and Learning: Maturation refers to the unfolding of genetically determined
traits, while learning involves acquiring new skills through experience.
Heredity and Environment: Heredity provides the genetic blueprint, while the
environment shapes development through experiences and influences.
2. Economic Development:
GDP and GNP: GDP (Gross Domestic Product) and GNP (Gross National Product) are
common measures of economic growth, but they have limitations in reflecting overall
well-being.
Human Development Index (HDI): HDI considers factors beyond economic output,
such as health and education, to provide a more comprehensive measure of development.
3. Concept Development:
Defining and Refining Ideas:
Concept development involves turning abstract ideas into well-defined, structured concepts
through critical thinking, research, and analysis.
Product Development:
In product development, concept development focuses on understanding user needs, evaluating
features, and refining the product design.
4. General Development:
Multidimensional Process:
Development is a multifaceted process encompassing economic, social, and environmental
improvements.
Quality of Life:
Development is ultimately about improving people's lives, considering factors like health,
education, and living standards.
How to create a PHP script
1. Line 1 – This tag tells the server that you are writing PHP code.
2. Line 2 – You can use the echo function to print out a string of text, this will be displayed
back when the script is run.
3. Line 3 – This tag tells the server that you have stopped writing PHP code.
Creating first PHP Scripts
Step 1: Create a new PHP file
1. Start by creating a new file with a .php extension, such as "first_script.php". This
extension is necessary to indicate that the file contains PHP code.
Step 2: Opening and closing PHP tags
In PHP, you need to enclose your code within opening and closing PHP tags. These tags allow
the PHP interpreter to identify and execute the PHP code within them.
Step 3: Writing your first PHP script
Step 4: Running your PHP script
USING VARIABLE AND OPERATORS:
Variables and operators are fundamental concepts in programming. Variables act as containers to
store data, while operators perform operations on that data. For example, in Python, x =
10 declares a variable x and assigns it the value 10. The = is the assignment operator. Arithmetic
operators like +, -, *, and / perform calculations, while comparison operators like ==, >,
and < compare values.
Variables:
A variable is a named storage location in a computer's memory that holds a value.
Variables can store different data types, such as numbers, text (strings), or true/false
values (booleans).
In most programming languages, you assign a value to a variable using the assignment
operator (=).
For instance, in Python: name = "Alice" assigns the string "Alice" to the variable name.
Operators:
Operators are symbols that perform operations on values and variables.
Arithmetic operators: perform mathematical calculations:
o + (addition), - (subtraction), * (multiplication), / (division), % (modulo)
Assignment operators: assign values to variables:
o = (assignment), += (add and assign), -= (subtract and assign), etc.
Comparison operators: compare values and return a Boolean result (True or False):
o == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or
equal to), <= (less than or equal to).
Logical operators: combine Boolean expressions:
o and, or, not.
Other operators: include bitwise operators, ternary operators, and more, depending on
the programming language.
Examples:
Arithmetic: result = 10 + 5 (result will be 15).
Assignment: x += 5 (equivalent to x = x + 5).
Comparison: if age >= 18: print("Eligible").
Logical: if is_raining and is_sunny: print("Rainbow").
Storing Data in variable:
variables are used to store and manage data within a program. They act as named containers for
values, allowing you to retrieve, modify, and reuse data throughout your code. Variables are
fundamental to dynamic web development, enabling interactive elements and personalized
experiences.
1. Declaration and Initialization:
Declaration: You create a variable by giving it a name (e.g., userName, productPrice).
Initialization: You assign an initial value to the variable using the assignment operator
(=). For example:
o let message = "Hello, world!";
o let count = 0;
o const pi = 3.14159; (using const creates a constant, whose value cannot be
changed)
Dynamic Values: Variables can hold different types of data, including:
o Numbers (integers, decimals)
o Text (strings)
o Boolean values (true/false)
o Arrays (lists of data)
o Objects (complex data structures)
o And more!
2. Accessing and Modifying Data:
Retrieving Values:
Once a variable is declared and initialized, you can access its value by using its name.
Changing Values:
The value of a variable can be updated throughout the program's execution. For example:
let score = 10;
score = score + 5; (The value of score is now 15)
score += 5; (Shortcut for the previous line)
3. Variable Scope:
Local Scope:
Variables declared inside a function or block are only accessible within that function or block.
Global Scope:
Variables declared outside of any function are accessible throughout the entire script.
4. Common Use Cases:
Storing user input: Capturing data from forms, text fields, etc.
Tracking game scores: Maintaining and updating scores, levels, and other game-related
data.
Managing user sessions: Storing information about logged-in users, shopping carts, etc.
Manipulating HTML elements: Changing content, styles, or attributes of elements on a
webpage.
Creating dynamic content: Building interactive and personalized experiences based on
user data.
Understanding Data types
In web technology, data types define the kind of value a variable can hold and the operations that
can be performed on it. They ensure data is stored and handled correctly, influencing how
programs interpret and process information. Common data types include strings, integers,
floating-point numbers, Booleans , and more complex types like objects and arrays.
Here's a breakdown of common data types and their roles:
1. Primitive Data Types:
Numbers: Represent numerical values, including integers (whole numbers) and floating-
point numbers (numbers with decimal points). Examples include 10, 3.14, -5.
Strings: Represent sequences of characters, like text. They are enclosed in single or
double quotes. Examples include "hello", "2024-07-01".
Booleans: Represent logical values, either true or false. They are fundamental for
conditional logic and control flow.
Null: Represents the intentional absence of a value.
Undefined: Represents a variable that has been declared but not yet assigned a value.
Symbol: Represents unique and immutable values, often used as object property keys.
BigInt: Used for representing integers larger than the maximum safe integer for the
Number type.
2. Non-Primitive (or Reference) Data Types:
Objects: Complex data structures that can hold collections of key-value pairs, where
keys are strings and values can be any data type. They allow for representing structured
data like user profiles or product details.
Arrays: Ordered collections of values, where each value can be accessed by its
index. They are useful for storing lists of items or data sets.
Functions: Reusable blocks of code that can be executed to perform specific tasks.
Why are data types important?
Data Integrity: Data types ensure that data is stored and processed as intended,
preventing errors and inconsistencies.
Memory Management: They help the system allocate appropriate memory space for
different types of data.
Code Clarity: Using appropriate data types makes code more readable and
understandable.
Performance: Data types can influence how efficiently data is processed by the system.
Examples in Web Development:
HTML Forms:
Data types are used to validate user input in web forms, ensuring that users enter data in the
expected format (e.g., numbers in a numeric field, text in a text field).
JavaScript:
JavaScript uses data types to determine how variables are handled and what operations can be
performed on them. For instance, you can't perform arithmetic operations on strings unless they
are converted to numbers.
Databases:
Databases use data types to define the structure and storage format of data in tables.
Setting and Checking variables Data types
In web technologies, variables are used to store data, and their data types determine the kind of
values they can hold and the operations that can be performed on them. Data types can be
checked using built-in functions or operators specific to the programming language used in web
development, such as JavaScript, PHP, or Python.
Data Types in Web Technologies:
JavaScript:
Common data types include Number, String, Boolean, BigInt, Symbol, null, and undefined, as
well as objects which can contain other data types.
PHP:
Supports various data types including integers, floats, strings, booleans, arrays, objects, and
more.
Python:
Has built-in data types like numbers (integers, floats, complex), strings, booleans, and collections
like lists, tuples, and dictionaries.
Setting Variables:
JavaScript: Uses var, let, and const keywords to declare variables.
PHP: Variables are declared with a dollar sign ($) followed by the variable name.
Python: Variables are assigned values using the assignment operator (=).
Checking Variable Data Types:
JavaScript: The typeof operator is used to check the data type of a variable.
PHP: The gettype() function returns a string representing the data type of a variable. PHP
also has is_int(), is_string(), is_array(), etc. functions for specific type checks.
Python: The type() function returns the type of an object, which can be used to check the
variable's data type.
Example (PHP):
$age=30;
$name="John";
$isStudent=true;
echogettype($age);
echogettype($name);
echo gettype($isStudent); // Output: Boolean
Using Constants
In web technology, constants are used to store values that should not be changed during the
execution of a program or script. They are useful for representing fixed values like API keys,
database connection strings, or mathematical constants like Pi, enhancing code readability and
maintainability.
Readability: Using meaningful names for constants instead of literal values (like
3.14159 for Pi) makes the code easier to understand.
Maintainability: If a constant value needs to be changed, it only needs to be updated in
one place (the constant definition), rather than in multiple locations throughout the code.
Reduced errors: Constants help prevent accidental modification of important values,
reducing the risk of errors caused by incorrect data.
PHP: define() function or const keyword can be used to define
constants. Constants defined with define() are globally accessible,
while const can be used within classes. For example:
Code
define("PI", 3.14159);
const API_KEY = "your_api_key";
Manipulating Variables with Operators
In web technology, operators are used to manipulate data within variables. These operators
perform various actions like arithmetic calculations, assignments, comparisons, and logical
operations. They are fundamental building blocks for creating dynamic and interactive web
experiences.
1. Assignment Operators:
These operators assign a value to a variable.
The basic assignment operator is =. For example, let x = 10; assigns the value 10 to the
variable x.
Compound assignment operators combine assignment with other operations. For
example:
o += (addition assignment): x += 5 is equivalent to x = x + 5.
o -= (subtraction assignment).
o *= (multiplication assignment).
o /= (division assignment).
o %= (modulo assignment).
2. Arithmetic Operators:
These operators perform mathematical calculations.
Examples include:
o + (addition).
o - (subtraction).
o * (multiplication).
o / (division).
o % (modulo - returns the remainder of a division).
3. Comparison Operators:
These operators compare two values and return a boolean result ( true or false ).
Examples include:
o == (equal to).
o != (not equal to).
o === (strictly equal to - checks for both value and type).
o !== (strictly not equal to).
o > (greater than).
o < (less than).
o >= (greater than or equal to).
o <= (less than or equal to).
4. Logical Operators:
These operators perform logical operations on boolean values.
Examples include:
o && (logical AND - returns true if both operands are true).
o || (logical OR - returns true if at least one operand is true).
o ! (logical NOT - inverts the boolean value of an operand).
5. Increment/Decrement Operators:
These operators increase or decrease the value of a variable by 1.
++ (increment).
-- (decrement).
They can be used as prefix ( ++x, --x ) or postfix ( x++, x-- ) operators, with subtle
differences in their behavior.
6. String Operators:
In some languages (like JavaScript), the + operator can also be used to concatenate
strings.