Nothing Special   »   [go: up one dir, main page]

CSCTR 000

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

WHAT IS PHP

1. PHP is a server-side scripting language designed


specifically for the Web

2. PHP was conceived in 1994 and was originally


developed by Rasmus Lerdorf

3. PHP code is interpreted at the Web server and


generates HTML or other output

4. PHP is an Open Source product and it is licensed


under GPL.

5. PHP is a recursive acronym whihc stands for PHP


Hypertext Preprocessor.
WHY PHP
1. PHP is vary fast.

2.PHP supports many databases viz.,


MySQL,PostgreSQL, Oracle, Sybase, Informix etc.,

3.It can connect to any database that provides


driver for Open DataBase Connectivity.

4. PHP has lots of extension like generating


images, generating pdf, mail , RSS, Web Services

5. PHP is cross platform.

6. It is easy to learn
BASIC SYNTAX

1. Can be embedded in HTML


<html>
<head><title>First Exercise</tilte></head>
<?php
echo "hello";
?>
<body>
World
</body>
</html>
TAGS

1. <? echo "hi how are you?"; ?>

2.<?php eho "Hello World"; ?>

3. <script langage="php"
echo "Hello World";
</script>

4. <% echo "Hello World"; %>


enable asp_tags in configuration file
DELIMITER

1. semicolon is used as instruction separator in PHP


COMMENTS
1. A double slash or # is used for single line comment
<?php
echo "This is one line comment"; //The part of
comment
echo "This is also a comment" ; #This is also part of
comment
?>

2. Multi line comment done in C or C++ way


<?php
/*
More than one line
is now commented
*/
?>
DATA TYPES

1. integer

2. Floating-point numbers

3. string

4. array

5. object
INTEGER

Integers can be assigned any of the following values:

$a = 120; # decimal number

$a = -123; # a negative number

$a = 0123; # octal number

$a = 0x12; # hexadecimal number


FLOATING POINT NUMBERS

Floating point numbers can be assigned any of the


following values:

$a = 1.234;

$a = 1.2e3;
STRING

Strings can be represented using eihter double quotes


or single quotes.

Assining value to a string variable.

$str = "This is a string";


ESCAPE SEQUENCES

\n linefeed

\r carriage return

\t horizontal tab
ARRAYS

1. PHP supports both indexed and key/value pair


array.

2. Create an array by setting values for each element


of an array
$arr[0]=1;
$arr[1]="hello";
$arr[2]="apple";
$arr[]=”flower”;

3. You can create an array using either list() or


array() functions.
OBJECTS

1. Objects are instances of classes.

class a
{
var $var="hello";

function echo_hello()
{
echo $this->var;
}
}
$a=new a();
TYPE CASTING

1. Type casting in PHP is done as below.


$a = 10; // $a is an integer
$b = (double) $b; //$b is double

2. The casts allowed are:


(int), (integer) - cast to integer
(real), (double), (float) - cast to double
(string) - cast to string
(array) - cast to array
(object) - cast to object
VARIABLES

1. Placeholder with name

2. The first letter of the varible name should be a


letter or underscore followed by letters, numbers and
underscores.

3. $_a="this a varible"; # valid


$b=100; # valid
$2c="test"; # invalid, variable name
starts with a digit
VARIABLE VARIABLES

1. A variable variable takes the value of a varible and


assigns it as the name of a varible
$a="hello";
$$a="this is a test";

2. echo $a $hello;
CONSTANTS

1. Constants are defined using the keyword define


define('HELLO',"hello”);
echo HELLO;
PREDEFINED VARIABLES

1. APACHE VARIABLES

2. PHP VARIABLES
APACHE VARIABLES
REQUEST_METHOD
GET, HEAD, POST, PUT.

QUERY_STRING
The query string, if any, via which the page was
accessed.

DOCUMENT_ROOT
The document root directory under which the
current script is executing, as defined in the server's
configuration file.

HTTP_USER_AGENT
Contents of the User_Agent
PHP VARIABLES
PHP_SELF
The filename of the currently executing script.
(not for cli)
$_COOKIE
An associative array of variables passed to the
current script via HTTP cookies.
$_GET
An associative array of variables passed to the
current script via the HTTP GET method.
$_POST
An associative array of variables passed to the
current script via the HTTP POST method.
$_FILES
An associative array of variables containing
information about files uploaded via the HTTP
SCOPE OF VARIABLE

1. Visibility or the life of the variable

2. PHP variables only a have single scope. This single


scope spans included and required files also.
$a=10;
include "footer.inc";

3. variables declared inside user-defined functions


have local scope
GLOBAL SCOPE

$visible=10;

function test()
{
print $visible;
}

test();

PHP will not look at the global variable when it


encounters an variable inside a fucntion.
OVERRIDE LOCAL SCOPE
$visible=10;

function test()
{
global $visible;
print $visible;
}

test();

function test()
{
print $GLOBALS['visible'];
}

test();
OPERATORS

1. Arithmetic Operators
2. String Operators
3. Assignment Operators
4. Comparison Operators
5. Logical Operators
6. Bitwise Operators
7. The Ternary Operator
8. The Error Control Operator
9. The Execution Operator
BITWISE OPERATORS

bitwise AND &

bitwise OR |

bitwise NOT ~

bitwise XOR ^

left shift <<

right shift >>


ARITHMETIC OPERATORS

'+' - Addition

'-' - Subtraction

'* - Multipication

'/' - Division

'%' - Modulus
COMPARISON OPERATORS

== equals
=== identical
!= not equal
<> not equal
< less than
> greater than
<= less than or equal to
>= greater than or equal to
LOGICAL OPERATORS

NOT !

AND &&

OR ||
BITWISE OPERATORS

bitwise AND &

bitwise OR |

bitwise NOT ~

bitwise XOR ^

left shift <<

right shift >>


String Operators
The dot operator is used as concatenation.
$a="hello"."world";

Assignment Operators
The '=' is used as the assignment operator.
$a+=5;
which is equivalent to
$a=$a+5;

The Ternary Operator


This operator, ?:, takes the form
condition ? value if true : value if false
The error control operator
@ - can be used in front of any expression

The Execution Operator


a pair of backticks (``)
PHP will attempt to execute whatever is contained
between the backticks as a system command.
VARIABLE FUNCTIONS

1. gettype(parameter)
2. settype(variable name, datatype as string)
3. is_int()
4. is_float($a)
5. is_string($a)
6. is_object($a)
7. int isset($a)
8. int isset($a)
9. int empty($a)
10. intval($a)
11. doubleval($a)
12. strval($a)
CONTROL STRUCTURES

To control the flow of execution

1. If-ElseIf-Else
2. while
3. Switch
4. break
5. continue
6. exit
7. for
8. foreach
9. include statements
IF-ELSEIF-ELSE

if ($a>$b)
{
echo "a is bigger than b";
}
elseif($b>$a)
{
echo "b is bigger than a";
}
else
{
echo "a and b are equal";
}
SWITCH

switch($gender)
{
case 'male' :
echo "gender is male";
break;

case 'female' :
echo "gender is female";
break;

default :
echo "none of the above";
break;
}
WHILE
$i=0;
while($i<10)
{
echo $i;
$i++;
}

$a=10;
do
{
echo $a;
$a--;
} while ($a > 1 );
BREAK

$a=0;

while($a<10)
{
$a++;
if($a==5)
{
break;
}
echo $a;
}
CONTINUE

$a=0;

while($a<10)
{
$a++;
if($a==5)
{
continue;
}
echo $a;
}
EXIT

$a=0;

while($a<10)
{
$a++;
if($a==5)
{
exit;
}
echo $a;
}
FOR

for ($i=0; $i<10; $i++)


{
echo $i;
}
FOREACH
$arr=array(1,2,3,45);
foreach($arr as $a)
{
echo $a;
}
$a = array (
"lab" => “192.168.1.10”,
"office" =>"192.168.1.12",
"gateway" => "192.168.1.1",
3 => "hosts"
);
foreach($arr as $key=>$value)
{
echo "Key : $key; Value: $value\n";
}
INCLUDE STATEMENTS

1. require (“connect.inc”);

2. include (“connect.inc”);

3. include_once(“connect.inc”);

4. require_once (“connect.inc”);
FUNCTIONS

1. A function is a self-contained module of code that


prescribes a calling interface, performs some task,
and optionally returns a result.

2.To write a function in php the 'function' keyword


followed by the name of the function and then list of
arguments passed to the function inside braces.
Optionally the function may return some value.
BASIC FUNCTION STRUCTURE

function name_of_function(arg1,arg2,......)
{

any valid php code


return anyvalue;

}
FUNCTION NAME

1. Function cannot have the same name as an


existing function.

2. Function name can only contain letters, digits, and


underscores

3. Function name cannot begin with a digit


FUNCTION DEFINITION
1. function square()
{
$a=5;
$b=$a*$a;
echo $b;
}

2. function square($a)
{
$b=$a*$a;
return $b;
}
FUNCTION CALL

1. square();

2. $c=square(5);
echo $c;

3. echo square(5);
SCOPE OF VARIABLE
1. A variable defined inside a function is visible only
within that i.e., the scope of the variable is local inside
a function.

2. To create a global variable inside a function use the


keyword 'global' before the variable.
function echo_var()
{
global $var = "globally scoped";
echo "The value of var = ".$var."\n";
}

echo "The value of var outside= ".$var."\n";


RECURSIVE FUNCTION
1. A recursive function is one that calls itself.
Recursive functions are slower and use more memory
than iteration, so you should use iteration wherever
possible.

function factorial($n)
{
if($n==1)
return 1;
else
return $n*factorial($n-1);
}
echo factorial(5);
PASS BY REFERENCE

If you wish to pass a variable by reference to a


function which does not do this by default, you may
prepend an ampersand to the argument name in the
function call:

$a=5;
echo factorial(&$a);
echo $a;
ARRAYS

Arrays actually act like both hash tables


(associative arrays) and indexed arrays
(vectors).
TYPE OF ARRAYS
This type array is supported in most of the programming
languages. Arrays can be created by assinging values to
each elements of the array using the index.
$arr[0]="flower";
$arr[1]="fruit";
$arr[2]="numbers";

In PHP, the indices start at zero by default. However, we can


instruct array( ) to use a different index using the =>
syntax:
$arr = array(1 => 'flower', 'fruit', 'vegetable', 'cereals');

Alternatively arrays can be created using the function array.


$arr=array("flower","fruit","numbers");
ACCESSING ARRAY ELEMENTS

$arr=array("flower","fruit","numbers");

The array elements can be accessed using their


index.
echo $arr[0];

A For loop can be used to access the elements of an


array
for($i=0; $i<3; $i++)
{
echo $arr[$i];
}
ASSOCIATIVE ARRAY

1. An associative array is an array which has its index


as key.

2. The key can be in any form either a string or


number etc.,

3. The associative array is created as the same a


scalar array is created.

4. The value of keys should be unique.


$fruits = array('red' => 'Apples', 'yellow' =>
'Bananas', 'beige' => 'Cantaloupes', 'brown' =>
'Dates');

while(list($key,$val)=each($fruits))
{
echo "key: $key; Value: $val\n";
}
MULTIDIMENSIONAL ARRAY

$employees = array(
array( "ram","manager", 10000 ),
array( "sam", "executive", 15000 ),
array( "raj", "vp", 40000 )
);
ARRAY FUNCTION
1. Sorting Arrays
$arr=array("flower","fruit","numbers");
sort($arr);

2. An associative array can be sorted in ascending


order, using asort() or ksort().

asort is used to sort the associative array based on


the value of the each item.

ksort is used to sort the array based on the keys.

3. Sorting Multidimensional Arrays is done using usort


usort(array array,function cmp_function)
ARRAY FUNCTIONS
1. array_reverse is a function used to reverse the
order of an array
2. $pwd=file("/etc/dhcp3/dhcp.conf");
print_r($pwd)
3. To modify every element in an array in the same
way use array_walk(array,function name);
function mul($a)
{
$a*=2;
print $a;
}
4. array_count_values($array), this function counts
how many times each unique value occurs in the
array $array. Creates an associative array with key as
array elements and value as no of occurances.

You might also like