Escolar Documentos
Profissional Documentos
Cultura Documentos
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:
The standard and the script tags are guaranteed to work under any
configuration, the other two need to be enabled in your php.ini
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.
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.
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
$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_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 ;