1. Introduction to Web Technology & Implementation of PHP Programs
1. Introduction to Web Technology & Implementation of PHP Programs
PHP code is embedded in HTML using the (<?php ... ?> )tags
Comments in PHP (//, #, /* ... */)
PHP case sensitivity for variables and keywords
Ending statements with semicolons (;)
<?php
echo "Hello, World!"; // Outputs Hello,
World!
?>
3. PHP Variables
Creating Variables: Start with $ followed by the variable name.
<?php
$x = 5; // Integer
$y = "John"; // String
echo $x; // Outputs: 5
echo $y; // Outputs: John
?>
Variable Scope:
Local: Accessible only within the function it is declared in.
<?php
function testLocal() {
$localVar = "I am local!";
echo $localVar;
}
testLocal(); // Outputs: I am local!
// echo $localVar; // Error: Undefined variable outside function
?>
Global: Accessible throughout the script by using global
keyword within functions.
<?php
$globalVar = "I am global!";
function testGlobal() {
global $globalVar;
echo $globalVar;
}
testGlobal(); // Outputs: I am global!
echo $globalVar; // Outputs: I am global!
?>
Static: Retains value across multiple function calls.
<?php
function testStatic() {
static $count = 0; // Retains its value on each function call
echo $count;
$count++;
}
testStatic(); // Outputs: 0
testStatic(); // Outputs: 1
testStatic(); // Outputs: 2
?>
6. PHP Strings
Single Quotes: Outputs exactly as written.
<?php
$name = 'John';
echo 'Hello $name'; // Outputs: Hello $name
?>
Double Quotes: Evaluates variables and escape sequences.
<?php
$name = "John";
echo "Hello $name"; // Outputs: Hello John
?>
Constants in PHP
Defining Constants Using define()
o Constants are declared using the define() function.
o Constants don’t use the $ symbol and are generally
written in uppercase by convention.
o Constants cannot be changed or undefined once they are
declared.
o Syntax:
define("PI", 3.14159);
define("SITE_NAME", "My Website");
echo PI; // Outputs: 3.14159
Characteristics of Constants
o Immutable: Once defined, their value cannot be changed.
o Global Scope: Constants have a global scope, so they can
be accessed from any part of the script, including inside
functions.
The const Keyword for Constant Declaration in Classes
o Constants can also be defined within classes using the
const keyword.
o Unlike variables, constants declared in classes don’t
require the $ symbol and are accessible via the
ClassName::CONSTANT_NAME syntax.
o Syntax:
class MathConstants {
const PI = 3.14159;
}
echo MathConstants::PI; // Outputs: 3.14159
Example:
<?php
$name = "Hello, World!";
echo $name; // Outputs: Hello, World!
?>
2. Integer
o Non-decimal numbers, positive or negative.
Example:
<?php
$number = 42;
echo $number; // Outputs: 42
?>
3. Float (Floating-Point Number)
o Numbers with decimal points or in exponential form.
Example:
<?php
$price = 19.99;
echo $price; // Outputs: 19.99
?>
4. Boolean
o Represents two possible values: true or false.
Example:
<?php
$isAvailable = true;
echo $isAvailable; // Outputs: 1 (true)
?>
Examples:
<?php
// Indexed Array
$colors = array("Red", "Green", "Blue");
echo $colors[1]; // Outputs: Green
// Associative Array
$age = array("Alice" => 25, "Bob" => 30);
echo $age["Alice"]; // Outputs: 25
// Multidimensional Array
$contacts = array(
array("Alice", "[email protected]"),
array("Bob", "[email protected]")
);
echo $contacts[0][1]; // Outputs: [email protected]
?>
2. Object
o An instance of a class containing properties and methods.
Example:
<?php
class Car {
public $model;
function __construct($model) {
$this->model = $model;
}
}
$car = new Car("Toyota");
echo $car->model; // Outputs: Toyota
?>
functions.
Example:
<?php
$file = fopen("test.txt", "r");
var_dump($file); // Outputs: resource type with details
fclose($file); // Close the resource after use
?>
o / (Division): $a / $b
o % (Modulus): $a % $b
o ** (Exponentiation): $a ** $b
2. Assignment Operators
Used to assign values to variables, sometimes with arithmetic
operations.
o = (Assignment): $a = $b
3. Comparison Operators
Used to compare values, resulting in Boolean outcomes (true or
false).
o == (Equal): $a == $b
comparison)
4. Logical Operators
Used to combine conditional statements.
o && (And): $a && $b
o || (Or): $a || $b
o ! (Not): !$a
o and, or, xor: These are alternative keywords for logical
operations.
5. String Operators
Used to concatenate strings.
o . (Concatenation): $a . $b (joins two strings)
$a)
6. Increment/Decrement Operators
Used to increase or decrease a variable's value by 1.
o ++$a (Pre-increment): Increments $a by 1, then returns
$a.
o $a++ (Post-increment): Returns $a, then increments by 1.
$a.
o $a-- (Post-decrement): Returns $a, then decrements by 1.
Expressions
Expressions are combinations of variables, operators, and
values that produce a result.
For example, ($a + $b) * $c is an expression combining addition
and multiplication to evaluate to a single value.
<label for="email">Email:</label>
<input type="email" id="email" name="email">
Example:
<form action="submit.php" method="get">
<!-- Form fields -->
</form>
In submit.php, access the data using $_GET:
$name = $_GET['name'];
POST Method:
o Data is sent in the HTTP request body, not visible in the
URL.
o Allows for a larger amount of data.
o More secure for sensitive information.
Example:
<form action="submit.php" method="post">
<!-- Form fields -->
</form>
In submit.php, access the data using $_POST:
$name = $_POST['name'];
Examples of Handling GET and POST Requests in PHP
GET Method Example:
<form action="submit.php" method="get">
<label for="city">City:</label>
<input type="text" id="city" name="city">
<input type="submit" value="Search">
</form>
In submit.php:
$city = $_GET['city'];
echo "Searching for city: " . $city;
POST Method Example:
<form action="submit.php" method="post">
<label for="feedback">Feedback:</label>
<textarea id="feedback" name="feedback"></textarea>
<input type="submit" value="Submit Feedback">
</form>
In submit.php:
$feedback = $_POST['feedback'];
echo "Received feedback: " . $feedback;