PHP and MySQL Final Year Bca
PHP and MySQL Final Year Bca
AISHWARYA
ASSISTANT PROFESSOR
WHAT IS PHP?
PHP (Hypertext Preprocessor), originally standing for "Personal Home Page," is a widely-used open-source
server-side scripting language primarily designed for web development. It was created by Rasmus Lerdorf in
1994 as a set of Common Gateway Interface (CGI) binaries written in C to manage his personal website. Over
time, PHP evolved significantly:
1. PHP/FI (Forms Interpreter) 2.0 (1997): The first major version of PHP was developed to handle forms and
interact with databases. This version laid the foundation for the language's growth.
2. PHP 3 (1997): Israeli developers Zeev Suraski and Andi Gutman's rewrote PHP's parser in C, resulting in
PHP 3. This version dramatically improved performance and capabilities, making PHP more robust and
efficient.
3. PHP 4 (2000): PHP 4 introduced support for object-oriented programming (OOP), which
allowed developers to write more structured, reusable code. It also included many performance
improvements, shaping PHP into a modern language.
4. PHP 5 (2004): This version further improved OOP support, adding features like interfaces,
abstract classes, and exception handling. PHP 5 became a key version for enterprise-level web
applications.
5. PHP 7 (2015): PHP 7 was a major performance upgrade, making it up to twice as fast as
PHP 5.x. It also introduced new features such as scalar type declarations and return type
declarations, improving code clarity and reliability.
6. PHP 8 (2020): PHP 8 introduced Just-In-Time (JIT) compilation, which improved execution
speed for certain types of code. It also brought new features and enhancements such as named
arguments, union types, and match expressions, strengthening PHP’s capabilities for modern
development.
FEATURES OF PHP
Performance: PHP script is executed much faster than those scripts which are written in
other languages such as JSP and ASP. PHP uses its own memory, so the server workload and
loading time is automatically reduced, which results in faster processing speed and better
performance.
Open Source: PHP source code and software are freely available on the web. You can
develop all the versions of PHP according to your requirement without paying any cost. All
its components are free to download and use.
Familiarity with syntax: PHP has easily understandable syntax. Programmers are
comfortable coding with it.
Embedded: PHP code can be easily embedded within HTML tags and script.
Platform Independent: PHP is available for WINDOWS, MAC, LINUX & UNIX
operating system. A PHP application developed in one OS can be easily executed in other
OS also.
Database Support: PHP supports all the leading databases such as MySQL, SQLite, ODBC,
etc.
Error Reporting - PHP has predefined error reporting constants to generate an error notice or
warning at runtime. E.g., E_ERROR, E_WARNING, E_STRICT, E_PARSE.
Loosely Typed Language: PHP allows us to use a variable without declaring its datatype. It
will be taken automatically at the time of execution based on the type of data it contains on its
value.
Security: PHP is a secure language to develop the website. It consists of multiple layers of
security to prevent threads and malicious attacks.
WHITE SPACE
White Space is a property defined in CSS (Cascading Style Sheets) that enable the user to control the text
wrapping and white spaces inside an element of the website. It can take several types of values depending
upon the user's need.
1. white-space: value;
Several values that can be passed to the white-space property are:
1. white-space: normal;
2. white-space: nowrap;
3. white-space: pre;
4. white-space: pre-wrap;
5. white-space: pre-line;
6. white space: break-spaces;
1.(normal wrapping):
Text will wrap to the next line when it hits the edge of the container.
The text fits inside the box.
2. (nowrap):
The text does not wrap and stays on one line.
If the text is too long, it will overflow outside the box.
Example:
<?php
$text = "This is a long sentence that might not fit in the box.";
?>
3.(pre):
Spaces and line breaks are preserved.
The text does not wrap to the next line, so it overflows if it’s too long for the container.
4.(pre-wrap):
Spaces and line breaks are preserved.
The text wraps to the next line when it reaches the container's edge.
Example:
<?php
// Text with extra spaces and line breaks
$text = "This is an example with:\n\n1. Extra spaces\n2. Line breaks\n3. Long
text that might overflow.";
?>
5.(pre-line):
Collapses multiple spaces into a single space.
Preserves line breaks in the text.
The text wraps within the container.
Example: "This is an example with:“
6. (break-spaces):
Preserves all spaces exactly as written (does not collapse spaces).
Preserves line breaks.
Spaces at the end of the line are also preserved.
Example: "This is an example with:“
Example:
<?php
// Text with extra spaces and line breaks
$text = "This is an example with:\n\n1. Extra spaces\n2. Line breaks\n3. Long
text with spaces.";
?>
Commenting in PHP :
Single-line comments:
1. Single-line comments are generally used for short explanations or notes
relevant to the local code.
2. There are two syntaxes for single-line comments in PHP:
Using #:
<?
# This is a comment, and
# This is the second line of the comment
// This is a comment too. Each style comments only
print "An example with single line comments";
?>
Multi-line comments:
1. Multi-line comments are generally used to provide pseudocode algorithms and
more detailed explanations when necessary.
2. The syntax for multi-line comments in PHP is the same as in CHere's an
example:
<?
/* This is a comment with multiline
Author: Mohammad Mohtashim
Purpose: Multiline Comments Demo
Subject: PHP
*/
print "An example with multi line comments";
PHP DATA TYPES
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types
that can be categorized further in 3 types:
4. boolean
5. integer
6. float
7. string
PHP Boolean
Booleans are the simplest data type works like switch. It holds only two values: TRUE (1) or
FALSE (0). It is often used with conditional statements. If the condition is correct, it returns
TRUE otherwise FALSE.
Example:
1. <?php
2. if (TRUE)
3. echo "This condition is TRUE.";
4. if (FALSE)
5. echo "This condition is FALSE.";
6. ?>
Output:
This condition is TRUE.
PHP Integer
Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e.,
numbers without fractional part or decimal points.
Rules for integer:
Example:
1. <?php
2. $n1 = 19.34;
3. $n2 = 54.472;
4. $sum = $n1 + $n2;
5. echo "Addition of floating numbers: " .$sum;
6. ?>
Output:
Addition of floating numbers: 73.812
PHP String
A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special
characters.
String values must be enclosed either within single quotes or in double quotes. But both are treated
differently. To clarify this, see the example below:
Example:
1. <?php
2. $company = "Javatpoint";
3. //both single and double quote statements will treat different
4. echo "Hello $company";
5. echo "</br>";
6. echo 'Hello $company';
7. ?>
Output:
Hello Javatpoint
Hello $company
PHP Data Types: Compound Types
Example:
1. <?php
2. $bikes = array ("Royal Enfield", "Yamaha", "KTM");
3. var_dump($bikes);
4. echo "Array Element1: $bikes[0] </br>";
5. echo "Array Element2: $bikes[1] </br>";
7. echo "Array Element3: $bikes[2] </br>";
8. ?> Output:
array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" }
Array Element1: Royal Enfield
Array Element2: Yamaha Array Element3: KTM
PHP object
Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly
declared. Example:
1. <?php
2. class bike {
3. function model() {
4. $model_name = "Royal Enfield";
5. echo "Bike Model: " .$model_name;
6. }
7. }
8. $obj = new bike();
9. $obj -> model();
10. ?>
Output:
Bike Model: Royal Enfield
PHP Data Types: Special Types
There are 2 special data types in PHP.
1. resource
2. NULL
PHP Resource
Resources are not the exact data type in PHP.
Basically, these are used to store some function calls
or references to external PHP resources.
For example - a database call. It is an external
resource
PHP Null
Null is a special data type that has only one value: NULL. There is a convention of
writing it in capital letters as it is case sensitive.
The special type of data type NULL defined a variable with no value.
Example:
1. <?php
2. $nl = NULL;
3. echo $nl; //it will not give any output
4. ?>
Keyword in PHP
In PHP, keywords are words that have special meanings in the
language and are reserved for specific purposes.
These words cannot be used as identifiers (such as variable names,
function names, or class
names) in your PHP code, because they are already reserved for built-
in functionality.
Examples: echo, if, else, foreach, class etc…
Control Flow Keywords
• if: Used for decision-making.
• else: Executes code if the if condition is false.
• elseif: Adds another condition to an if statement.
• switch: Tests a variable against multiple values.
• case: Defines a condition inside a switch.
• default: Provides a default action in a switch.
• for: Loop that repeats a block of code a specific number of times.
• foreach: Loop for iterating over arrays.
• while: Loop that repeats as long as a condition is true.
• do: Executes a loop at least once before checking the condition.
• break: Exits a loop or switch.
• continue: Skips the rest of the current loop iteration.
Class and Object Keywords
• class: Defines a blueprint for creating objects.
• public: Makes properties or methods accessible everywhere.
• private: Restricts access to properties/methods to the class itself.
• protected: Allows access within the class and its children.
• new: Creates a new object.
• extends: Inherits a class.
• implements: Implements an interface.
• this: Refers to the current object.
• self: Refers to the current class.
Function Keywords
• function: Defines reusable code (a function).
• return: Sends a value back from a function.
File Handling Keywords
• include: Includes an external file.
• require: Includes an external file and stops execution if it fails.
• include_once: Includes a file only once.
• require_once: Requires a file only once.
Error Handling Keywords
• try: Starts a block of code to test for errors.
• catch: Handles errors thrown in a try block.
• finally: Code that always executes after try/catch.
• throw: Manually triggers an exception.
Miscellaneous Keywords
• echo: Outputs text or data.
• print: Outputs text or data (similar to echo).
• isset: Checks if a variable is set and not null.
• unset: Destroys a variable.
• empty: Checks if a variable is empty.
• die / exit: Stops script execution.
• global: Accesses a global variable inside a function.
• static: Retains a variable's value between function calls.
• abstract: Defines a class/method that must be implemented in a
child class.
• final: Prevents a class or method from being overridden.
PHP VARIABLES
In PHP, a variable is declared using a $ sign followed by the variable name.
Syntax:$variablename=value;
Rules for declaring PHP variable:
o A variable must start with a dollar ($) sign, followed by the variable name.
o It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
o A variable name must start with a letter or underscore (_) character.
o A PHP variable name cannot contain spaces.
o One thing to be kept in mind that the variable name cannot start with a number or special symbols.
o PHP variables are case-sensitive, so $name and $NAME both are treated as different variable.
PHP EXPRESSION