Php Tutorial
Php Tutorial
PHP ─ INTRODUCTION
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
• 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:
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");
i. Single-line comments:
This is a single line comment using double slashes // or hash symbol #
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
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 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.
What is Operator?
• Arithmetic Operators
• Comparison Operators
• Assignment Operators