php basic presentation

43

Upload: krutitrivedi

Post on 19-Jul-2015

65 views

Category:

Software


2 download

TRANSCRIPT

PHP

➢What is open source? Why do people prefer using open source software?

➢What is PHP?

➢What is PHP File?

➢What Can PHP Do?

➢Why PHP?

➢Who Uses PHP?History.

OPEN SOURCE

● Open source software is different. Its authors make its source code available to others who would like to view that code, copy it, learn from it, alter it, or share it.

● It means that anyone should be able to modify the source code to suit his or her needs.

What is PHP?

● PHP: Hypertext Preprocessor

● PHP is a widely-used, open source scripting language

● PHP is free to download and use.

● It is deep enough to run the largest social network (Facebook)!

What is a PHP File?

● PHP files can contain text, HTML, CSS, JavaScript, and PHP code.

● PHP code are executed on the server, and the result is returned to the browser as plain HTML.

● PHP files have extension ".php"

What Can PHP Do?

● PHP can generate dynamic page content

● PHP can create, open, read, write, delete, and close files on the server

● PHP can collect form data

● PHP can add, delete, modify data in your database

Why PHP?

● PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)

● PHP is compatible with almost all servers used today (Apache,IIS,etc).

● PHP supports a wide range of databases

● PHP is free. Download it from the official PHP resource: www.php.net

● PHP is easy to learn and runs efficiently on the server side

WHO USES PHP?

PHP HISTORY

● Created by Rusmus Lerdorf

● PHP/FI 1.0 relesed in 1995

● PHP/FI 2.0 relesed in 1997

● PHP 3.0 relesed in 1998

● PHP 4.0 relesed in 2005

● PHP 5.0 relesed in 2009

● PHP 7.0 relesed in 2014

INSTALLATION

INSTALLATION

INSTALLATION

INSTALLATION

PHP BASIC

● Create New Folder within wamp/www/folder with the name you want to give your project.

● E.G. c:\wamp\www\myproject.

● Open your Browser and type localhost/myproject.

PHP SYNTAX

● Each php script must be enclosed with revered php tags

● <?php

...code

● ?>

● All php statements end with semi-colon. ;

PHP SYNTAX

● Each php script must be enclosed with revered php tags

● <?php

...code

● ?>

● All php statements end with semi-colon. ;

Commenting

● // comment a single line● # comment single line-shell style● /* comment multiple

lines */

Echo Command

● The simple use of echo command is print a string as a argument in php.

● <?php

echo “HELLO WORLD” ;

?>

● Use \n for new line & <br/> for new line

echo “line 1 \n”;

echo “line 2”; &

echo “line1 <br/>”;

echo “line 2”;

Out put = line1 line 2 (\n).Out put of <br/>= line 1

Line 2

Variables

● Variables are identified and define by prefixing theier name with dollor sign.

● E.G $var1,$var2

● Variables may contain .(letter,underscore,number or dash) there should be no space.

● Variable name are case-sensitive.

● $thisVar,$ThisvAr

Example of Variable

● $items

● $Items

● $_blog

● $product3

● $this-var

● $this_var

● $_bookPage

Variable variables

● $varName = ”age”;

● $$varName = “20”;

● This is exactlly equvivalent to

● $age = 20;

Constants

● Constants are define with difine() function

● E.G : define(“variable1”,”value”);

● Constants name follow same rule as a variable name.

● Valid constant

define(“firstname”,”a”);

define(“last_name”,”b”);

define(“_1bookprice”,”22”);

define(“USER”,”Modi”);

echo “Welcome “. USER;

OUT PUT: Welcome Modi

Data Types

● boolean :A value that can only either be true or false

● Int :A signed numeric integer value

● float : A signed floating-point value

● string : A collection of binary data

● Null is a special value that indicates no value.

● Arrays are containers of ordered data elements

● Objects are containers of both data and code

Define Data Types

● $int_var = 12345; Integer

● $temperature = 56.89; Floating Point

● $first_double = 123.456; Double

● TRUE=1, FALSE=0 Boolean

● $name = “abrakadabra” String

Operators

● Assignment (x = y,x += y, x -= y, x *= y, x /= y, x %= y)

● Arithmetic (+, - ,* ,/ ,%)

● Comparison (==,===,!=,<>,!==,>,<,>=,<=)

● Logical (and, or,xor,&&,||,!)

● String (. , .=)

Control Structures

● Conditional Control Structures

● If

● If else

● Switch

● Loops

● For

● while

● Do while

Errors and Error Management

Compile-time errors Detected by parser while compiling a script. Cannot be trapped within the script itself.

Fatal errors Halt the execution of a script. Cannot be trapped.

Recoverable errors Represent significant failures but can be handled in a safe way.

Warnings Recoverable errors that indicate a run-time fault. Does not halt execution.

Notices Indicates that an error condition has occurred but is not necessarily significant. Does not halt script execution

functions

● A function is a block of statements that can be used repeatedly in a program.

● A function will not execute immediately when a page loads.

● A function will be executed by a call to the function.

● Syntax

function functionName() {

code to be executed;

}

Example of function

● <?php

function writeMsg() {

echo "Hello world!";

}

writeMsg(); // call the function

?>

● Out put : Hello World!

Array

● An array stores multiple values in one single variable: array();

<?php

$cars = array("Xylo", "BMW", "Swift");

echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

?>

● Out put : I like Xylo, BMW and Swift.

DataBase Connection (db)//get connect to mysql

$link = mysql_connect($host,$username,$password);

//check for connection

if (!$link) {

die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully <br/>';

$database='learndb';

if(mysql_select_db($database))

{ echo &quot;we are working with database: &quot;.$database;

} else {

echo &quot;There is some error: &quot;.mysql_error(); die();

}

//close the DB connection

mysql_close($link); ?>

Output: Connected successfully we are working with database: learndb

How to select values from MySQL tables

● Function : mysql_query($sql)

● Description :Send a MySQL query Syntax :resource

● mysql_query ( string $query [, resource $link_identifier ] )

● Example : Add following code before

● mysql_close($link) statement.

● //Start Querying table

● $selectSQL='select * from employee';

● $resultSet=mysql_query($selectSQL);

● echo &quot;<table border='1'>&quot;; echo &quot;<tr><td><b>Name</b></td><td><b>Department</b></td>

● <td><b>Designation</b></td></tr>&quot;;

Session

● you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state.

● Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.

● So; Session variables hold information about one single user, and are available to all pages in one application.

Session

● A session is started with the session_start() function.

● <?php //Start the session

session_start();

?>

● <html><body>

<?php

// Set session variables

$_SESSION["favcolor"] = "green";

$_SESSION["favanimal"] = "cat";

echo "Session variables are set.";

?>

</body></html>

What is a Cookie?

● A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

● setcookie(name, value, expire, path, domain, secure, httponly);

Cookie

● Cookies are text files stored on the client computer and they are kept of use tracking purpose. PHP transparently supports HTTP cookies.

● Server script sends a set of cookies to the browser. For example name, age, or identification number etc.

● Browser stores this information on local machine for future use

● When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user.

GET & POST

● The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? Character.

● http://www.test.com/index.htm?name1=value1&name2=value2

● The GET method produces a long string that appears in your server logs, in the browser's Location: box.

● The GET method is restricted to send upto 1024 characters only.

● Never use GET method if you have password or other sensitive information to be sent to the server.

● GET can't be used to send binary data, like images or word documents, to the server.

● The data sent by GET method can be accessed using QUERY_STRING environment variable.

GET & POST

● The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

● POST requests are never cached

● POST requests do not remain in the browser history

● POST requests cannot be bookmarked

● POST requests have no restrictions on data length

● <?php

● if( $_POST["name"] || $_POST["age"] )

● {

● echo "Welcome ". $_POST['name']. "<br />";

● echo "You are ". $_POST['age']. " years old.";

● exit();

● }

● ?>

● <html>

● <body>

● <form action="<?php $_PHP_SELF ?>" method="POST">

● Name: <input type="text" name="name" />

● Age: <input type="text" name="age" />

● <input type="submit" />

● </form>

● </body>

● </html>

PHP FRAME WORKS

● Joomla

● Wordpress

● Zend cart

● Cake PHP

● Drupal

● Smarty

● Magento

● YII