Server Side Scripting
Server Side Scripting
PHP Variables
Variables are "containers" for storing information.
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two different
variables)
PHP data type
PHP has a total of eight data types which we use to construct our variables −
Integers − are whole numbers, without a decimal point, like 4195.
Doubles − are floating-point numbers, like 3.14159 or 49.1.
Booleans − have only two possible values either true or false.
NULL − is a special type that only has one value: NULL.
Strings − are sequences of characters, like 'PHP supports string operations.'
Arrays − are named and indexed collections of other values.
Objects − are instances of programmer-defined classes, which can package up
both other kinds of values and functions that are specific to the class.
Resources − are special variables that hold references to resources external to
PHP (such as database connections).
The first five are simple types, and the next two (arrays and objects) are compound
- the compound types can package up other arbitrary values of arbitrary type,
whereas the simple types cannot.
PHP Comments
A comment in PHP code is a line that is not executed as a part of the
program. Its only purpose is to be read by someone who is looking at the
code.
Comments can be used to:
Let others understand your code
Remind yourself of what you did - Most programmers have experienced
coming back to their own work a year or two later and having to re-
figure out what they did. Comments can remind you of what you were
thinking when you wrote the code
PHP supports several ways of commenting:
Syntax for single-line comments:
<?php
// This is a single-line comment
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
Arithmetic Operators
The PHP arithmetic operators are used to perform common arithmetic operations
such as addition, subtraction, etc. with numeric values.
Assignment Operators
The assignment operators are used to assign value to different variables. The basic
assignment operator is "=".
Bitwise Operators
The bitwise operators are used to perform bit-level operations on operands. These
operators allow the evaluation and manipulation of specific bits within the integer.
Comparison Operators
Comparison operators allow comparing two values, such as number or string.
Below the list of comparison operators are given:
Incrementing/Decrementing Operators
The increment and decrement operators are used to increase and decrease the value
of a variable.
Logical Operators
The logical operators are used to perform bit-level operations on operands. These
operators allow the evaluation and manipulation of specific bits within the integer.
String Operators
The string operators are used to perform the operation on strings. There are two
string operators in PHP, which are given below:
Operator Name Example Explanation
Variables Scopes
The scope of a variable is defined as its range in the program under which it
can be accessed.
In other words, "The scope of a variable is the portion of the program within
which it is defined and can be accessed."
PHP has three types of variable scopes:
Local variable
Global variable
Static variable
Local variable
The variables that are declared within a function are called local variables for
that function.
These local variables have their scope only in that particular function in which
they are declared.
This means that these variables cannot be accessed outside the function, as
they have local scope.
A variable declaration outside the function with the same name is completely
different from the variable declared inside the function.
Example:
<?php
function local_var()
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $num;
}
local_var();
?>
Global variable
The global variables are the variables that are declared outside the
function. These variables can be accessed anywhere in the program.
To access the global variable within a function, use the GLOBAL
keyword before the variable
. However, these variables can be directly accessed or used outside the
function without any keyword.
Therefore there is no need to use any keyword to access a global
variable outside the function.
Example:
<?php
$name = "Sanaya Sharma"; //Global Variable
function global_var()
{
global $name;
echo "Variable inside the function: ". $name;
echo "</br>";
}
global_var();
echo "Variable outside the function: ". $name;
?>
Static variable
It is a feature of PHP to delete the variable, once it completes its
execution and memory is freed.
Sometimes we need to store a variable even after completion of
function execution.
Therefore, another important feature of variable scoping is static
variable.
We use the static keyword before the variable to define a variable,
and this variable is called as static variable.
Example:
<?php
function static_var()
{
static $num1 = 3; //static variable
$num2 = 6; //Non-static variable
//increment in non-static variable
$num1++;
//increment in static variable
$num2++;
echo "Static: " .$num1 ."</br>";
echo "Non-static: " .$num2 ."</br>";
}
DROP Command
DROP is a DDL command used to delete/remove the database objects from the
SQL database.
Syntax:
DROP DATABASE Database_Name;
Example:
DROP DATABASE Books;
Syntax:
DROP TABLE Table_Name;
Example:
DROP TABLE Student;
ALTER Command
ALTER is a DDL command which changes or modifies the existing structure of
the database, and it also changes the schema of database objects.
Syntax to add a newfield in the table:
ALTER TABLE name_of_table ADD column_name column_definition;
ALTER TABLE table_name MODIFY(column_definitions....);
Example:
ALTER TABLE Student ADD Father's_Name Varchar(60);
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
TRUNCATE Command
TRUNCATE is another DDL command which deletes or removes all the records
from the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE;
INSERT
INSERT is another most important data manipulation command in Structured
Query Language, which allows users to insert data in database tables.
Syntax:
INSERT INTO TABLE_NAME ( column_Name1 , column_Name2 ,
column_Name3 , .... column_NameN )
Example:
INSERT INTO Student (Stu_id, Stu_Name, Stu_Marks, Stu_Age) VALUES
(104, Anmol, 89, 19);
UPDATE
UPDATE is another most important data manipulation command in Structured
Query Language, which allows users to update or modify the existing data in
database tables.
Syntax:
UPDATE Table_name SET [column_name1= value_1, ….., column_nameN =
value_N] WHERE CONDITION;
Example:
UPDATE Product SET Product_Price = 80 WHERE Product_Id = 'P102' ;
DELETE
DELETE is a DML command which allows SQL users to remove single or
multiple existing records from the database tables.
This command of Data Manipulation Language does not delete the stored data
permanently from the database. We use the WHERE clause with the DELETE
command to select specific rows from the table.
Syntax:
DELETE FROM Table_Name WHERE condition;
Example:
DELETE FROM Product WHERE Product_Id = 'P202' ;
DELETE FROM Student WHERE Stu_Marks > 70 ;
b. Revoke:
It is used to take back permissions from the user.
Example
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;