0% found this document useful (0 votes)
14 views14 pages

Unit 5

PHP (Hypertext Preprocessor) is an open-source server-side scripting language used for web development, enabling dynamic content generation, database interaction, and session management. It supports various data types, functions, and file handling capabilities, making it versatile for creating data-driven applications. PHP also includes security features and superglobals for managing user input and server data efficiently.

Uploaded by

Devabn Nirmal
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)
14 views14 pages

Unit 5

PHP (Hypertext Preprocessor) is an open-source server-side scripting language used for web development, enabling dynamic content generation, database interaction, and session management. It supports various data types, functions, and file handling capabilities, making it versatile for creating data-driven applications. PHP also includes security features and superglobals for managing user input and server data efficiently.

Uploaded by

Devabn Nirmal
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/ 14

UNIT V

PHP
Introduction to PHP
• PHP (Hypertext Preprocessor) ia a open-source server-
side scripting language designed specifically for web
development.
• It is embedded within HTML
• It is used to manage dynamic content, databases,
session tracking, and even build entire e-commerce
websites.
• Rasmus Lerdorf in 1994, maintained by The PHP Group.
• PHP files typically have a .php extension and can
contain text, HTML, CSS, JavaScript, and PHP code.
Role of PHP

• Dynamic Web Page Generation:


• PHP enables web pages to be generated dynamically based on user inputs or other data sources.
• Example: Displaying user profiles or content retrieved from a database.
• Server-Side Scripting:
• PHP code runs on the server before the result (usually HTML) is sent to the client’s browser.
• This helps in keeping source code hidden from the user and reduces the risk of code tampering.
• Database Interaction:
• PHP can easily connect to various databases such as MySQL, PostgreSQL, and MongoDB.
• Commonly used in creating data-driven applications like blogs, forums, and content management systems.
• Form Handling:
• PHP is used to collect, validate, and process user inputs from web forms.
• It supports GET and POST methods for data transfer.
• Session and Cookie Management:
• PHP allows developers to create and manage sessions and cookies to maintain user state and preferences
across multiple web pages.
• Integration with HTML and CSS:
• PHP code can be embedded within HTML, making it seamless to create templates and content-driven
pages.
• Security Features:
• PHP includes built-in functions to help protect against common web attacks like SQL injection, cross-site
scripting (XSS), and cross-site request forgery (CSRF).
PHP Basics
• Php syntax:
<?php
// PHP code goes here
?>
Variables:
In PHP, a variable starts with the $ sign, followed by the name of the
variable:
Example
$x = 5;
$y = "John";

$x = 5;
$y = 4;
echo $x + $y;
• PHP echo and print Statements are used to output data to the screen. Echo
does not return any value print returns 1, so it can be used in the expressions.
Data Types

• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP is a loosely typed language.
Getting data type:
$x = 5;
var_dump($x);
Strings

• $x = "John"; • $x = "Hello World!";


• echo "Hello $x"; • echo strtoupper($x);
• $x = "John"; • echo strtolower($x);
• echo 'Hello $x'; • echo str_replace("World", "Dolly",
• Double quoted string literals $x);
perform operations for special • echo strrev($x);
characters(it returns the value of • String concatenation:
the variable:) $x = "Hello";
• Single quoted string literals returns $y = "World";
the string as it is: $z = $x . $y;
• echo strlen("Hello world!"); echo $z;
• echo str_word_count("Hello • Slicing
world!"); $x = "Hello World!";
echo substr($x, 6, 5);
• echo strpos("Hello world!",
"world");
• Operators
• If…else
• Switch
• Continue
• Break
• Loops
• While
• Do while
• For
• foreach
Functions

• Php has predefined and user defined functions like other


languages.
• Uses the same syntax like javascript
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}

familyName("Hege", "1975");
familyName("Stale", "1978");
Arrays

• An array in PHP is a data structure that foreach($fruits as $fruit) {


allows you to store multiple values in a echo $fruit . "<br>";
single variable. }
• $colors = array("Red", "Green", • Associative Array:
"Blue"); foreach($age as $name => $value) {
• Types of Arrays echo "$name is $value years old<br>";
• Indexed Array }
• Stores values with numeric keys (starting • Common Array Functions
from 0). Function Description
• $fruits = array("Apple", "Banana",
"Cherry"); count($arr) Returns the number of ele
• echo $fruits[1]; // Output: Banana array_push() Adds elements to the end
• Associative Array array_pop() Removes last element
• Uses named keys (like a dictionary). array_merge() Merges two or more arrays
• $age = array("Alice"=>25, "Bob"=>30); in_array() Checks if a value exists in array
• echo $age["Bob"]; // Output: 30 Array_keys() Returns all the keys of the
• Looping Through Arrays arr
• Indexed Array: Array_sum() find sum of array elements
Working with forms

• Php runs on the server side it receives the data(request) from client
and return it in simple html format.
• HTML forms send data to PHP using:
• GET – Appends data to the URL.
• POST – Sends data in the request body.
• GET vs POST Methods
• Feature GET
POST
• Visibility Appends data to URL Sends data in
request body
• Use Case Bookmarkable/search forms Login, registration,
payments
• Data Limit Limited (~2000 characters) No size limit
• Security Less secure More secure
S u p e rg lo b a ls in P H P

• Superglobals are built-in global arrays in PHP that are always


accessible, regardless of scope — you can use them anywhere in your
script, without needing to declare them as global.
• List of PHP Superglobals
• Superglobal Description
• $_GET Data sent via URL parameters (query string)
• $_POST Data sent via HTML forms using the POST method
• $_REQUEST Combines $_GET, $_POST, and $_COOKIE
• $_SERVER Info about server and execution environment
• $_SESSION Stores data across user sessions
• $_COOKIE Stores small pieces of data on the user's browser
• $_FILES Deals with file uploads via forms
• $_ENV Environment variables
• $_GLOBALS References all global variables in the script
Common PHP Functions for Form Input

• htmlspecialchars() converts special characters to HTML-safe entities.


• empty()
• Checks whether a variable is empty.
• Returns true if value is: "", 0, "0", NULL, false, or an empty array.
Example:
$name = "";
if (empty($name)) {
echo "Name is required";
}
• trim()
Removes whitespace from the beginning and end of a string.
Useful for cleaning user input.
$input = " John Doe ";
$clean = trim($input); // "John Doe"
• stripslashes()
Removes backslashes (\) from a string.
Commonly used to clean up data submitted from forms where magic quotes might be applied
(mostly legacy).
$input = "O\'Reilly";
$clean = stripslashes($input); // "O'Reilly"
PHP File Handling

• PHP allows you to create, read, write, and close files using
built-in functions.
• Common File Functions
• Function Purpose
• fopen() Opens a file
• fread() Reads a file
• fwrite() Writes to a file
• fclose() Closes a file
• file_get_contents() Reads file content as a string
• file_put_contents() Writes data to a file
• Writing to a File
$file = fopen("data.txt", "w"); // 'w' = write (overwrite)
fwrite($file, "Hello, world!\n");
fclose($file);
• If data.txt doesn't exist, it will be created.
• "w" mode overwrites content if the file exists.
• Appending to a File
$file = fopen("data.txt", "a"); // 'a' = append
fwrite($file, "Another line\n");
fclose($file);
• Reading from a File
$file = fopen("data.txt", "r"); // 'r' = read
$content = fread($file, filesize("data.txt"));
fclose($file);
• echo $content;
• Quick Read & Write (Simple Way)
• // Read
• $content = file_get_contents("data.txt");
• // Write (overwrites)
• file_put_contents("data.txt", "New content");
• // Append
• file_put_contents("data.txt", "Extra line", FILE_APPEND);

You might also like