Transcript PhP - qu
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).
• 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.
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
PHP file
• PHP files can contain text, HTML tags and
scripts
• PHP files are returned to the browser as plain
HTML
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
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
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-SP1Redistributable-Package-x86/3000-2247_4-75450985.html
Installing Wamp
• If the installation went well, you should have an new icon in
the bottom right, where the clock is:
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
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
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
?>
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 */
A PHP script
<html>
<body>
<?php echo “hello PHP”; ?>
</html>
The above program will display hello in the
browser
Example 1: title
• <html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
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
Example 2
<html>
<body>
<?php echo "hello PHP";
$txt="Hello World!";
$x=16;
echo $txt, $x;
?>
</html>
Conditions
• If else
if(condn) {….}
elseif(condn) {….}
else { ….}
• Switch case
switch(var)
{
case c1: statements;break
.
.
Default: statements; break;
}
Example 3
<html>
<body>
Extract 3 character value from
the system date
<?php
$d=date("D");
if ($d==“Wed")
{
echo "Have a nice weekend! <br />";
echo “ Good bye”;
}
else
{
echo "Have a nice day!";
}
?>
</body>
</html>
newline
Loops
• For
for(intialisation;condition;increment/decrement)
{ statements }
• While
while(condn) { ….}
• Do While
do {….} while(condn);
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