You are on page 1of 33

Introduction to PHP

What is PHP?
PHP is a widely-used server-side scripting language that is especially suited for Web development and can be embedded into HTML. PHP stands for PHP: Hypertext Preprocessor (Formerly Personal Home Page)

How to start?
Install Apache (or IIS) on your own server, install PHP, and MySQL Installing WAMP Server includes MySQL and Apache All files should be copied in the folder C:\wamp\www\..... Alias Directories can also be created

How to start?
Need a Text Editor PHP files have a file extension of ".php", ".php3", or ".phtml Any web development applications can be used. Eg: Adobe Dreamweaver

Programs
A PHP script always starts with
<?php and ends with ?>.

A PHP script can be placed anywhere in the document. Eg: <html> <body> <?php echo "Hello World";?> This Part is HTML <?php echoOne more script;?> </body> </html>

Comments
// or # to make a one-line comment or /* */ to make a comment block Eg: <?php //This is a comment /* This is a comment block */ ?>

PHP Variables
<?php $txt="Hello World!"; $x=16; $f=16.7 $x=$f; ?>

PHP Variables
<?php $auth = true; echo gettype($auth); // returns "boolean" $age = 27; echo gettype($age); // returns Integer" $name = Mepco'; echo gettype($name); // returns "string" $temp = 98.6; echo gettype($temp); // returns double ?>

PHP Variables
PHP also supports a number of specialized functions to check if a variable or value belongs to a specific type
is_bool() Checks if a variable or value is Boolean is_string() Checks if a variable or value is a string is_numeric() Checks if a variable or value is a numeric string is_float() Checks if a variable or value is a floating point number is_int() Checks if a variable or value is an integer is_null() Checks if a variable or value is NULL is_array() Checks if a variable is an array is_object() Checks if a variable is an object

String
Can be represented within single or double quotes
Eg $str=hi or hi;

Concatenation .
Eg: $txt1="Hello World!"; $txt2="What a nice day!"; echo $txt1 . " " . $txt2;

strlen(hi); //2 strpos(hi mepco, mepco);//3 strcmp(abc,efg); //-1 stristr(abcdef,c);//cdef

Operators and Control Statements


Similar to C and C++ Eg: $d=10 if (($d%2)==0) echo Even number"; else echo Odd number;

Arrays
Numeric array
$cars=array("Saab","Volvo","BMW","Toyota");

Associative Arrays
Eg: $months=array(jan=>1,feb=>2);

Multidimensional Arrays $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ) );

Arrays
$dept=array(CSE,ECE,EEE); echo $dept[0]; // CSE
---------------------array(key=>value) $hod=array(cse=>Dr.KM,IT=>Dr.TR) echo $hod*cse+; ----------------------

How to define Array


<?php // Numeric Array $flavors[0] = 'strawberry'; $flavors[1] = 'grape'; //Associative Array $fruits['red'] = 'apple'; $fruits['yellow'] = 'banana'; ?>

How to add element


Use index & key $flavors[2] = mango';//Numeric Array // if you don't know the next available index $flavors[] = 'mango'; // add an element to an associative array $fruits['pink'] = 'peach';

How to display Array elements


// print array elements for ($x = 0; $x < sizeof($dept);$x++) echo "<li>$dept[$x]"; ----------------------------------------------------------foreach ($dept as $x) echo "<li>$x"; ----------------------------------------------------------foreach($hod as $key => $value) echo $key $value;

Some array functions


array_pop() array_push() array_search() array_shift()

Receiving data from HTML Forms


Using GET
Information sent from a form will be displayed in the browser's address bar. Received by using the variable $_GET Eg: $name=$_GET*text1"+;

Using POST $name=$_POST*text1"+;


Information sent from a form with the POST method is invisible to others Received by using the variable $_POST Eg: $name=$_POST*text1"+;

Receiving data from HTML Forms


Using REQUEST
used to collect form data sent with both the GET and POST methods. Received by using the variable $_REQUEST Eg:$name=$_REQUEST*text1"+;

Date() Function
getdate() function returns an associative array containing keys for the current hour, minute, second, day, date,month, and year. <?php $current = getdate(); // get current date and time // turn it into a string $current_time = $current['hours'] . ':' . $current['minutes'] . ':' . $current['seconds']; $current_date = $current['mday'] . '.' . $current['mon'] . '.' . $current['year']; echo "It is now $current_time on $current_date"; ?>

Importing & ReDirection


include() require() include_once() require_once() header()

Cookies
The setcookie() function is used to set a cookie
Eg: setcookie(name, bala, time()+3600);

$_COOKIE variable is used to retrieve a cookie value


Eg: echo $_COOKIE["user"];

isset() function to find out if a cookie has been set


Eg: if (isset($_COOKIE*"user"+)) echo cookie available;

To delete a cookie the expiration date is set as past


Eg: setcookie(name, , time()-3600);

Session
To Start a session : <?php session_start(); ?>

To store and retrieve values, use $_SESSION variable


Eg: $_SESSION*name+=bala; echo $_SESSION*name+;

To remove a variable in the session


Eg: unset($_SESSION*name+);

To destroy a session
Eg: session_destroy();

Database Connectivity
Connecting to a database
$con =mysql_connect("localhost","peter","abc123"); If(!$con) die('Could not connect: ' . mysql_error());

Selecting a database
mysql_select_db(Sample", $con);

Closing a connection
mysql_close($con);

Executing a query
We can use mysql_query() function
mysql_query(select * from stu,$con); or $sq=select * from stu; mysql_query($sq,$con)

Passing user defined parameters


To pass user data into the query
$sq=select * from stu where name= $_POST*t1+;

To retrieve the results


$result = mysql_query("SELECT * FROM Persons"); while($rst=mysql_fetch_array($result)) echo $rst[0] or echo $rst*name+

Introduction to AJAX

AJAX
AJAX = Asynchronous JavaScript And XML AJAX uses JavaScript to send and receive data between a web browser and a web server. In traditional web applications, the server returns a new web page each time the user submits input. With AJAX, web applications can send and retrieve data without reloading the whole web page. This is done by sending HTTP requests to the server, and by modifying only parts of the web page using JavaScript when the server returns data

Famous Applications that uses AJAX


Google Maps Gmail Google suggest Flickr

How it works?
A client event occurs An XMLHttpRequest object is created The XMLHttpRequest object is configured The XMLHttpRequest object makes an asynchronous request to the Webserver. Webserver returns the result containing XML document. The XMLHttpRequest object calls the callback() function and processes the result. The HTML DOM is updated

Simple AJAX example


Step 1: Create AJAX object for the browser Step 2: Create a function to send the request to the server Step 3: Create a function to receive the response from the server

Database example

References
www.w3schools.com www.tutorialspoint.com www.tizag.com en.wikipedia.org

You might also like