0% found this document useful (0 votes)
24 views37 pages

UNIT I (Introduction To PHP)

The document provides an overview of server-side scripting, particularly focusing on PHP as a powerful scripting language for web development. It covers PHP's features, syntax, variable handling, data types, operators, flow control statements, and the inclusion of PHP in web pages. Additionally, it highlights the importance of server-side scripting for dynamic content generation and secure data processing.

Uploaded by

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

UNIT I (Introduction To PHP)

The document provides an overview of server-side scripting, particularly focusing on PHP as a powerful scripting language for web development. It covers PHP's features, syntax, variable handling, data types, operators, flow control statements, and the inclusion of PHP in web pages. Additionally, it highlights the importance of server-side scripting for dynamic content generation and secure data processing.

Uploaded by

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

Web Technology II

(Unit I)
Prepared By:
Ukesh Bhattarai
Server Side Scripting

 Scripting Language is executed step by step in a runtime whereas


programming language compiles the code and executes.
 Scripting Language is used to interpret and communicate with other
programming languages.
 Server-side scripting is a technique used in web development where
scripts run on the server rather than on the user's browser.
 It enables dynamic content generation, database interactions, and
enhanced security.
 Unlike client-side scripts (e.g., JavaScript), which run on the user's
browser, server-side scripts execute on the server, ensuring better
control over data processing and security.
 Server-side scripting is used in user authentication, database
management, content management systems (CMS), and online
transactions.
 Examples: PHP, Python (Django, Flask), Node.js, Ruby on Rails, Java
(Spring), ASP.NET.
Introduction to PHP

 PHP was created by Rasmus Lerdorf in 1994 and has since evolved into
a powerful scripting language for web development.
 Hypertext Preprocessor(Open Source Scripting Language).
 PHP is used for developing dynamic websites, handling forms, managing
databases, and creating CMS applications.
 Open-source, cross-platform, easy to learn, database connectivity, rich
library support, and high scalability.
 Popular Applications Built Using PHP: WordPress, Facebook (early
versions), and Laravel-based applications.
 PHP runs on various platforms(Mac OS, Linux, UNIX, Windows)
 PHP is compatible with all servers used until today. For example: XAMPP,
APACHE, NGINX etc.
 Supports wide ranges of databases.
 It is free and can access from www.php.net
Server side scripting using PHP
Features of PHP

 Simple
 Interpreted
 Faster
 Open Source
 Platform Independent
 Case Sensitive
 Real Time access(Chats, Live Games)
Language Basics

 Lexical Structures:
Syntax Rules and Code Structure: PHP scripts start with <?php and end
with ?>. It is case-sensitive and follows C-like syntax.
PHP supports single-line (// and #) and multi-line (/**/)comments.
PHP can be embedded within HTML files using standard PHP tags.
Hello World!!

<html>
<head>
<title>PHP test</title>
</head>
<body>

<?php
echo ‘<p>Hello World!!</p>’;
?>

</body>
</html>
Variables

 Variables are container for storing data values.


 Variables are used to store data that can be referenced and manipulated
throughout a script.
 Declaring variables:
Syntax: A variable in PHP is declared by prefixing its name with
the dollar sign($). For example:

$variablename = value;
 Naming Rules:
 Variable name must start with a letter (a-z or A-Z) or an underscore(_)
followed by any number of letters, numbers, or underscores.
 Variable names are case sensitive.
 Avoid starting variable names with numbers or special characters.
 Variable variables:
 PHP allows the use of variable variables, where the name of a
variable is dynamic and determined by the value of another variable.
 This feature can be useful in certain scenarios but should be used
cautiously to maintain code readability.
 For example:
<?php
$varName = 'dynamicVar';
$$varName = 'I am dynamic!';
echo $dynamicVar;
?>
Scope of Variables:
 The scope of a variable determines where it can be accessed within a script. PHP
has three main scopes:
 Local Scope: Variables declared within a function are local to that function and cannot
be accessed outside of it.
 Global Scope: Variables declared outside of any function have a global scope and can
be accessed anywhere in the script, except inside functions unless explicitly specified.
To access a global variable within a function, use the global keyword or the $GLOBALS array
<?php
$globalVar = "I'm global";
function testFunction() {
global $globalVar;
echo $globalVar
}
testFunction();
?>
 Static Variables
 Static variables maintain their value between function calls. For example:

<?php
function counter()
{
static $count = 0;
$count++;
echo $count;
}
counter();
counter();
counter();

?>
Predefined variables

 PHP provides several predefined variables, such as $_GET, $_POST,


$_SESSION and $_COOKIES which are used to access data from various
sources like form submissions, session data, and cookies.
Data Types

 PHP supports various data types, which can be broadly categorized into
scalar, compound, and special types.
 These data types define the kind of values a variable can hold.
 Scalar Types:
1. Integer
2. Float
3. String
4. Boolean
 Compound types:
 Compound types can hold multiple values.
1. Array
 Stores multiple values in one variable.
 Types of arrays:
 Indexed array (uses numeric keys)
 Associative array (uses named keys)
 Multidimensional array (array inside another array)

2. Object

• Special Types:
• NULL
• Represents a variable with no value.
• Resource
• Used for external resources like database connections, file handles, sockets, etc.
Type Conversion and Type Juggling

 PHP automatically converts types when needed (Type Juggling), but we


can also explicitly convert types.
 Automatic type juggling
$sum = "5" + 10;
echo $sum;

 Explicit Type Casting

$var = "100";
$intVar = (int)$var;
var_dump($intVar);
Operators in PHP
 Operators are symbols that tell the PHP processor to perform specific actions,
such as arithmetic, comparisons, or logical operations.
 Some regular operators:
1. Arithemetic Operators:
These operators perform basic arithmetic operations:
•Addition (+): Adds two operands.
•Example: $sum = $a + $b;

•Subtraction (-): Subtracts the right operand from the left.


•Example: $difference = $a - $b;

•Multiplication (*): Multiplies two operands.


•Example: $product = $a * $b;

•Division (/): Divides the left operand by the right.


•Example: $quotient = $a / $b;

•Modulus (%): Returns the remainder of the division.


•Example: $remainder = $a % $b;

•Exponentiation (**): Raises the left operand to the power of the right.
•Example: $power = $a ** $b;
2. Assignment Operators:

Used to assign values to variables.


•Basic Assignment (=): Assigns the right operand to the left
operand.
•Example: $a = 5;

•Combined Assignment: Performs an operation and assigns the


result.
•Addition Assignment (+=): $a += 5; (equivalent to $a = $a +
5;)

•Subtraction Assignment (-=): $a -= 5;

•Multiplication Assignment (*=): $a *= 5;

•Division Assignment (/=): $a /= 5;

•Modulus Assignment (%=): $a %= 5;


 Comparison/Relational Operators

Compare two values and return a boolean result.

•Equal (==): Returns true if operands are equal.


•Example: $a == $b

•Identical (===): Returns true if operands are equal and of the same type.
•Example: $a === $b

•Not Equal (!= or <>): Returns true if operands are not equal.
•Example: $a != $b

•Not Identical (!==): Returns true if operands are not equal or not of the same type.
•Example: $a !== $b

•Greater Than (>): Returns true if the left operand is greater than the right.
•Example: $a > $b

•Less Than (<): Returns true if the left operand is less than the right.
•Example: $a < $b

•Greater Than or Equal To (>=): Returns true if the left operand is greater than or equal to the right.
•Example: $a >= $b

•Less Than or Equal To (<=): Returns true if the left operand is less than or equal to the right.
•Example: $a <= $b

•Spaceship Operator (<=>): Returns -1 if the left operand is less, 0 if equal, and 1 if greater.
•Example: $a <=> $b
 Logical Operators:

Used to combine conditional statements.

•AND (&& or and): Returns true if both operands are true.


•Example: $a && $b

•OR (|| or or): Returns true if either operand is true.


•Example: $a || $b

•NOT (!): Returns true if the operand is false.

<?php

$a = NULL;

if(!isset($a))
{
echo “True”;
}
else
echo “False”;
?>
Precedence order of Operators

 Parenthesis()
 Exponentiation **
 Multiplication *, Division /,Modulus %
 Addition +, Subtraction -
Flow Control Statements

 In PHP, flow control statements determine the order in which code


blocks are executed, allowing for decision-making and repeated
execution based on specific conditions.
 These statements are essential for creating dynamic and responsive
applications.
Conditional Statements

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


 if ($condition) {

 if...else Statement: Executes one code block if the condition is true


and another if it is false.
 if ($condition) {

}
else {
}
 if...elseif...else Statement: Evaluates multiple conditions in
sequence, executing the corresponding code block for the first true
condition.
 if ($condition1){

}
elseif ($condition2) {
}
else {
}
 Switch Statement
 Selects one of many code blocks to execute based on the value of an
expression.
 If we have menu driven/choice based program, we can use switch case.

switch (expression) {
case value1:
break;
case value2:
break;
default:
}
Looping Statements

 Looping statements repeatedly execute a code block as long as a specified condition


is true.
 Loop means repetition, when a block of code needs to be executed again and again
loop is used.
 while Loop: Executes a code block as long as the condition remains true.
while ($condition) {
}

For example:

$a = 1;

while($a <= 10)

echo “Value is $a”;

$a++;

}
 do...while Loop: Similar to the while loop, but guarantees that the
code block is executed at least once before evaluating the condition.
do {
} while ($condition);

For example:
$a = 1;
do{
echo “value is: “ .$a. “<br>”;
$a++;
}while($a <=10);
 for Loop: Executes a code block a specific number of times, with
initialization, condition, and iteration expressions.
for ($initialization; $condition; $increment) {
}

For example:
for($a=1;a<=10;a++)
{
echo “$a”;
}
 foreach Loop: Specifically designed for iterating over arrays or objects.

foreach ($array as $value) {


}
For example:
$language = array(“PHP”, “Java”, “Python”);
foreach ($language as $value)
{
echo “$value <br>”;
}
Including in PHP

 In PHP, include and require statements are used to incorporate the content of
one PHP file into another.
 This practice promotes code reusability and modularity, allowing you to
maintain common code segments, such as headers, footers, or configuration
settings, in separate files and include them wherever needed.
 include 'filename.php';
 require 'filename.php';

Key Differences Between include and require:

Error Handling:
include: Emits a warning (E_WARNING) if the specified file cannot be found or
included, but the script continues execution.
require: Emits a fatal error (E_COMPILE_ERROR) if the specified file cannot be
found or included, halting the script execution.
 For Example:
Assume you have a file named header.php containing common
header elements:
<!-- header.php -->
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
 You can include this header in your main page as follows:
<!-- index.php -->
<?php
include 'header.php';
?>
<main>
<p>This is the main content of the page.</p>
</main>
</body>
</html>

In this example, the content of header.php is included at the beginning of index.php,


allowing for a consistent header across multiple pages.
Embedding PHP in web pages

 Embedding PHP code within HTML allows you to create dynamic web
pages by integrating server-side logic directly into your HTML structure.
 This approach is fundamental in web development, enabling the
generation of content that can change based on various factors, such as
user input or data from a database.
 For example:
<!DOCTYPE html>
<html>
<head>
<title>My Dynamic Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>
<?php
phpinfo();
?>
</p>
</body>
</html>
Important Considerations:

 File Extension: Ensure your file has a .php extension so that the server
processes the PHP code within it.
 Server Configuration: The server must have PHP installed and properly
configured to execute PHP code.

You might also like