Unit I - Introduction to PHP

 

1. Introduction to PHP

1.1 Introduction to Static and Dynamic Websites

Static Website

  • Content is fixed and does not change automatically.

  • Created using HTML and CSS.

  • Same content is shown to all users.

Example:
A college notice page written only in HTML.

<h1>Welcome to Our College</h1> <p>Admission Open</p>

Dynamic Website

  • Content changes based on user interaction or database.

  • Uses server-side languages like PHP.

  • Can display different data to different users.

Example:
Login system, online result system, feedback form.


1.2 Introduction to PHP and Its History

  • PHP stands for Hypertext Preprocessor.

  • It is a server-side scripting language.

  • PHP code runs on the server and sends output to the browser as HTML.

History of PHP

  • Created by Rasmus Lerdorf in 1994.

  • Initially called Personal Home Page.

  • Later developed into a powerful web development language.

  • Widely used with MySQL for database-driven websites.


1.3 Basic PHP Syntax and File Structure

  • PHP files use the extension .php

  • PHP code is written inside <?php ?> tags.

  • Statements end with a semicolon (;)

Basic PHP Program

<?php echo "Hello World"; ?>

File Structure

  • PHP code can be written inside HTML.

<!DOCTYPE html> <html> <body> <?php echo "Welcome to PHP"; ?> </body> </html>

1.4 Output Statements: echo and print

echo

  • Used to display output.

  • Faster than print.

  • Can display multiple values.

<?php echo "Hello Students"; echo "<br>"; echo "Welcome to PHP"; ?>

print

  • Used to display output.

  • Returns value 1.

<?php print "Learning PHP"; ?>

1.5 PHP Variables and Value Types

Variables

  • Used to store data.

  • Start with $ symbol.

  • No need to declare data type.

<?php $name = "Jaydeep"; $age = 20; ?>

Value Types (Data Types)

  • String"Hello"

  • Integer25

  • Float10.5

  • Booleantrue / false

  • Array

  • NULL

<?php $price = 99.99; // Float $isActive = true; // Boolean ?>

1.6 PHP Constants and Magic Constants

Constants

  • Value cannot be changed.

  • Defined using define().

<?php define("COLLEGE", "GTU"); echo COLLEGE; ?>

Magic Constants

  • Predefined constants.

  • Start and end with __.

Magic ConstantDescription
__LINE__Current line number
__FILE__File path
__DIR__Directory path
__FUNCTION__Function name
<?php echo __FILE__; ?>

1.7 PHP Operators and Their Precedence

1. Arithmetic Operators

OperatorUse
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
<?php $a = 10; $b = 3; echo $a + $b; ?>

2. Increment and Decrement Operators

<?php $x = 5; echo ++$x; // Pre-increment echo $x--; // Post-decrement ?>

3. Assignment Operators

<?php $a = 10; $a += 5; // $a = $a + 5 echo $a; ?>

4. Logical Operators

OperatorMeaning
&&AND
`
!NOT
<?php $a = true; $b = false; echo $a && $b; ?>

5. Bitwise Operators

  • Work on binary values.

<?php $a = 5; // 0101 $b = 3; // 0011 echo $a & $b; ?>

1.8 Decision-Making Statements

if Statement

<?php $marks = 70; if($marks >= 35){ echo "Pass"; } ?>

if-else Statement

<?php if($marks >= 35){ echo "Pass"; } else { echo "Fail"; } ?>

else-if Clause

<?php if($marks >= 75){ echo "Distinction"; } elseif($marks >= 60){ echo "First Class"; } else { echo "Second Class"; } ?>

switch-case Statement

<?php $day = 1; switch($day){ case 1: echo "Monday"; break; case 2: echo "Tuesday"; break; default: echo "Invalid Day"; } ?>

1.9 Loops

while Loop

<?php $i = 1; while($i <= 5){ echo $i."<br>"; $i++; } ?>

for Loop

<?php for($i=1; $i<=5; $i++){ echo $i."<br>"; } ?>

Nested Loop

<?php for($i=1; $i<=3; $i++){ for($j=1; $j<=3; $j++){ echo "$i $j <br>"; } } ?>

break and continue

<?php for($i=1; $i<=5; $i++){ if($i == 3) break; echo $i; } ?>
<?php for($i=1; $i<=5; $i++){ if($i == 3) continue; echo $i; } ?>

Comments

Popular posts from this blog

Practical - 1 IWD

Practical - 10 IWD