Você está na página 1de 17

<?

php
print "Hello World!";
?>
In this script PHP tags are used to separate the actual
PHP content from the rest of the file.
You can inform the interpreter that you want it to execute
your commands by adding a pair of such tags:

standard tags <?php ?>;


short tags <? ?>;
ASP tags <% %>;
script tags <SCRIPT LANGUAGE=php> </SCRIPT>.

The standard and the script tags are guaranteed to work under any
configuration, the other two need to be enabled in your php.ini

print and echo


Both are used to print data on screen.
Difference between print and echo is that print
returns value indicating success or failure,
whereas echo doesnt return any such value.
echo() can take multiple expressions. Print
cannot take multiple expressions.
echo "The first", "the second";
echo has the slight performance advantage
because it doesn't have a return value.

Terminating Execution
exit() and die() are used to terminate script
execution.
exit() takes either string or number as an
argument, prints that argument and then
terminates execution of script.
The die() function is an alias for exit().
$filename = '/path/prog1.php';
$file = fopen($filename, 'r') or exit("unable to open file ($filename)");
$connection=mysql_connect(192.168.0.1,user,pass) ;
if ( ! $connection ) die (Connection not established.);

functions
Syntax:
function function_name()
{
/*
function statements
*/
return result;
}
Variables defined in a function are local by default.
To access any variable of function out of that function, use global variables.
function sum($a,$b)
{
global $c;
$c=$a+$b;
}
$c=0;
sum ( 5 , 1 );
print $c;
o/p - > 6

Static Variables
If you don't want to alter value of a functions
variable outside your function, and you still want to retain your
variable, you can use the static variable.
A static variable exists only in a local function scope, but it does
not lose its value when program execution leaves this scope.
function sum($a,$b)
{
static $c=0;
$c=$a+$b;
print Value of \$c in function is $c \n;
}
$c=3;
sum ( 5 , 1 );
print Value of \$c outside the function is $c \n;
o/p
Value of \$c in function is 6
Value of \$c in outside the function is 3

Arrays
PHP arrays are associative arrays because they
associates keys with values.
You can use it either as a simple c like array or
as an associative array.
It is similar to perls hash.
Here array indices are enclosed into [] rather
than {}.
Rather than having a fixed number of slots, php
creates array slots as new elements are added
to the array.
You can assign any type for keys and values
.such as string, float ,integer etc.

Syntax to create an array:

For simple array:


$arr=array(ele1,ele2,ele3);
OR
$arr[0]=ele1;
$arr[1]=ele2;
$arr[2]=ele3;
OR
$arr[]=ele1;
$arr[]=ele2;
$arr[]=ele3;
OR
$arr=( 0 => ele1 , 1=> ele2 , 2 => ele3 );
For associative array :
$arr[key1]=val1;
$arr[key2]=val2;
$arr[4]=val3;
OR
$arr=(key1=>val1 , key2=>val2 , 4 => val3 );

To create empty array ,


$arr=array();
After creating array like this, you can add elements using
any of the above methods.
You can print the array with print.
print $arr;
To retrieve array element:
$val=$arr[0];
OR
$val=$arr[key1];
OR You can assign your array values to list of scalars.
list($val1,$val2,$val3)=$arr;
List is reverse of array because array packages its
arguments into array and list takes array and assign its
values to list of individual variables.

Array Functions
is_array()
syntax : [true/false] = is_array(array );
If variable is of type array, then is_array function
will return true otherwise false.
count()
syntax: [no. of eles.] = count ( array );
It returns number of elements in the given array.
in_array()
syntax: [true/false] = in_array( array , value ) ;
It checks if value exists in given array or not.
Isset ( $arr[$key] ) . Returns true if key $key
exists in array.

Functions to traverse through an array:


current() function returns stored value that the
current pointer points to.
Initially, current() will point to the first element in
the array.
next(array) function returns the current value
and then advances array pointer. Returns false if
no next element is available.
reset() function sets the pointer to the first
element & returns the stored value.
prev() sets the pointer to the next element.
end() sets the pointer to the last element.
key() returns key of current element.
each() returns the current key and value pair
from an array and advances the array pointer.

e.g.
$transport = array(bus', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = bus';
$mode = next($transport); // $mode = 'bike';
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport); // $mode = bus';
$mode = end($transport); // $mode = 'plane';
$mode = reset($transport); // $mode = 'plane';
$mode = key($transport); // $mode = 3 ;
$array_cell=each($transport);
// $array_cell[key] will be 3 and
// $array_cell[value] will be plane

Traversing an array with while loop .


$arr = array("one", "two", "three");
reset ($arr);
while (list( , $value) = each ($arr)) {
echo "Value: $value<br>\n";
}
reset ($arr);
foreach ($arr as $value) {
echo "Value: $value<br>\n";
}
For both loops , o/p
Value: one
Value: two
Value: three

Traversing an associative array with loop .

$a = array (
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
while (list( $key , $value) = each ($a)) {
echo $key => $value<br>\n";
}
reset($a);
foreach ($a as $key => $value )
{
print "\$a[$key] => $value.\n";
}

o/p
one => 1
two => 2
three => 3

array_keys()
array array_keys ( array input [, mixed search_value])

array_keys() returns the keys from the input array.


If the optional search_value is specified, then only the keys for that value
are returned. Otherwise, all the keys from the input are returned.
array_values ()
array array_values ( array input)

array_values() returns all the values from the input array and indexes the
array numerically.
array_count_values ()
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
Returns an array using the values of the input array as keys and their
frequency as values.

o/p
Array (
[1] => 2 ,
[hello] => 2 ,
[world] => 1 )

array_flip ()
array array_flip ( array trans)
Exchanges all keys with their associated values in an array
If a value has several occurrences, the latest key will be used as its values, and
all others will be lost.
array_flip() returns FALSE if it fails.
$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);
o/p -> 1=>b , 2=>c
array_reverse ()
array array_reverse ( array array [, bool preserve_keys])
array_reverse() takes input array and returns a new array with the order of the
elements reversed, preserving the keys if preserve_keys is TRUE.
array_merge ()
array array_merge ( array array1, array array2 [, array ...])
It merges two or more arrays.
$arr1= (a=>1,b=>2);
$arr2= (C=>3, D=>4);
$arr_result=array_merge($arr1,$arr2);
OR
$arr_result= $arr1 + $arr2 ;

array_slice() Extract a slice of the array .


array array_slice ( array array, int offset [, int length])
Returns the sequence of elements from the array as specified by the
offset and length parameters.
If offset is positive, the sequence will start at that offset in the array. If
offset is negative, the sequence will start from the end of the array.
If length is given and is positive, then the sequence will have that many
elements in it. If length is given and is negative then the sequence
will stop that many elements from the end of the array. If it is
omitted, then the sequence will have everything from offset up until
the end of the array.
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2);
// returns "c", "d", and "e"
$output = array_slice($input, 2, -1); // returns "c", "d"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"

array_splice() Remove a portion of the array and


replace it with other elements .
array array_splice ( array input, int offset [, int
length [, array replacement]])
array_splice() removes the elements
designated by offset and length from the input
array, and replaces them with the elements of
the replacement array, if supplied. It returns an
array containing the extracted elements.
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

Você também pode gostar