WEB TECHNOLOGIES - 1 : CHAPTER 1 : EXAM PREPARATION : TYBCS : SPPU PYQ

 Chapter 1 Introduction to HTML, HTTP and PHP 

1 mark

--Q. Give four examples of web browsers.     

Answer: Examples of web browsers are Google chrome, Mozilla firefox, Microsoft edge, Apple Safari,etc.

Q.Which operator is used to compare data as well as data type?

Answer: Strict equality operator compares both the values and the types of operands. It is represented by three equal signs ('===').


--Q. Write role of web server and web browser?

Answer:

Web Server: It stores, processes and delivers the web content to users over internet. It's primary role is to handle requests from web browsers and send appropriate responses. 

It can also handle dynamic content and manage security,

Web Browser: It allows users to access and interact with web content on the internet. It renders HTML documents, displays multimedia content and provides user interface for navigating the web.

It also manage cookies and sessions which helps to remember user preferences.


Q. Write purpose of resource data type with suitable example.

Answer: Resource is a special PHP data type that refers to external resource (like file, image,etc) which is not part of PHP native language. It deals with outside world by creating and using resources using special functions.

Q. Explain concept of variable variables with an example.


Q. State the features of PHP.

Answer: Following are the features of PHP:

1. Open Source: It is open source scripting language and has large community of developers contributing to its development.

2. Cross-Platform Compability: It can run on various operating systems and is compatible with wide range of web servers.

3. Large library of functions: It offers extensive standard library with built-in functions.

4. Database Integration: It provides support for various databases including MySQL, PostgreSQL, and more. 

5. Server Side Scripting: It is primarily used on server side of web applications which allows dynamic content generation.


Q. State the advantages of PHP.

 Answer: 

Advantages of PHP are: 

1. Open Source: It is free to use and widely accesible.

2. Cross platform compatibility:  It can run on various operating systems and is compatible with wide range of web servers.

3. Rapid Development: Its simplicity and ease of use contributes to rapid application development.

4: Scalability: It can be scaled to accomodate increased traffic and demand.

5. Security: It offers security features like built in data filtering and support for secure protocols (HTTPS) helping developers build secure applications.


5 marks:

Q. Discuss the scope of a variable in PHP with an example.

Answer: Scope of variable is the part of program in which it is accessible.

There are four types of variable scopes in PHP:

1. Local Scope: A variable which is declared inside the function is called as local variable. It has access life only within that variable.

Example:

function myFunction() {

    $v1 = "I am a local variable.";

    echo $v1;

}

myFunction(); // Outputs: I am a local variable.

// echo $v1; // This will result in an error, as $v1 is not accessible here.


2. Global Scope:A variable with global scope is one that is defined outside of any function. It's accessible from anywhere in the script, both inside and outside functions.


Example: $g1 = "I am a global variable.";

function myFunction() {

    global $g1; // Use the 'global' keyword to access the global variable

    echo $g1;

}

myFunction(); // Outputs: I am a global variable.

echo $g1; // Outputs: I am a global variable.


3. Static Variable:A static variable is one that retains its value between function calls. It's declared within a function and is accessible only within that function.


Example: 

function counter() {

    static $count = 0; // Static variable retains its value between calls

    $count++;

    echo "Count: $count<br>";

}

counter(); // Outputs: Count: 1

counter(); // Outputs: Count: 2

counter(); // Outputs: Count: 3

4. Function Parameter: Function parameters act as variables within the function, allowing you to pass values to the function when it's called.


function greet($name) {

    echo "Hello, $name!";

}


greet("Alice"); // Outputs: Hello, Alice!


Q. What is the difference between break and continue statement with an example? 


Answer: Break and Continue statements are used within loops to control the flow of execution but they serve different purposes.

1. Break statement is used to immediately terminate the execution of the innermost loop where it is encountered and exit the loop entirely.

2. The continue statement is used to skip the remaining code within the current iteration of the loop and move to the next iteration.

Example:

When we use break statement 


for($i=1; $i<= 10; $i++)

{

if($i==6)

{

break;

}

echo $i." ";

}

OUTPUT: 1 2 3 4 5


whereas when we use continue statement 

for ($i = 1; $i <= 5; $i++) {

    if ($i == 3) {

        continue; // Skip the iteration when $i equals 3

    }

    echo $i . " ";

}


OUTPUT: 1 2 4 5 


Q. What is data type? List different data types in PHP. Explain any two.

Answer: A datatype defines the type of data a variable can store. It is a set of values and has some allowed operations on those values.

PHP supports 8 primitive data types:

1. Four scalar data types: integer, float, string, and Boolean.

2. Two compound data types: array and object

3. Two special data types: resource and NULL.


Explaination:


1. Integer: It is a datatype used to specify a numeric value without a fractional component.

The integer size can be determined using PHP_INT_SIZE

is_int() or is_integer() is used to test whether the value is integer.


2. String: It is a sequence of characters where a character is same as a byte.

String literals are determined by single '..' or double quotes "...".


For example: 'Hello PHP' and "Hello PHP " is same.

But variables are expanded within double quotes:

for example: $a="Good"

echo "$a, morning \n";

echo '$a, morning' ;

Output:

 Good, Morning

 $a, morning


Q. Explain the execution of PHP script using a suitable diagram.

Answer: 

The PHP script execution happens in following steps:

1. Client accesses the webpage by sending a request to a PHP script.

2. The web server recves the request and it routes to request to PHP interpreter if the file contains PHP scripts.

3.  PHP interpreter processes PHP scripts and produces the output as HTML content which is sent back to the client's browser .

4. If request does not contain PHP script then HTML output is returned to the browser. 

5. Client browser receives the HTML response and final webpage is displayed to the user.


Q. Explain the following statements with syntax and example:


(i) While statement (ii) Switch Statement 


Answer:

(i) While Statement: The while statement is used in PHP for creating a loop that repeatedly executes a block of code as long as a given condition is true. 


Syntax for the while statement:


while (condition) {

    // Code to be executed 

}

For example:


$counter = 1;


while ($counter <= 5) {

    echo "Count: $counter<br>";

    $counter++;

}


(ii) Switch Statement: It is used to compare the given value with multiple case values and matching case are executed up to the first break keyword.

Syntax for switch statement:


switch(variable)

{

case value1;

break;

case value 2;

break;

default:


}


For example: 


$dayOfWeek = "Wednesday";


switch ($dayOfWeek) {

    case "Monday":

        echo "It's the start of the week.";

        break;

    case "Tuesday":

    case "Wednesday":

    case "Thursday":

        echo "It's a workday.";

        break;

    case "Friday":

        echo "It's almost the weekend!";

        break;

    case "Saturday":

    case "Sunday":

        echo "It's the weekend. Time to relax!";

        break;

    default:

        echo "Invalid day entered.";

}