What is PHP
Variables ?
Variables are like the "containers" for storing information.
A variable can have a short
name (like a and b) or a more descriptive name (age, username, user_name).
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 variable names are case-sensitive.
- Example: $a=30, $name=”Lopa”.
PHP is
a Loosely Typed Language
In other languages such as C,
C++, and Java, the programmer must declare the name and type of the variable
before using it.
But we did not have to tell
PHP which data type the variable is.
PHP automatically converts the
variable to the correct data type, depending on its value.
Scope of PHP Variables
In PHP, variables can be
declared anywhere in the script.
The scope of a variable is the
part of the script where the variable can be referenced/used.
PHP has three different
variable scopes:
- local
- global
- static
1)Global Scope
A variable declared outside a
function has a GLOBAL SCOPE and can only be accessed outside a function:
Example:
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
2)Local Scope
A variable declared within a
function has a LOCAL SCOPE and can only be accessed within that function:
Example:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
3)Static Scope
A
variable declared as static called as static variable and that will not lose
its value when the function exits and will still hold that value should the
function be called again.
You
can declare a variable to be static simply by placing the keyword STATIC in
front of the variable name.
Syntax:
STATIC $x=0;
Example:
<?php
function myTest() {
static $x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
function myTest() {
static $x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
Naming Rule of a Variable:
Rules for
naming a variable is −
·
Variable
names must begin with a letter or underscore character.
·
A
variable name can consist of numbers, letters, underscores but you cannot use
characters like + , - , % , ( , ) . & , etc
There is no
size limit for variables.
Data Types in PHP
The values
assigned to a PHP variable may be of different data types including simple
string and numeric types to more complex data types like arrays and objects.
PHP supports total
eight primitive data types: Integer, Floating point number or Float, String,
Booleans, Array, Object, resource and NULL.
These data types
are used to construct variables.
1) PHP Integers
They are whole numbers, without a decimal point,
like (...,
-2, -1, 0, 1, 2, ...). They are the simplest type and they correspond to
simple whole numbers, both positive and negative.
Integers
can be assigned to variables, or they can be used in expressions, like so –
$var1=12345;
$another_var1=-12345+12345;
Integer
can be specified in decimal (base
10), octal (base 8), and hexadecimal (base 16) format.
Integer
can be optionally preceded by a sign –ve or +ve.
Decimal
format is the default, octal integers are specified with a leading 0, and
hexadecimals have a leading 0x.
For
most common platforms, the largest integer is (2**31 . 1) (or 2,147,483,647),
and the smallest (most negative) integer is . (2**31 . 1) (or .2,147,483,647).
<?php
$a=123; //decimal
number
var_dump($a);
echo “<br>”;
$b=-123; //a
negative number
var_dump($b);
echo “<br>”;
$c=0x1A; //hexadecimal
number
var_dump($c);
echo “<br>”;
$d=0123; //octal
number
var_dump($d);
echo “<br>”;
?>
PHP Floating Point Numbers or Doubles
They
like 1.14159 or 45.1.
By
default, doubles print with the minimum number of decimal places needed. For
example,
<?php
$a=2.2888800;
$b=2.2888800;
$new=$a+
$b;
print(“$a
+ $b= $new <br>”);
?>
Output
of above is 2.28888 + 2.21112=4.5
Floating point numbers (also known as "floats",
"doubles", or "real numbers") are decimal or fractional
numbers.
Examples:
<?php
$a=1.234;
var_dump($a);
echo “<br>”;
$b=10.2e3;
var_dump($b);
echo “<br>”;
$c=4E-10;
var_dump($c);
echo “<br>”;
3)PHP Booleans
They
have only two possible values either true or false. PHP provides a couple of
constants especially for use as Booleans: TRUE and FALSE, which can be used
like so
If(TRUE)
print(“this
will always print<br>”);
else
print(“this
will never print<br>”);
Booleans are like a switch it has only two possible
values either 1(true) or 0(false).
$show_booleans = true;
var_dump($show_booleans);
echo “<br>”;
Interpreting
other types as Booleans
Here are the
rules for determine the "truth" of any value not already of the
Boolean type −
·
If
the value is a number, it is false if exactly equal to zero and true otherwise.
·
If
the value is a string, it is false if the string is empty (has zero characters)
or is the string "0", and is true otherwise.
·
Values
of type NULL are always false.
·
If
the value is an array, it is false if it contains no other values, and it is
true otherwise. For an object, containing a value means having a member
variable that has been assigned a value.
·
Valid
resources are true (although some functions that return resources when they are
successful will return FALSE when unsuccessful).
·
We
Don't use double as Booleans.
4)PHP Arrays
An array is a variable that can hold more than one value
at a time. It is useful to aggregate a series of related items together, for
example a set of country or city names.
An array is formally defined as an indexed collection of
data values. Each index (also known as the key) of an array is unique and
references a corresponding value.
Example:
<?php
$name=array(“Ram”,”Hari”,”Gopal”);
var_dump($name);
echo “<br>”;
$title=array(
“Ram”=>”Mishra”,
“Hari”=>”Das”,
“Ram”=>”Mishra”
);
var_dump($title);
?>
5)PHP Strings
Strings are sequences of characters, where every
character is the same as a byte.
A string can hold letters, numbers, and special
characters and it can be as large as up to 2GB (2147483647 bytes maximum). The
simplest way to specify a string is to enclose it in single quotes (e.g. 'Hello
world!'), however you can also use double quotes ("Hello world!").
<?php
$a=”Hello World!”;
echo $a;
echo “<br>”;
$b=’Plz stay here,I\’ll
be back again’;
echo $b;
echo “<br>”;
6)PHP Objects
An object is a data type that not only allows storing
data but also information on, how to process that data. An object is a specific
instance of a class which serve as templates for objects. Objects are created
based on this template via the new keyword.
Every object has properties and methods corresponding to
those of its parent class. Every object instance is completely independent,
with its own properties and methods, and can thus be manipulated independently
of other objects of the same class.
Here's a simple example of a class definition followed by
the object creation.
Example:
<?php
//class definition
class newclass {
public $str=”Hello
World!”;//properties
function show_new(){
return $this->str;
}
}
$msg=new newclass;
var_dump($message);
?>
7)PHP NULL
The special NULL
value is used to represent empty variables in PHP. A variable of type NULL is a
variable without any data. NULL is the only possible value of type null.
<?php
$a=NULL;
var_dump($a);
echo “<br>”;
$b=”Hello World!”;
$b=NULL;
var_dump($b);
?>
When a variable is
created without a value in PHP like $var; it
is automatically assigned a value of null. Many novice PHP developers
mistakenly considered both $var1 =
NULL; and $var2 =
""; are same, but this is not true. Both variables are
different — the $var1 has null
value while $var2 indicates no value assigned
to it.
8)PHP Resources
A resource is a
special variable, holding a reference to an external resource.
Resource variables
typically hold special handlers to opened files and database connections.
<?php
$handle=fopen(“note.txt”,”r”);
var_dump($handle);
echo “<br>”;
// Connect to MySQL database server with default setting
$link=mysql_connect(“localhost”, “root”, “”);
var_dump($link);
?>