0% found this document useful (0 votes)
0 views

1. Introduction to Web Technology & Implementation of PHP Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

1. Introduction to Web Technology & Implementation of PHP Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction to Web Technology &

implementation of PHP Programs


Evaluation of PHP
PHP (Hypertext Preprocessor) is a server-side scripting language
widely used for web development. It’s integrated with HTML and
supports databases, making it ideal for dynamic web pages.
Advantages of PHP:
 Open-source, free to use.
 Cross-platform compatibility.
 Extensive community and support.
 Compatible with numerous databases, especially MySQL.

Basic Syntax of PHP

 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!
?>

Defining Variables and Constants in PHP


Open Source Software
 Definition: Software where source code is publicly accessible,
modifiable, and distributable under licenses that align with the
Open Source Definition.
PHP itself is an example of open-source software.

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
?>

4. PHP Output Statements


 echo: Faster, accepts multiple arguments, does not return a
value.
<?php
echo "Hello", " World"; // Outputs: Hello World
?>
 print: Slower, accepts one argument, returns 1.
<?php
print("Hello"); // Outputs: Hello
$result = print "Hello"; // Outputs: Hello, returns 1
?>

5. PHP as a Loosely Typed Language


 Definition: PHP automatically converts variable types based on
the assigned value.
<?php
$num = 5; // Integer
$num = "5"; // Now string, automatically converted by PHP
echo $num; // Outputs: 5
?>

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

PHP Data type

Primitive Data Types


1. String
o A sequence of characters, used for text.

o Strings can be defined using single or double quotes.

Example:
<?php
$name = "Hello, World!";
echo $name; // Outputs: Hello, World!
?>
2. Integer
o Non-decimal numbers, positive or negative.

o No decimal points or commas allowed.

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)
?>

Compound Data Types


1. Array
o Used to store multiple values in a single variable.
o Indexed Array: Uses numeric indexes.

o Associative Array: Uses named keys.

o Multidimensional Array: Contains arrays within arrays.

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.

o Objects are created from custom classes.

Example:
<?php
class Car {
public $model;
function __construct($model) {
$this->model = $model;
}
}
$car = new Car("Toyota");
echo $car->model; // Outputs: Toyota
?>

Special Data Types


1. NULL
o Represents a variable with no value. NULL is the only

possible value for this type.


Example:
<?php
$var = null;
var_dump($var); // Outputs: NULL
?>
2. Resource
o A special type that holds a reference to an external

resource, such as a database connection or file handle.


o Resources are usually created using specific PHP

functions.
Example:
<?php
$file = fopen("test.txt", "r");
var_dump($file); // Outputs: resource type with details
fclose($file); // Close the resource after use
?>

Operators and Expressions


Operators
Operators are symbols used to perform operations on variables
and values. PHP supports various operators that can be
grouped into the following categories:
1. Arithmetic Operators
Used for basic mathematical operations.
o + (Addition): $a + $b
o - (Subtraction): $a - $b
o * (Multiplication): $a * $b

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

o += (Add and assign): $a += $b (same as $a = $a + $b)

o -= (Subtract and assign): $a -= $b

o *= (Multiply and assign): $a *= $b

o /= (Divide and assign): $a /= $b

o %= (Modulus and assign): $a %= $b

3. Comparison Operators
Used to compare values, resulting in Boolean outcomes (true or
false).
o == (Equal): $a == $b

o != or <> (Not equal): $a != $b

o === (Identical): $a === $b

o !== (Not identical): $a !== $b

o > (Greater than): $a > $b

o < (Less than): $a < $b

o >= (Greater than or equal): $a >= $b

o <= (Less than or equal): $a <= $b

o <=> (Spaceship): $a <=> $b (returns -1, 0, 1 based on

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)

o .= (Concatenation assignment): $a .= $b (appends $b to

$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.

o --$a (Pre-decrement): Decrements $a by 1, then returns

$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.

Basics of HTML Forms


Forms in HTML are used to collect user input and send it to a
server for processing. PHP is often used as the backend to
handle form data.
1. Form Creation
A basic HTML form contains various input fields wrapped in
<form> tags. The form includes attributes like:
 action: URL where the form data will be sent.
 method: HTTP method to be used (GET or POST).
Example:
<form action="submit.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">

<label for="email">Email:</label>
<input type="email" id="email" name="email">

<input type="submit" value="Submit">


</form>
2. Form Handling
When a form is submitted, the data from input fields is sent to
the server. PHP can handle the submitted data by accessing it
through $_GET or $_POST superglobal arrays, depending on the
method used.
Example: In submit.php:
$name = $_POST['name'];
$email = $_POST['email'];
echo "Name: " . $name;
echo "Email: " . $email;
3. Form Submission (GET and POST Methods)
 GET Method:
o Data is appended to the URL in key-value pairs.

o Limited to a small amount of data.

o Suitable for non-sensitive data and bookmarking.

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;

You might also like