variable and constant - PHP variable & constant where Use
Important thing about variable
Let’s first
understand that what is variable? or what do you mean by variable? In general
term variable means change-able or the capability which storing some data is
known as variable. The data may be a number, string, array etc.
Important
thing about variable in PHP
·
Like other programming
language PHP doesn’t having data types like. (int, float, char, string, array
etc..).
·
Its data type will be
identifying through its value, means what value you store in PHP variable then
that value will decide what type of variable is that.
·
Syntax $variable_name
·
every variable in PHP will
start with a ‘$’ sign and then follow by variable name
·
In above example I declare
two variable $name and $age, $name variable contain string type value and $age
contain integer type value.
·
If you want to know its
data types then use “gettype()” method it we show the datatype
·
If you want to know data
type with its value, then use “var_dump($varaiblename)” method
·
Types of Variables
There are
three different types of variable in PHP
·
Local: Those variable which are created within
function or block of code known as local variable. Local variable will not
access outside of the function or block of code because it scope is bound
within that function or block of code in which they are declared
·
Global: Those variable which are created outside the function
or block of code and are known as global variable. global variable will be
access within function or block of code and also outside the function or block
of code. If you want to access any global variable inside the function or block
of code, then to do this you have to explicitly say that it’s a global variable
for this you have to declare variable with global keyword inside of the
function or block of code.
·
Example: without
global keyword
·
Static: Those variable which are declare as static
is known as static variable. The purpose of these static variable is to
access local variable after function execution complete. Let’s see the code you
will understand this very well
·
Note: static
variable will occupy memory once so they are alive till the program execution
once you declare a variable as a static then if you change its value then its
will be saved with previous value.
PHP Constants:
·
In PHP constant is a name or
identifier for a simple value, that constant value never during the execution
of the PHP script.
·
Constant values are very
useful only when we want that the value of variable is never change in entire
program
·
It will be very useful when we connect our web
application with any database, once we initialize the database name to our
application then its name will not be changed again.
·
To create constant in PHP,
we will use define() function.
·
Constants are naturally global;
you will access them inside or outside of the functions
·
Syntax
. Define(name, value, case-insensitive)
. name: Specifies
name of the constant variable
. value: Specifies
value of the constant variable
. case-insensitive: Specifies name of constant should be
case-sensitive, by default case-sensitive is false
·
Example: of constant
·
PHP Echo and Print
. Echo and print these two
are different built-in function in PHP
. Both are used to display
data on the browser
. Syntax of echo: echo() or echo “”;
.Syntax of print: print() or print “”;
.Echo statements support
multiple parameter and it is non-return type function
. Print statements support
only single parameter and it is return type function
·
PHP
Operators
The thing
which is used to perform some operation on variables or values is known as
Operators
PHP support
following type of operators.
·
Arithmetic operators
·
Assignment Operators
·
Comparison Operators
·
Increment/Decrement
Operators
·
Logical Operators
Arithmetic
operators
Simple
algebraic operators are known as Arithmetic operators
Operator Description Result
+ Add two variables $a+$b
- Subtract two variables $a-$b
* Multiply two variables $a*$b
/ Divide two variables $a/$b
% of two variables $a%$b
It is same
like arithmetic but after performing arithmetic between to variables it stores
the result to left hand variable
Operator
|
Description
|
Result
|
=
|
It is used to assign value
|
$x = $y
|
+=
|
It will add two variable then result will assign to left variable
|
$x+=$y or $x=$x+$y
|
-=
|
It will subtract two variable then result will
assign to left variable
|
$x-=$y or $x=$x-$y
|
*=
|
It will multiply two variable then result will assign to left
variable
|
$x*=$y or $x=$x*$y
|
/=
|
It will divide two variable then result will assign
to left variable
|
$x/+=$y or $x=$x/$y
|
%=
|
It will remainder two variable then result will assign to left
variable
|
$x%=$y or $x=$x%$y
|
Comparison
Operators
Comparison
operators are used to compare the two variables by its value and also by its
data types
Operator Name Example Result
== Equal to $x == $y Return true if both are equal
!= Not Equal to $x != $y Return true if both are not equal
=== Identical $x === $y Return true if both having same value and same datatype
!== Not Identical $x!== $y Return true if both having same value or same datatype
<> Not Equal to $x<>$y Return true if both are not equal to each other
< Less than $x<$y Return true if $x has value less than $y
<= Less than or equal to $x<= $y Return true if $x has value less or equal to $y
> Greater than $x>$y Return true if $x has value greater than $y
>= Greater than or equal to $x>= $y Return true if $x has value greater or equal to $y
Increment
operator is used to increase the value by one and decrement operator is used to
decrease the value by one.Increment/Decrement
Operators
Operator Name Description
$x++ Post-increment First print the value then increment by one
++$a Pre-increment First increment the value by one then print
$x-- Post-decrement First print the value then decrement by one
--$x Pre-decrement First decrement the value by one then print
Logical Operators
Logical
operators are used to valid the conditional statements
Operator Name Description
and And Return True if both variable are true
or Or Return True if any one variable is true
xor Xor Return if either any variable is true not both
&& And Return True if both variable are true
|| OR Return True if any one variable is true
! Not Return True if variable is false
Decision
making means make a suitable decision and execute that particular part of the
program, so for decision making we use if, if else, if elseif and switch. Let’s
understand these things in details
IF
statements
When you
want to execute the particular code then you can use IF statement, if statement
is true then block of if statements will execute.
Syntax
If(some conditions) {
Statements
statements
}
IF Else
statements
If “if
statement” is true, then if statement block will execute otherwise else block
will execute
Syntax
If (some condition) {
Statements
} else {
Statements
}
IF ElseIf
Else statements
It contains
multiple if conditions, if any if statement is true the if that respected if
block will execute otherwise else block will execute
Syntax
If (some condition) {
Statements
} elseIf (some condition) {
Statements
} else {
Statements
}
Switch
Statement
Switch
statement is also used for decision making. It works same like “If Elseif Else”
statement. In switch statement multiple case are defined, if any case match
with user input then that matched case will be execute. If none of case will
execute then default will execute. Default work like else block.
Syntax:
Switch (input) {
Case n1:
Statements;
break;
Case n2:
Statements;
break;
Case n3:
Statements;
break;
. . . . . .
default:
Statements;
}
No comments:
Post a Comment