Php Tutorial
PHP ─ INTRODUCTION
• PHP is a recursive acronym for "PHP: Hypertext Preprocessor".
• PHP is a server-side scripting language that is embedded in HTML.
• It is used to manage dynamic content, databases, session tracking, even build entire e-
commerce sites.
• It is integrated with a number of popular databases, including MySQL, PostgreSQL,
Oracle, Sybase, Informix, and Microsoft SQL Server.
Common Uses of PHP
PHP performs system functions, i.e. from files on a system it can create, open, read, write,
and close them. The other uses of PHP are:
• PHP can handle forms, i.e. gather data from files, save data to a file, thru email you
can send data, return data to the user.
• You add, delete, modify elements within your database thru PHP.
• Access cookies variables and set cookies.
• Using PHP, you can restrict users to access some pages of your website.
• It can encrypt data.
Characteristics of PHP
Five important characteristics make PHP's practical nature possible:
• Simplicity
• Efficiency
• Security
• Flexibility
• Familiarity
Hello World" Script in PHP
Escaping to PHP
The PHP parsing engine needs a way to differentiate PHP code from other elements in the
page. The mechanism for doing so is known as 'escaping to PHP.' There are four ways to do
this:
i. Canonical PHP tags
ii. Short-open (SGML-style) tags
iii. ASP-style tags
iv. HTML script tags
PHP data Types
PHP supports several data types, which can be categorized into scalar, compound, and special
types:
i. Integer
a. Whole numbers, e.g., 10, -42, 0.
b. Example:
c. $x = 10;
ii. Float (Double)
a. Numbers with a decimal point or in exponential form, e.g., 3.14, 2.5e3.
b. Example:
c. $x = 3.14;
iii. String
a. A sequence of characters, e.g., "Hello, World!".
b. Example:
c. $x = "Hello, World!";
iv. Boolean
a. Represents TRUE or FALSE.
b. Example:
c. $x = true;
v. Array
a. An ordered map of values, e.g., [1, 2, "apple"].
b. Example:
c. $x = array(1, 2, "apple");
vi. Object
a. Represents an instance of a class.
b. Example:
c. class MyClass {
d. public $prop = "Hello";
e. }
f. $x = new MyClass();
vii. NULL
a. Represents a variable with no value.
b. Example:
c. $x = NULL;
viii. Resource
a. A special variable holding a reference to an external resource, like a database
connection or file handle.
b. Example:
c. $file = fopen("test.txt", "r");
Commenting PHP Code
i. Single-line comments:
This is a single line comment using double slashes // or hash symbol #
ii. Multi-lines comments:
Multi-lines printing:
PHP is whitespace insensitive
PHP whitespace insensitive means that it almost never matters how many whitespace characters
you have in a row.
PHP –Variables
1. Local variables
Variables declared inside a function are local and accessible only within that function.
2. Global variables
Variables declared outside a function are global and can’t be accessed inside a function directly.
Use the global keyword to access them.
3. Static variables
A static variable retains its value even after the function execution ends.
4. Function parameters
Function parameters are declared after the function name and inside parentheses.
PHP ─ CONSTANTS
• A constant is a name or an identifier for a simple value.
• A constant value cannot change during the execution of the script.
• By default, a constant is case-sensitive.
• By convention, constant identifiers are always uppercase.
• A constant name starts with a letter or underscore, followed by any number of letters,
numbers, or underscores.
• If you have defined a constant, it can never be changed or undefined
constant () function
As indicated by the name, this function will return the value of the constant. This is useful when
you want to retrieve value of a constant, but you do not know its name, i.e., it is stored in a
variable or returned by a function.
Differences between constants and variables are
• There is no need to write a dollar sign ($) before a constant, where as in Variable one has to
write a dollar sign.
• Constants cannot be defined by simple assignment; they may only be defined using the
define () function.
• Constants may be defined and accessed anywhere without regard to variable scoping rules.
• Once the Constants have been set, may not be redefined or undefined.
PHP Magic constants
PHP provides a large number of predefined constants to any script which it runs.
There are five magical constants that change depending on where they are used.
For example, the value of __LINE__ depends on the line that it's used on in your script.
These special constants are case-insensitive and are as follows:
PHP ─ OPERATOR TYPES
What is Operator?
Simple answer can be given using expression 4 + 5 is equal to 9.
Here 4 and 5 are called operands and + is called operator.
PHP language supports following type of operators.
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators