PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands...

20
PHP By Dr. Syed Noman Hasany

Transcript of PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands...

Page 1: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

PHP

ByDr. Syed Noman Hasany

Page 2: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

PHP• PHP was originally created by Rasmus Lerdorf in 1995.• PHP stands for PHP: Hypertext Preprocessor (a recursive

acronym).• Hypertext is the underlying concept defining the structure

of the World Wide Web, is the text displayed on a computer or other electronic device with references (hyperlinks) to other text.

• PHP has term “preprocessor” because PHP libraries are already compiled and processed. when any person request any PHP page in browser address bar that request first go to server for example Apache which interprets PHP files and return back response in form of HTML.

Page 3: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

PHP

• PHP is a server-side scripting language, like ASP• An embedded scripting language for HTML. • PHP scripts are executed on the server• PHP supports many databases (MySQL,

Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)

• PHP is an open source software, so it is free to download and use

Page 4: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

PHP file

• PHP files can contain text, HTML tags and scripts

• PHP files are returned to the browser as plain HTML

Page 5: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Why PHP?

• PHP runs on different platforms (Windows, Linux, Unix, etc.)

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

• PHP is easy to learn ( syntax like C) and runs efficiently on the server side

Page 6: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

MySQL

• MySQL is a database server• MySQL is ideal for both small and large

applications• MySQL supports standard SQL• MySQL compiles on a number of platforms• MySQL is free to download and use

Page 7: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

PHP + MySQL + Apache• PHP combined with MySQL are cross-platform (you can

develop in Windows and serve on a Unix platform)• To get access to a web server with PHP support, you

can: Install Apache on your own server, install PHP, and MySQL

• Wamp server: All in one download and installation for Windows (visual C++ 2010 is needed)– http://sourceforge.net/projects/wampserver/files/latest/download – http

://download.cnet.com/Microsoft-Visual-C-2010-SP1-Redistributable-Package-x86/3000-2247_4-75450985.html

Page 8: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Installing Wamp• If the installation went well, you should have an new icon in

the bottom right, where the clock is:

Page 9: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Localhost• Localhost just refers to the server running on your own

computer. Another way to refer to your server is by using the IP address 127.0.0.1

Page 10: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Running PHP scripts

• Suppose created php script name is first.php√ http://localhost/first.php

• if a folder under www contains the file, then:√ http://localhost/folder_name/script_name.php

• Wrong formats:× c:/wamp/www/test1.php× http://localhost/www/test1.php

Page 11: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Basic PHP Syntax

• A PHP script starts with <?PHP and ends with ?> and can be placed anywhere in the document.

• On servers with shorthand-support, <? and end with ?>, but not recommended due to possible xml conflicts.

<?PHP?>

Page 12: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

PHP Comments

• All php files are saved with extension .php• Can be written in notepad or any text editor• Single line comment– // this is single line comment

• Multi line comment– /*….

This is a multi line comment */

Page 13: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

A PHP script

<html><body>

<?php echo “hello PHP”; ?></html>

The above program will display hello in the browser

Page 14: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Example 1: title

• <html> <head> <title>Example</title> </head> <body>

<?php echo "Hi, I'm a PHP script!"; ?>

</body></html>

Page 15: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Variables

• Case sensitive• Variables start with a $ symbol• Variables can contain _ or numbers or alphabets• $ should be followed by _ or alphabet and not

by a number• PHP is loosely typed language. There is no strict

data typing• Variable can be assigned with any values

Page 16: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Example 2

<html><body>

<?php echo "hello PHP"; $txt="Hello World!";$x=16;

echo $txt, $x;?></html>

Page 17: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Conditions• If else

if(condn) {….}elseif(condn) {….}else { ….}

• Switch caseswitch(var){case c1: statements;break..Default: statements; break;}

Page 18: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Example 3<html><body>

<?php$d=date("D");if ($d==“Wed") { echo "Have a nice weekend! <br />";

echo “ Good bye”; }else { echo "Have a nice day!"; }?></body></html>

Extract 3 character value from the system date

newline

Page 19: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Loops

• Forfor(intialisation;condition;increment/decrement){ statements }

• While while(condn) { ….}

• Do Whiledo {….} while(condn);

Page 20: PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in 1995. PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).

Example 4• <html>

<body>

<?php$i=1;do { $i++; echo "The number is " , $i , "<br />";

echo "The number is " . $i . "<br />"; }while ($i<=5);?>

</body></html>

, or . (dot) as concatenation operator