Ch -4 Elementary Server Side Scripting Using PHP

You are currently viewing Ch -4 Elementary Server Side Scripting Using PHP
Ch -4 Elementary Server Side Scripting Using PHP

Ch -4 Elementary Server Side Scripting Using PHP

Q 1) What is the use of Dreamweaver in web designing?

Dreamweaver is a popular web design and development tool created by Adobe. It offers a range of features that make it useful for web designers and developers. Here are some of the key uses of Dreamweaver in web designing:

  1. WYSIWYG Interface: Dreamweaver provides a “What You See Is What You Get” (WYSIWYG) interface, allowing designers to create web pages visually.
  2. Code Editor: For developers who prefer to work directly with code, Dreamweaver includes a powerful code editor with features like syntax highlighting, code completion, and error checking.
  3. Responsive Design: Dreamweaver includes tools for creating responsive designs that adapt to different screen sizes and devices. This is essential for modern web design, given the variety of devices people use to access the internet.
  4. Built-in Templates and Themes: Dreamweaver comes with a variety of templates and themes that can speed up the design process. These can be customized to fit the specific needs of a project.
  5. Live Preview: Designers can see a live preview of their web pages as they are being built, which helps in making real-time adjustments and ensuring the final product looks as intended.

Q 2) What is the use of CSS in web designing?

CSS (Cascading Style Sheets) is a fundamental technology used in web design and development to control the presentation and layout of web pages. Here are the primary uses and benefits of CSS in web designing:

  1. Styling and Layout: CSS allows us to control the appearance of your web pages. We can define styles for elements like fonts, colors, spacing, and layout.
  2. Separation of Content and Design: By using CSS, we can separate the content (HTML) from its presentation (CSS). This makes it easier to maintain and update our website, as we can change the design without altering the HTML structure.
  3. Responsive Design: CSS enables us to create responsive designs that adapt to different screen sizes and devices. This is crucial for providing a good user experience on both desktop and mobile devices.
  4. Animations and Effects: CSS supports animations and transitions, allowing us to add dynamic effects to your web pages. This can enhance user interaction and engagement.

Q 3) PHP is a server side scripting language . Explain?

PHP (Hypertext Preprocessor) is a widely-used server-side scripting language designed primarily for web development but also used as a general-purpose programming language. Here are some key points about PHP:

  1. Server-Side Execution: PHP code is executed on the server, meaning the server processes the PHP code and sends the resulting HTML to the client’s browser. This allows for dynamic content generation based on user interactions or other conditions.
  2. Embedded in HTML: PHP can be embedded directly within HTML code, making it easy to add dynamic elements to static web pages. This integration allows for seamless interaction between the server-side logic and the client-side presentation.
  3. Database Interaction: PHP is commonly used to interact with databases, such as MySQL. It can retrieve, insert, update, and delete data in a database, making it ideal for creating data-driven websites like forums, blogs, and e-commerce sites.
  4. Wide Range of Functions: PHP offers a vast array of built-in functions for tasks like file handling, form processing, session management, and more. This extensive functionality makes it a versatile tool for web developers.
  5. Open Source and Cross-Platform: PHP is open-source, meaning it’s free to use and has a large community of developers contributing to its improvement. It is also cross-platform, running on various operating systems like Windows, Linux, and macOS.

Q 4) Why conditional statements are used in PHP ? explain.

Conditional statements in PHP are used to perform different actions based on different conditions. They are fundamental for controlling the flow of a program. Here are the main reasons why conditional statements are used in PHP:

  • Decision Making
  • Control Flow
  • Error Handling
  • Loops and Iterations
  • Form Processing

Here are the main types of conditional statements in PHP:

if Statement: Executes a block of code if a specified condition is true.

if…else Statement: Executes one block of code if a condition is true, and another block if it is false.

if…elseif…else Statement: Allows for multiple conditions to be checked in sequence.

switch Statement: Selects one of many blocks of code to be executed.

Q 5) PHP is used for designing dynamic websites? Explain?

Dynamic websites contain web pages that are generated in real-time. It is a collection of dynamic web pages whose content changes dynamically. These pages include web scripting code such as PHP. When a dynamic page is accessed, the code within the page is parsed on the web server and the resulting HTML is sent to the client’s web browser. PHP is a server side scripting language. Server side scripting allows the same page to display different content every time it is loaded. The content of a dynamic page is stored in a server’s database and the dynamic behaviour is controlled by the scripting programs running at server side. Hence reloading of the page is required by the browser every time to retrieve the dynamic content.

Q 6) Why session variables are being used ? explain.

Session variables in PHP are used to store information about a user’s interaction with a web application across multiple pages or visits. They provide a way to persist user-specific data for the duration of a user’s session, which typically lasts until the user closes the browser or logs out. Here’s an in-depth explanation of why session variables are used:

  1. Maintaining State: HTTP is a stateless protocol, meaning each request is independent and does not retain information about previous interactions. Session variables allow you to maintain state across multiple requests, enabling a more cohesive user experience.
  2. User Authentication: Session variables are commonly used to manage user authentication. When a user logs in, their information can be stored in session variables, allowing the application to recognize the user on subsequent pages without requiring them to log in again.
  3. Storing User Preferences: You can use session variables to store user preferences, such as language settings, theme choices, or other personalized settings. This helps in providing a customized experience for each user.
  4. Shopping Carts: In e-commerce websites, session variables are used to manage shopping carts. They keep track of the items a user has added to their cart as they navigate through the site, ensuring the cart’s contents are preserved until the user is ready to check out.
  5. Security: Session variables are stored on the server, making them more secure than cookies, which are stored on the client’s browser. This reduces the risk of sensitive information being exposed or tampered with.

Q 7) Why Functions are being used in PHP? Explain.

Functions in PHP, as in other programming languages, are blocks of code designed to perform specific tasks. They are fundamental to writing efficient, maintainable, and reusable code. Functions in PHP are used for several important reasons:

  1. Code Reusability: Functions allow us to write a block of code once and reuse it multiple times throughout your application. This reduces redundancy and makes our code more maintainable.
  2. Modularity: By breaking our code into smaller, manageable functions, we can create a modular structure. This makes it easier to understand, test, and debug your code.
  3. Abstraction: Functions help in abstracting complex operations. we can encapsulate complex logic within a function and call it with a simple function name, making our main code cleaner and more readable.
  4. Parameterization: Functions can accept parameters, allowing us to pass different values to the function and get different results. This makes our functions more flexible and adaptable to various situations.
  5. Scope Management: Functions create their own scope, meaning variables defined inside a function are not accessible outside of it. This helps in avoiding variable name conflicts and keeps your code organized.

Q 8) What is the difference between get and post methods ? Explain?

The Get and Post methods in php are two methods that are commonly used to submit data from a web form to a server-side script for processing. The GET method is used to retrieve data from the server and display it in the browser, while the POST method is used to send data to the server for processing. GET sends data in the URL as a query string, while POST sends data in the request body. GET is useful for retrieving data, while POST is more secure and used for submitting sensitive information like login credentials.

Get MethodPost Method
When using GET, data parameters are included in the URL and visible to everyone.When using POST, data is not displayed in the URL but in the HTTP message body.
GET request is comparatively less secure because the data is exposed in the URL bar.POST request is comparatively more secure because the data is not exposed in the URL bar.
Request made through GET method are stored in Browser history.Request made through POST method is not stored in Browser history.
The GET method supports only string data typesWhile the POST method supports different data types such as string, numeric, binary, and so on.
Data passed through GET method can be easily stolen by attackers.Data passed through POST method can not be easily stolen by attackers.
GET method request can be saved as bookmark in browser.POST method request can not be saved as bookmark in browser.
In GET method we can not send large amount of data rather limited data is sent because the request parameter is appended into the URL.In POST method large amount of data can be sent because the request parameter is appended into the body.

Q 9) What is the difference between while and do while loop in PHP? Explain.

While and do-while, both loops are used to execute a block of code repeatedly, but they differ in how they check the loop condition.

While loopdo-while loop
In a while loop, the condition is checked first, and if it is true, the loop's block of code is executed. If the condition is false initially, the loop is skipped entirely.In a do-while loop, the loop's block of code is executed first, and then the condition is checked. This guarantees that the loop runs at least once, regardless of the initial condition.
It is also known as an entry-controlled loopIt is also known as an exit-controlled loop.
Syntax for while loop-:
while(condition){ //loop body }
Syntax for do-while loop-:
do{ //loop body } while(condition);
The controlling condition appears at the beginning of the loop.The controlling condition is present at the end of the loop.
No semicolon is used as a part of syntax,while(condition).Semicolon is used as part of syntax, while(condition);
It follows a top-down approach.It follows a bottom-up approach.

Q 10) How can write a script in PHP? Explain.

The full form of PHP is a Hypertext preprocessor. It was developed by Rasmus Lerdorf. It is a programming language for developing dynamic web applications and interactive websites. It is a server-side scripting language.

A PHP code can be written in 3 ways

  1. Without any HTML markups
  2. Embedding HTML markups in PHP code
  3. Embedding PHP code in HTML.
  1. Without any HTML markups:
<?php
Echo “hello world! 
?>
  1. Embedding HTML markups in PHP code:
<?php
echo "<html>";
echo "<h1> welcome </h1>" ;
echo "</html>" ;
?>
  1. Embedding PHP code in HTML:
<html>
<body>
<h1>Way to write PHP Script</h1>
<?php
echo "Hello World!";
?>
</body>
</html>

Q 11) What do you mean by a form? How can you design a form? Explain.

A form in web development is an interactive element that allows users to input data and submit it to a web server for processing. Forms are a fundamental part of web applications and are used for various purposes, such as user registration, login, search, data submission, and more. PHP is commonly used to design and handle forms on the server side.

Designing a Form in PHP:

The example below displays a simple HTML form(form.html) with two input fields and a submit button:

<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>

When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named “welcome.php”. The form data is sent with the HTTP POST method.

To display the submitted data we could simply echo all the variables. The “welcome.php” looks like this:

<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

Leave a Reply