PHP - REFERENCE
PHP - REFERENCE
PHP Overview
1. Handling Forms: PHP can handle form operations. It can gather data, save
data to a file and send data through emails.
2. Database Operations: PHP can also create, read, update and delete
elements in your database.
3. Encryption: It can perform advanced encryption and encrypt data for you.
4. Dynamic Page Content: It can generate dynamic page content.
Basic Syntax PHP
PHP comments
Single line Comments
Multiline comment
Variables in PHP
Variables are containers that can store information which can be manipulated
or referenced later by the programmer within the code.
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
How to Run PHP file?
To run PHP file we want a server here we are using WAMP
In the following path you can create a folder and store all the PHP files in it:
C:\wamp64\www
In the WAMP localhost it will show the folder
String
Integer
Float
Boolean
Array
NULL
1.String
A string is a sequence of characters that holds letters and numbers. It can
be anything written inside single or double quotes.
For Example:
2. Integer
An integer is a non-decimal number typically ranging between -2,147,483,648
and 2,147,483,647.
3. Float
A float is a number with a decimal point. It can be an exponential number or
a fraction.
4. Boolean
A Boolean represents two values: True or False.
5. Array
Array is a collection of similar data elements stored in a single variable.
6. NULL
Null is a special data type with only one value which is NULL. In PHP, if a
variable is created without passing a value, it will automatically assign itself a
value of NULL.
PHP Operators
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. PHP Increment/ Decrement Operators
5. PHP Logical Operators
6. PHP String Operators
7. PHP Array Operators
8. PHP Conditional Operators
PHP Conditional Statements
PHP Conditional Statements
Conditional Statements are used to perform actions based on different
conditions. Sometimes when we write a program, we want to perform some
different actions for different actions. We can solve this by using conditional
statements.
In PHP we have these conditional statements:
1. if Statement.
2. if-else Statement
3. If-elseif-else Statement
4. Switch statement
1. if Statement
This statement executes the block of code inside the if statement if the expression
is evaluated as True.
2. if-else Statement
This statement executes the block of code inside the if statement if the
expression is evaluated as True and executes the block of code inside the else
statement if the expression is evaluated as False.
If-else-if-else
This statement executes different expressions for more than two conditions.
4. Switch Statement
This statement allows us to execute different blocks of code based on
different conditions. Rather than using if-elseif-if, we can use the switch
statement to make our program.
PHP Iterative Statements
PHP Iterative Statements
Iterative statements are used to run same block of code over and over again
for a certain number of times.
In PHP, we have the following loops:
1. while Loop
2. do-while Loop
3. for Loop
4. foreach loop
1. While Loop
The While loop in PHP is used when we need to execute a block of code again
and again based on a given condition. If the condition never becomes false,
the while loop keeps getting executed. Such a loop is known as an infinite
loop.
2. Do-While Loop
The do-while loop is similar to a while loop except that it is guaranteed to
execute at least once. After executing a part of a program for once, the rest of
the code gets executed based on a given boolean condition.
3. For Loop
The for loop is used to iterate a block of code multiple times.
4. Foreach loop
The foreach loop in PHP can be used to access the array indexes in PHP. It
only works on arrays and objects.
Function Basics
Function arguments are variables of some supported data type that are
processed within the body of the function. It can take input as an argument
and return value.
PHP has more than 1000 built-in functions, and in addition, you can also
create your own functions.
Advantages:
1. Functions reduce the complexity of a program and give it a modular
structure.
2. A function can be defined only once and called many times.
3. It saves a lot of code writing because you don't need to write the same
logic multiple times, you can write the logic once and reuse it.
PHP Built-in Functions
PHP has over 1000 built-in functions that can be called directly, from within a
script, to perform a specific task.
Please check out our PHP reference for a complete overview of the PHP built-
in functions.
User Defined Functions: Apart from built-in functions, We can also create our
own functions and call them easily.
A user-defined function looks something like this:
<?php
Function functionname(){
//Code
}
functionname(); // Calling Function
?>
Function Arguments
Function Arguments: Argument is just like a variable which can be used to pass
through information to functions.
PHP supports Call by Value, Call by Reference, Default Argument Values and
Variable-length argument.
1. Call by Value
In Call by Value, the value of a variable is passed directly. This means if the value
of a variable within the function is changed, it does not get changed outside of
the function.
2. Call by Reference
In call by reference, the address of a variable (their memory location) is
passed. In the case of call by reference, we prepend an ampersand (&) to
the argument name in the function definition. Any change in variable
value within a function can reflect the change in the original value of a
variable.
3. Default Argument Values
If we call a function without arguments, then PHP function takes the default
value as an argument.
4. Variable Length Argument
It is used when we need to pass n number of arguments in a function. To use
this, we need to write three dots inside the parenthesis before the argument.
Arrays
An array is a collection of data items of the same data type. And
it is also known as a subscript variable.
1. Numeric Array
These are arrays with a numeric index where values are stored and accessed
in a linear fashion.
2. Associative Array
These are arrays with string as an index where it stores element values
associated with key values
3. Multidimensional Arrays
A multidimensional Array is an array containing one or more arrays where
values are accessed using multiple indices.
PHP-MySQl
What is MYSQL?
MySQL is a Database Management System.