You are on page 1of 6

Constant does not start with $ symbol.

8 Datatypes is there: integer, float, string,


Boolean, array, objects, references, NULL
Single and double quotation marks work in
different ways. If you enclose a string in single
quotation marks, PHP uses the string exactly as
typed. However, double quotation marks give you a
couple of extra features:
Any variable names within the string are parsed
and replaced with the variable s value
You can include special characters in the string by

escaping them
$favoriteAnimal = cat;
echo My favorite animals are {$favoriteAnimal}s;
This produces the expected result:
My favorite animals are cats
If you need to extract a sequence of characters
from a string, you can use PHP s substr() function.

This
function takes the following parameters:
The string to extract the characters from
The position to start extracting the characters. If
you use a negative number, substr() counts
backward from the end of the string

The number of characters to extract. If you use a


negative number, substr() misses that many
characters from the end of the string instead.
This parameter is optional; if left out, substr()
extracts from the start position to the end of the
string
strstr() tells you whether the search text is within
the string
strpos() and strrpos() return the index position of
the first and last occurrence of the search
text, respectively
substr_count() tells you how many times the
search text occurs within the string
strpbrk() searches a string for any of a list of
characters
To find out exactly where a string of text occurs
within another string, use strpos() . This function
takes the same two parameters as strstr() : the
string to search, and the search text to look for.
If the text is found, strpos() returns the index of
the first character of the text within the string.
If its not found, strpos() returns false

STR_PAD_RIGHT to pad the string on the right


(the default setting), left - aligning the string
STR_PAD_LEFT to pad the string on the left,
right - aligning the string
STR_PAD_BOTH to pad the string on both the
left and the right, centering the result as much as
possible
Many modern programming languages including
PHP support two types of arrays:
Indexed arrays These are arrays where each
element is referenced by a numeric index, usually
starting from zero. For example, the first element
has an index of 0, the second has an index of 1, and
so on
Associative arrays This type of array is also
referred to as a hash or map. With associative
arrays, each element is referenced by a string
index. For example, you might create an array
element representing a customer s age and give it
an index of age
sort() and rsort() For sorting indexed arrays
asort() and arsort() For sorting associative
arrays

ksort() and krsort() For sorting associative


arrays by key rather than by value
array_multisort() A powerful function that can
sort multiple arrays at once, or multidimensional
arrays
array_unshift() Adds one or more new elements
to the start of an array
array_shift() Removes the first element from
the start of an array
array_push() Adds one or more new elements to
the end of an array
array_pop() Removes the last element from the
end of an array
array_splice() Removes element(s) from and/or
adds element(s) to any point in an array
array_merge() This function is useful for
merging two or more arrays together
explode() and implode() These let you convert
between arrays and strings
list() You can use this to store array elements
in a list of individual variables
you don t need to have created a variable outside
a function to use it as a global variable. Take a
look at the following script:

function setup() {
global $myGlobal;
$myGlobal = Hello there!;
}
function hello() {
global $myGlobal;
echo $myGlobal < br/ > ;
}
setup();
hello(); // Displays Hello there!
you can also access global variables using the

$GLOBALS array. This array is a special type of


variable called a superglobal , which means you can
access it from anywhere without using the global
statement. It contains a list of all global variables,
with the variable names stored in its keys and the
variables values stored in its values. Here s an

example that uses $GLOBALS :


$myGlobal = Hello there!;
function hello() {
echo $GLOBALS[myGlobal] . < br/ > ;
}
hello(); // Displays Hello there!
unlike local variables, which disappear when a

function exits, static variables remember their


values from one function call to the next.
To declare a local variable as static, all you need to
do is write the word static before the variable
name, and assign an initial value to the variable:
static $var = 0;
To get a function to accept an argument as a
reference rather than a value, put an ampersand

( & ) before
the parameter name within the function definition:
function myFunc( & $aReference ){
// (do stuff with $aReference)
}
you place an ampersand before the function name

in your function
definition. Then, when you return a variable with

the return statement, you pass a reference to that


variable back to the calling code, rather than the
variable s value