BIT301 ยท 2081 ยท 10 marks
What is server side scripting? How can you define and declare variables in PHP? Write PHP programs to demonstrate concepts of while and do while loops.
What is server side scripting? How can you define and declare variables in PHP? Write PHP programs to demonstrate concepts of while and do while loops.[10]
Quick answer
Server side scripting is a technique used in web development where scripts are executed on the web server rather than on the client's browser. The server processes the script, generates output (usually HTML), and sends only the final result to the client. The user cannot view the original source code.
Server Side Scripting, PHP Variables, and Loops
Part A: Server Side Scripting
How it works:
- The client sends a request to the server (e.g., requesting a
.phpfile) - The web server identifies the script and passes it to the appropriate interpreter (e.g., PHP interpreter)
- The interpreter executes the code, interacts with databases if needed, and produces HTML output
- The server sends this HTML response back to the client's browser
Examples of server side scripting languages: PHP, Python (Django/Flask), ASP.NET, Node.js, JSP
Advantages:
- Dynamic web page generation
- Database interaction is possible
- Source code is hidden from the client
- Supports user authentication, session management, etc.
Part B: Defining and Declaring Variables in PHP
PHP is a loosely typed language, meaning there is no need to explicitly declare the data type of a variable. Variables are declared using the dollar sign ($) followed by the variable name.
Rules for declaring variables:
- Must start with
$followed by the variable name - Variable name must begin with a letter or underscore (not a digit)
- Variable names are case-sensitive (
$Ageand$ageare different) - No data type declaration is required; PHP assigns it automatically
Example:
<?php
$name = "John"; // String variable
$age = 25; // Integer variable
$price = 99.5; // Float variable
$is_active = true; // Boolean variable
echo $name . " ";
echo $age;
?>
Variable Scopes in PHP:
| Scope | Description |
|---|---|
| Local | Declared inside a function; accessible only within that function |
| Global | Declared outside a function; accessible outside (use global keyword inside functions) |
| Static | Retains its value even after the function finishes executing |
Part C: While Loop in PHP
The while loop is an entry-controlled loop. The condition is checked before executing the loop body. If the condition is false initially, the loop body never executes.
Syntax:
while (condition) {
// code to execute
}
Program: Print numbers 1 to 5 using while loop
<?php
$i = 1;
while ($i <= 5) {
echo "Number: " . $i . " ";
$i++;
}
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Part D: Do-While Loop in PHP
The do-while loop is an exit-controlled loop. The loop body is executed at least once, and then the condition is checked. If the condition is true, the loop continues.
Syntax:
do {
// code to execute
} while (condition);
Program: Print numbers 1 to 5 using do-while loop
<?php
$i = 1;
do {
echo "Number: " . $i . " ";
$i++;
} while ($i <= 5);
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Key Difference: While vs Do-While
| Feature | While Loop | Do-While Loop |
|---|---|---|
| Type | Entry-controlled | Exit-controlled |
| Condition check | Before loop body | After loop body |
| Minimum executions | 0 times (if condition is false) | At least 1 time |
| Semicolon after condition | Not required | Required (; after while) |
Conclusion
Server side scripting enables dynamic web content generation on the server. PHP provides simple variable declaration using $ with no type declaration needed. The while loop checks the condition before execution (entry-controlled), while the do-while loop guarantees at least one execution by checking the condition after the loop body (exit-controlled).
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.