Web Programming
Web Programming
The term "Hypertext" in HTML refers to the way Doctype: The <!DOCTYPE> declaration is the
web pages are interconnected through links. very first line of an HTML document. It informs
These links allow users to navigate between the web browser about the version of HTML
different web pages by simply clicking on them. being used in the document. The doctype
In HTML, links are represented by the <a> tag, declaration is important because it helps the
which stands for anchor. browser interpret the HTML correctly. The most
common doctype declaration for modern HTML
HTML is considered a markup language because
documents is <!DOCTYPE html>.
it uses tags to mark up or define different
elements within a document. These tags are HTML: The <html> element is the root element
enclosed in angle brackets (< >) and provide of an HTML document. It serves as a container
instructions to web browsers on how to interpret for all the other elements of the page. The
and display the content. For example, the <h1> opening tag <html> is placed at the beginning of
tag is used to mark a heading, the <p> tag the document, and the closing tag </html> is
represents a paragraph, and the <ul> tag defines placed at the end. All other elements are nested
an unordered list. inside the <html> element.
Originally, HTML was developed with the Head: The <head> element is a container for
purpose of structuring scientific documents to metadata and other non-visible elements in an
facilitate the sharing of information among HTML document. It is placed immediately after
researchers. It provided a standardized way to the opening <html> tag. The content inside the
represent headings, paragraphs, lists, and other <head> element is not displayed on the web
elements commonly found in scientific papers. page itself, but it provides important information
to the browser and search engines. This is where
However, over time, HTML has evolved and
you would typically include the document's title,
become the foundation for formatting and
links to CSS stylesheets, scripts, and other meta
presenting content on the World Wide Web. It is
information.
now widely used by web developers to create and
design websites. HTML offers a variety of tags Title: The <title> element is used to define the
and attributes that enable the creation of complex title of an HTML document. It is placed within
layouts, multimedia integration, forms, and the <head> element. The text placed between
interactive elements. the opening <title> and closing </title> tags
appears as the title of the web page in the
Web browsers, such as Chrome, Firefox, and
browser's title bar or tab. Additionally, it is also
Safari, interpret the HTML code and render it as
used by search engines when displaying search
a visual representation of a web page. This
results.
allows users to view and interact with the
content presented on the web.
Body: The <body> element represents the main <h6> the lowest level. The purpose of heading
content area of an HTML document. It is placed tags is to structure the content and provide visual
immediately after the closing </head> tag. All hierarchy to the headings on a webpage.
the visible content of the web page, such as text,
Here are the heading tags along with their usage:
images, links, and other elements, is placed
within the <body> element. The opening 1. <h1>: Represents the highest level of
<body> tag is followed by the content, and the heading. It is typically used for the main
closing </body> tag is placed at the end of the title of a page or section. Example:
content. <h1>Welcome to My Website</h1>
h1: The <h1> element represents the highest 2. <h2>: Represents a level below <h1>
level of heading in HTML. It is used to define the and is used for subheadings. Example:
main heading or title of a section or webpage. <h2>About Me</h2>
The <h1> tag is typically used once per page or
section and is often followed by subheadings of
3. <h3>: Represents a lower level heading,
often used for subheadings under <h2>.
lower hierarchy such as <h2>, <h3>, and so
Example: <h3>Education</h3>
on. The actual text of the heading is placed
between the opening <h1> and closing </h1> 4. <h4>: Represents a lower level heading,
tags. typically used for subheadings under
p: The <p> element is used to define a <h3>. Example: <h4>Work
paragraph in HTML. It represents a block of text Experience</h4>
that is typically separated from other paragraphs. 5. <h5>: Represents a lower level heading,
The <p> tag is used to structure and format text used for subheadings under <h4>.
content, and it automatically adds vertical Example: <h5>Skills</h5>
spacing before and after the paragraph. Text
content placed between the opening <p> and 6. <h6>: Represents the lowest level of
closing </p> tags is treated as a paragraph. heading, used for subheadings under
<h5>. Example: <h6>Contact
In summary, these terms are fundamental
Information</h6>
elements in HTML:
When using these heading tags, it is good
• <!DOCTYPE> declares the HTML version.
practice to follow a hierarchical structure, starting
• <html> is the root element of the HTML
with <h1> as the main heading and descending
document.
down to lower-level headings as needed. This
• <head> contains metadata and non-
helps organize the content and makes it easier for
visible elements.
both humans and search engines to understand
• <title> specifies the title of the
the structure and importance of different sections
document.
on the page.
• <body> represents the visible content
area. THE PARAGRAPH TAG
• <h1> defines the main heading.
The <p> tag in HTML is used to define a
• <p> represents a paragraph of text.
paragraph of text. It represents a block of text
that is typically separated from other paragraphs.
HTML HEADING TAG The <p> tag helps structure and format the
textual content on a webpage, allowing for better
In HTML, heading tags are used to define
readability and visual organization.
different levels of headings or titles within a
document. These tags range from <h1> to Here's an example of using the <p> tag in
<h6>, with <h1> being the highest level and HTML:
Here's an example of using the line breaking tag
in HTML:
In this example, we have various sections on the In this example, we have used the <br> tag to
webpage, and each section is introduced by a insert line breaks within paragraphs. Each time
heading tag. Following each heading, we use the the <br> tag is used, it creates a new line,
<p> tag to define paragraphs that provide more effectively breaking the content onto a new line.
details or information about the respective
For instance, in the first paragraph under the
section.
"Welcome to My Website" section, we have used
For instance, under the "About Me" section, we the <br> tag to separate the text into two lines.
have a paragraph that describes the web Similarly, in the "About Me" and "Contact
developer's passion and expertise. Similarly, the Information" sections, line breaks are introduced
other sections like "Education," "Work to create separate lines of text within each
Experience," "Skills," and "Contact Information" paragraph.
have corresponding paragraphs that provide
Using the <br> tag is particularly useful when
relevant information.
you want to force a line break without starting a
The <p> tag helps separate and structure the new paragraph. It is commonly used for
text content, making it easier to read and addresses, song lyrics, poetry, or other instances
comprehend. It automatically adds vertical where a line break is required without the need
spacing before and after the paragraph, visually for creating a new paragraph.
distinguishing it from other elements on the page.
Remember that the <br> tag doesn't require a
By using the <p> tag, you can organize and closing tag since it is a self-closing tag. You can
present textual content in a clear and structured use it directly within the content, and it will
manner, enhancing the overall readability and create a line break at that point.
user experience of your webpage.
However, it's generally recommended to use line
THE LINE BREAKING TAG breaks sparingly and maintain proper HTML
structure by using appropriate elements like
The line breaking tag in HTML is used to create
paragraphs or headings for better semantic
a line break, forcing text or elements to start on
markup and maintainability of the code.
a new line. The line breaking tag is represented
by the <br> tag. It is a self-closing tag, meaning
it doesn't require a closing tag.
HORIZONTAL LINES
PRESERVE FORMATTING
<p>This is a paragraph.</p>
In the above example, the <h1> element and the
<p> element are nested within the <div>
In the above example, <p> is the opening tag,
element. The <div> element acts as the parent
and </p> is the closing tag. They enclose the
element, and the heading and paragraph elements
paragraph content and indicate that it should be
are the child elements. The content inside the
displayed as a paragraph.
child elements is displayed within the context of
HTML Element: An HTML element refers to a the parent element.
complete unit of content within an HTML
Nested elements can be nested further to create
document. It consists of an opening tag, the
deeper levels of hierarchy. For instance:
content, and a closing tag. Elements can be as
simple as a single tag or more complex with
nested elements inside.
<h1>This is a heading</h1>
2. Debugging or troubleshooting: Comments It's worth noting that not all HTML elements
can be used to temporarily remove or have attributes, and the available attributes may
disable a portion of the code to identify vary depending on the element. HTML attributes
or isolate an issue. play a crucial role in extending the functionality
and customization of HTML elements within web
3. Leaving reminders or to-do lists:
pages.
Developers can use comments to leave
reminders or notes for themselves or their
team members about pending tasks or ITALIC TEXT
improvements.
In HTML, you can make text appear in italics by
It's important to note that comments should be
using the <em> element or the <i> element.
used judiciously and kept up to date. Excessive or
Both elements are used to indicate emphasis or to
outdated comments can clutter the code and
italicize text.
make it harder to maintain.
Here's an example of using the <em> element:
<p>This is <em>italic</em> text.</p> <p>This is <u>underlined</u> text.</p>
<p>This is a <strong>strong</strong>
3. <meta name="description" content="A
statement.</p> brief description of the webpage">:
Provides a concise summary or description
In the above example, the word "strong" is of the webpage's content. This information
enclosed within the <strong> tags. When is often displayed by search engines in
rendered in a web browser, the word "strong" search results.
will appear in bold, indicating its importance or
4. <meta name="keywords"
emphasis within the sentence.
content="keyword1, keyword2,
The <strong> element provides semantic keyword3">: Specifies the keywords or
meaning to the text, indicating to browsers, phrases that describe the content of the
search engines, and assistive technologies that the webpage. While less important for search
enclosed content is of particular importance or engine rankings nowadays, they can still
relevance. It is typically used to highlight provide context to search engines.
keywords, important phrases, or critical
information within the content.
5. <meta name="author" content="Author
Name">: Indicates the author of the
It's worth noting that the visual representation of webpage.
strong text (e.g., bold) can be modified using CSS
styles. However, the <strong> element should be 6. <meta name="robots" content="index,
used to convey the semantic meaning of strong follow">: Instructs search engine robots
content, while the actual styling can be adjusted on how to crawl and index the webpage.
using CSS. The values can include "index" (to include
the webpage in search results) and
"follow" (to follow and index links on the
HTML META TAGS page).
2. <meta name="viewport"
content="width=device-width, initial-
scale=1.0">: Sets the viewport properties
HTML LISTS Unordered lists (<ul>) in HTML support various
attributes to modify their behavior and
In simpler terms, HTML lists are used to organize appearance. Let's explore some commonly used
and present information in a structured manner. attributes:
Lists allow you to group related items or content
together and provide a clear visual hierarchy. type: This attribute specifies the type of bullet or
There are three types of HTML lists: unordered marker used in the list. The default value is
lists, ordered lists, and definition lists. "disc", which displays a filled circle. Other
common values include "square" (square bullet)
HTML UNORDERED LISTS and "circle" (hollow circle).
You can customize the appearance of the bullets Here's an example of an ordered list in HTML:
using CSS to change their shape, size, color, or
even remove them completely. The unordered list
structure (<ul> and <li>) remains the same, and
the styling can be adjusted to match the design
and style of your webpage.
In the above example, the <ol> element To create a definition list in HTML, you use the
represents the ordered list, and each list item is <dl> element along with the <dt> element for
enclosed within an <li> element. The browser terms and the <dd> element for definitions.
will render this as a numbered list:
Here's an example of a definition list in HTML:
1. First item
2. Second item
3. Third item
In the above example, the src attribute specifies In simpler terms, HTML text links are clickable
the URL or file path of the image file, and the elements on a webpage that allow users to
alt attribute provides alternative text that navigate to other web pages or specific parts of a
describes the image. The alt attribute is important page. These links are commonly known as
for accessibility, as it is displayed by assistive hyperlinks.
technologies and search engines when the image
To create a text link in HTML, you use the <a>
cannot be loaded or is unavailable.
(anchor) tag. The content placed between the
In addition to the src and alt attributes, HTML opening <a> tag and the closing </a> tag
images support various other attributes that can becomes clickable, and when a user clicks on
modify their behavior and appearance. Some that part, they are directed to the linked
commonly used attributes for images include :- document or webpage.
• width and height: These attributes specify Here's a simplified explanation:
the width and height dimensions of the
• Hyperlinks are used to connect different
image in pixels. You can set these
web pages or specific parts of a page.
attributes to control the display size of
• You can create hyperlinks using text or
the image on the webpage.
images on a webpage.
• title: The title attribute provides a tooltip • To create a text link, you use the <a>
or additional information about the image (anchor) tag in HTML.
when a user hovers over it with the • Anything placed between the opening and
cursor. It is displayed as a tooltip text. closing <a> tags becomes clickable.
• class and id: These attributes allow you to • When a user clicks on the clickable part,
assign CSS classes or unique identifiers to they are taken to the linked document or
the image element. This allows you to webpage.
apply custom styles or manipulate the For example, if you want to create a link to a
image using CSS or JavaScript. webpage called "example.html" and display the
• style: The style attribute lets you apply text "Click here" as the clickable part, you can
inline CSS styles to the image element. use the following HTML code :-
You can use it to adjust properties like <a href="example.html">Click here</a>
borders, margins, padding, or positioning.
THE TARGET ATTRIBUTE
These are just a few examples of attributes that
can be used with HTML images. Depending on In HTML, the target attribute is used to specify
your requirements, you may also utilize attributes where a linked document or resource should be
such as srcset for responsive images, loading to opened when a user clicks on a hyperlink. It
control image loading behavior, or usemap to determines the target browsing context or
associate an image with an image map for window in which the linked content will be
clickable regions. displayed.
Remember, when using images on your webpage, The target attribute can take several values :-
it's essential to consider factors such as image
• _blank: Opens the linked document or Step 1: Creating a named anchor: To designate
resource in a new browser tab or the location within the webpage where you want
window. to link to, you first need to create a named
• _self: Opens the linked document or anchor. This is done by using the <a> tag with
resource in the same browsing context or the name attribute.
window (the default behavior if the target
<a name="top"></a>
attribute is not specified).
• _parent: Opens the linked document or In this example, the <a> tag with the name
resource in the parent frame or window, attribute is placed at the top of the desired
if the current page is inside a frameset. section. The value "top" is assigned to the name
• _top: Opens the linked document or attribute, but you can choose any name you
resource in the top-level browsing prefer.
context, discarding any frames if the
Step 2: Creating a hyperlink to the named
current page is inside a frameset.
anchor: After creating the named anchor, you can
• A custom frame or window name: If you
create a hyperlink that will take the user to that
specify a custom name (e.g.,
specific section. To do this, you use the <a> tag
target="myframe"), it opens the linked
again, but with the href attribute containing the
document or resource in a specific named
"#" symbol followed by the name of the anchor.
frame or window.
Here's an example that demonstrates the usage of <a href="#top">Go to the Top</a>
the target attribute:
In this example, the <a> tag with the href
<a href="example.html" target="_blank">Open attribute is used. The value "#top" in the href
in new window</a> attribute specifies that the link should take the
user to the section with the "top" named anchor.
In this example, the linked document
"example.html" will be opened in a new browser When the link is rendered in a web browser, it
tab or window when the user clicks on the link, will display as "Go to the Top." Clicking on this
as the target attribute is set to _blank. link will scroll the page to the section where the
named anchor is placed (in this case, the top of
It's worth noting that the behavior of the target the page).
attribute can be influenced by the user's browser
settings, such as whether they have enabled pop- This technique allows you to create internal page
up blockers or overridden the default behavior. navigation within a webpage, enabling users to
Additionally, it's important to use the target jump directly to specific sections of the content.
attribute responsibly and consider the impact on It is particularly useful for long web pages or
the user experience and accessibility. articles where users may want to navigate easily
to different parts of the page.
When using the target attribute, it is
recommended to provide clear indications to
users that a link will open in a new window or HTML IMAGE LINKS
tab to avoid any confusion or unexpected
behaviors. Here's a breakdown and explanation of the code:
To create a link to a specific section within a <a href="page1.html">: This part creates the
webpage, you can use the name attribute in hyperlink and specifies the destination page or
HTML. This process involves two steps: URL that the user will be directed to when they
click on the image. In this example, "page1.html"
is used as the destination, but you can replace it In this example, the <table> element represents
with the desired URL or page name. the table. Each row is defined using the <tr>
element, and within each row, cells are created
<img src="aa.jpg">: This part inserts the image using the <td> element. The browser will render
within the hyperlink. The src attribute specifies this code as a simple table with two rows and
the image source or file path. In this case, two columns:
"aa.jpg" is used as the image source, but you
Tables can contain more complex structures,
should replace it with the actual source file of
including header cells (<th>) for column
your image.
headings, spanning cells across multiple rows or
When this code is rendered in a web browser, it columns, or nested tables for advanced layouts.
will display In this example, the <table> CSS styles can also be applied to tables to control
element represents the table. Each row is their appearance, such as borders, backgrounds,
defined using the <tr> element, and within and alignment.
each row, cells are created using the <td> HTML tables are versatile and widely used for
element. The browser will render this code as a presenting data in various contexts, such as
simple table with two rows and two columns: an financial reports, schedules, pricing information,
image that is clickable. Clicking on the image and more. They provide an effective way to
will take the user to the specified destination organize and present tabular data in a structured
page (in this case, "page1.html"). and accessible manner
TABLE BACKGROUNDS
Using the bgcolor attribute: This attribute allows
you to set the background color for the entire
table or for individual cells within the table. It
provides a way to add a solid color as the
background.
Example :-
<frame> Tag Attributes:
Additional attributes can be used with the file In simpler terms, the <embed> tag is used to
upload box, such as the accept attribute, which include videos or sound in your webpage, and if
specifies the types of files the server accepts. This the browser supports it, it will display the
attribute can be set to specific file extensions or multimedia content with built-in controls.
MIME types to filter the files that can be Example:
selected.
CLICKABLE BUTTONS
In this example, we use the <embed> tag to CASCADING STYLE SHIT (CSS)
embed a video file named "video.mp4" with the
specified media type of "video/mp4". The width CSS (Cascading Style Sheets) is a language used
and height attributes define the dimensions of the to describe how HTML elements should look and
video player on the webpage. be displayed on a web page. It allows you to
control the appearance of various elements such
The browser will display the video and provide
as text, images, and layout.
default controls for playing, pausing, and
adjusting the playback. Instead of directly applying styles to HTML
elements, CSS provides a way to separate the
You can use different media types like Flash
presentation (how things look) from the content
movies (.swf), AVI files (.avi), MOV files (.mov),
and structure of a web page (what things are).
or MPEG files (.mpeg) within the <embed> tag
This separation makes it easier to maintain and
depending on the browser's support and the
modify websites, especially when dealing with
multimedia formats you want to embed.
large-scale projects.
Background Audio with <bgsound> Tag :-
By using CSS, you can create a set of rules or
The <bgsound> tag is used to play a soundtrack styles that define the visual aspects of your
in the background of your webpage. However, it website, such as the choice of fonts, colors,
is supported only by Internet Explorer, and most spacing, and positioning of elements. These styles
other browsers ignore this tag. can be saved in external CSS files, which can be
applied to multiple web pages at once. This
In simpler terms, the <bgsound> tag allows you
means that if you want to change the appearance
to play an audio file as background music on
of your entire website, you can simply replace
your webpage, primarily in Internet Explorer.
the style sheet, and all the pages will adopt the
Example: new styles, making it easier to update the design
or layout of your site.
Example:
Element selectors in CSS are used to target Class selectors in CSS are used to target HTML
specific HTML elements based on their tag name. elements based on their assigned class attribute.
They apply styles to all instances of that element The class attribute allows you to assign a specific
throughout the document. Element selectors are name to one or multiple elements, and class
selectors are denoted by a dot (.) followed by the Class selectors offer a powerful way to apply
class name. consistent styles to multiple elements across your
web page by grouping them based on common
Class selectors provide a way to group and style
attributes. They provide flexibility and re-
elements that share the same class. This is
usability, allowing you to create more
especially useful when you want to apply the
maintainable and organized CSS code.
same styles to multiple elements without affecting
other elements on the page.
3) ID SELECTORS
In the above example, the .highlight class selector
In CSS, an ID selector is used to target a specific
targets all elements that have the highlight class.
It sets their background color to yellow. To apply HTML element based on its unique ID attribute.
this style, you need to assign the class to the The ID attribute provides a unique identifier for
desired HTML elements using the class attribute. an element within an HTML document. ID
selectors are denoted by a hash symbol (#)
followed by the ID value.
Example:
GROUP SELECTORS
Here's a breakdown of different types of group Group selectors offer a convenient way to apply
selectors: shared styles to multiple elements without
duplicating the CSS code. They can be used with
1. Element Group Selectors: You can group
element selectors, class selectors, ID selectors, or
multiple element selectors to apply the
any combination thereof. By grouping selectors,
same styles to multiple elements.
you can make your CSS code more concise and Example :-
maintainable.
3. Interactivity and Event Handling: This is useful for tasks like initializing the state
JavaScript facilitates user interactions on of a web application, making AJAX requests for
web pages. It can respond to events like data, or attaching event handlers to specific
button clicks, form submissions, mouse elements on the page.
movements, and keyboard input. Through
Things that should be done when the page is
event handling, developers can trigger
closed: JavaScript also provides the ability to
specific actions or execute code in
execute code when a web page is about to be
response to these events.
closed or navigated away from. This can be
4. Cross-Browser Compatibility: JavaScript is useful for performing cleanup tasks or saving user
supported by all major web browsers, data before leaving the page. This event handler
including Chrome, Firefox, Safari, and allows you to prompt the user with a
Edge. This allows developers to write confirmation dialog before they leave the page :-
code that works consistently across
Here, you can include any necessary code to
different browsers and platforms.
handle data saving, clean up resources, or display
5. Integration with HTML and CSS: a confirmation message to the user.
JavaScript can be seamlessly integrated
Action that should be performed when a user
with HTML and CSS, the other core
clicks a button: JavaScript allows you to define
technologies of web development. It
event handlers that respond to user actions, such
enables dynamic modification of HTML
as clicking a button. By attaching an event
elements, manipulation of CSS styles, and
handler function to the button's onclick event,
the creation of interactive user interfaces.
you can specify the code that should run when
6. Extensibility and Libraries: JavaScript has the button is clicked. For example :-
a vast ecosystem of libraries and Here, you can define the desired action or
frameworks that extend its capabilities behavior that should occur when the button is
and simplify common tasks. Popular clicked, such as displaying a message, validating
libraries include jQuery, React.js, input, making AJAX requests, or updating the
AngularJS, and Vue.js, among others. page content.
JavaScript's versatility and widespread adoption Content that should be verified when a user
make it a crucial language for web development. inputs data: JavaScript can also be used to verify
It enables developers to create interactive user and validate user input in real-time. By attaching
experiences, validate form inputs, perform data event handlers to input fields, you can respond to
manipulation, communicate with servers, and user interactions, validate the entered data, and
build complex web applications. provide feedback or error messages. For example,
you can use the on input event to check the
WHAT CAN JAVASCRIPT DO ? validity of an email input field as the user
types :-
Things that should be done every time a page
loads: When a web page loads, JavaScript can be Here, you can write code to validate the entered
used to perform various tasks automatically. This email value against a specific pattern or perform
can include initializing variables, setting up event other checks. You can then update the UI to
listeners, fetching data from a server, indicate whether the input is valid or display
manipulating the page structure or content, and error messages if necessary.
much more. For example, you can use the
By utilizing event handlers in JavaScript, you can a breakdown of the different ways you can
control and respond to various user interactions include JavaScript code in your HTML:
and browser actions, ensuring a dynamic and
Inline JavaScript: You can include JavaScript code
interactive user experience on your web pages or directly within the <script> tags in your HTML
application document. This can be done within the <head>
or <body> section of the HTML.
JavaScript Program Structure: A JavaScript
program consists of variables, objects, and
functions. It is a high-level programming
language that follows an object-oriented
programming (OOP) approach.
Terminating Statements and Blocks: In JavaScript, External JavaScript: JavaScript code can be
each statement is terminated by a semicolon (;). placed in external files with a .js extension. This
Blocks of code are enclosed within curly brackets allows you to separate your JavaScript code from
{} to define their scope and separate them from the HTML, making it easier to manage and reuse.
other code blocks. Example: myScript.js
Functions and Parameters: Functions in JavaScript
are defined using the function keyword, followed
by the function name and a pair of parentheses
(). Parameters (if any) are declared inside the
parentheses. Functions are a way to encapsulate
reusable code blocks. To use an external JavaScript file, include the
<script> tag with the src attribute pointing to
Variables: Variables in JavaScript are declared the file.
using the var keyword. They can hold various
types of values, including numbers, strings, <script src="myScript.js"></script>
booleans, objects, and more. Variables are used
to store and manipulate data within a program. You can place the external script reference in the
<head> or <body> section as per your
Script Placement: JavaScript code can be
preference.
implemented within the <script>...</script>
HTML tags in a web page. The <script> tags are External References: External JavaScript files can
used to alert the browser to interpret the be referenced using a full URL or a relative path
enclosed text as JavaScript code. to the current web page.
Example:
Comments are commonly used to: • let was introduced in ES6 (ECMAScript
2015) and provides block scope. It is
• Explain the purpose or functionality of a
limited to the block it is declared in, such
particular code segment.
as within a function or loop.
• Provide documentation and improve code
• const also came with ES6 and is used to
readability for other developers.
declare constants. Once assigned a value,
• Disable specific lines or blocks of code
the value cannot be changed.
temporarily during testing or debugging.
Examples of variable declarations and verifying if a date is entered correctly, or
assignments: ensuring that numeric fields only contain numeric
values.
• Check if the password field is at least 8 This example covers basic form validation tasks
characters long. If the password is less such as checking for required fields, validating
than 8 characters, display an alert email format, and ensuring a minimum password
message saying "Password must be at length.
least 8 characters long" and return false
to prevent form submission.
PHP
Variables in PHP
Return Value:
In PHP, data types specify the type of data that a $cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
variable can hold. PHP supports several data
types, including strings, integers, floats, booleans,
arrays, objects, NULL, and resources. NULL: NULL is a special data type that represents
Understanding these data types is essential for the absence of a value. It is often used to
working with data and performing various indicate that a variable has no value assigned to
operations in PHP. it. Here's an example:
STRING IN PHP
Integer: An integer is a non-decimal number GET THE LENGTH OF A STRING
without a decimal point. It can be either positive
or negative. PHP integers can be specified in In PHP, a string is a sequence of characters, such
decimal, hexadecimal, or octal formats. Here's an as "Hello world!". Strings are commonly used to
example: store and manipulate text data in PHP.
In PHP, you can count the number of words in a SEARCH FOR A SPECIFIC TEXT WITH IN A
string using the str_word_count() function. This STRING
function takes a string as input and returns the
count of words present in that string. In PHP, you can search for a specific text within
a string using the strpos() function. This function
For example, let's consider the string "Hello
takes two parameters: the string to search within
world!". We can use the str_word_count()
and the text you want to find.
function to count the number of words in it:
For example, let's consider the string "Hello
<?php
world!". We can use the strpos() function to
echo str_word_count("Hello world!"); // outputs
2 ?> search for the text "world" within this string:
<?php
In this example, the str_word_count("Hello echo strpos("Hello world!", "world"); // outputs 6
world!") statement returns the value 2 because ?>
the string "Hello world!" contains two words,
"Hello" and "world". The echo statement then In this example, the strpos("Hello world!",
displays the result. "world") statement searches for the text "world"
within the string "Hello world!". Since "world" is
The str_word_count() function is useful when you
found starting at the 6th character of the string,
need to count the number of words in a string,
the function returns the character position 6. The
which can be helpful in tasks such as text
echo statement then displays this result.
analysis, word processing, or word frequency
calculations. If the text you are searching for is not found
within the string, the strpos() function will return
REVERSE A STRING
FALSE. This can be useful for checking if a
specific text exists within a string.
In PHP, you can reverse a string using the
strrev() function. This function takes a string as The strpos() function is commonly used when you
input and returns the reversed version of that need to locate the position of a specific substring
string. within a larger string and perform certain
operations based on its position.
For example, let's consider the string "Hello
world!". We can use the strrev() function to
reverse it: REPLACE TEXT WITH IN A STRING
include 'variables.php';
In the example above, the header.php, In this example, the $name variable from the
navigation.php, and footer.php files are included variables.php file is accessed and concatenated
within the HTML structure. This allows you to with the string "Hello, " to produce the output.
separate the common elements of your website 4. Modifying Variables: You can also modify
into separate files for easier maintenance and the included variables within the
reusability. including file. Any changes made to the
Remember to use the appropriate include variables will affect their values in the
statement (include or require) based on the included file as well. For example:
importance of the included file to your include 'variables.php';
application's execution flow.
$age += 5; // Modifying the $age variable from
the included file
USING VARIABLES FROM THE INCLUDED FILE
In this example, the $age variable from the
When you include a file in PHP, any variables
variables.php file is incremented by 5 within the
defined within that file become accessible in the
including file.
file that includes it. This allows you to use and
manipulate those variables as if they were It's important to note that including files should
defined directly in the including file. be done with caution to avoid naming conflicts
or unexpected behavior. Ensure that the included
Here's how it works in detail:
file does not redefine variables that are already
1. Including the File: To include a file and in use in the including file to avoid conflicts.
make its variables accessible, you can use
Example: Assume you have an variables.php file
the include or require statement followed
with the following contents :-
by the file path. For example:
include 'variables.php';
SELECT STATEMENT
UPDATE STATEMENT
POST Method:
• Inside the <form> element, add input Now, let's see an example of a simple HTML
fields such as text fields, checkboxes, form and its corresponding PHP form processing
radio buttons, dropdown menus, etc., code:
using appropriate HTML input elements
HTML Form (index.html):
like <input>, <textarea>, <select>, etc.
• Assign a unique name attribute to each
input field. This name will be used to
identify the form field when processing
the form data in PHP.
Submit Button:
The form has an action attribute set to Make sure to save this PHP code in a file named
update.php, which is the PHP script that will update.php and ensure that you have the correct
handle the form submission and perform the database credentials and table structure in place.
update operation.