Professional Documents
Culture Documents
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;
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+; ----------------------
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"; ?>
Cookies
The setcookie() function is used to set a cookie
Eg: setcookie(name, bala, time()+3600);
Session
To Start a session : <?php session_start(); ?>
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)
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
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
Database example
References
www.w3schools.com www.tutorialspoint.com www.tizag.com en.wikipedia.org