LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Chapter 1: Introduction
Dr. Mogeeb Moslehh
Date: 15 March 2014
Saturday (9.00-1.00pm) SE3Lecture Room: 405
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
History• Started as a Perl hack in 1994 by Rasmus Lerdorf
(to handle his resume), developed to PHP/FI 2.0• By 1997 up to PHP 3.0 with a new parser engine
by Zeev Suraski and Andi Gutmans• Version 5.2.4 is current version, rewritten by Zend
(www.zend.com) to include a number of features, such as an object model
• Current is version 5• php is one of the premier examples of what an
open source project can be
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Why php?• ..there are no. of server side scripting available like ASP,
SSJS, JSP…..• PHP involves
– simplicity in scripting (..generally using the database)
– platform independence.
• PHP is – primarily designed for web applications
– well optimized for the response times needed for web applications
• Is an open source.
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
4
PHP Overview • PHP Hypertext Preprocessor.
– Other Names : Personal Home Page, Professional Home Page
• PHP is a server-side scripting language whose scripts are embedded in HTML documents
• Is a server side scripting language.– Capable of generating the HTML pages
• HTML generates the web page with the static text and images.
• However the need evolved for dynamic web based application, mostly involving database usage.
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
5
Installing Software• Install a Web server
– http://httpd.apache.org/
• Install PHP– http://www.php.net/downloads.php
• Install MySQL– http://dev.mysql.com/downloads/
• Portable WAMP server– http://www.uniformserver.com/
• NetBeans IDE: Quickly and easily develop desktop, mobile and web applicationswith Java, HTML5, PHP, C/C++ and more
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Installation Options• XAMPP: is a very easy to install package for (Linux-
Windows- Apple). http://www.apachefriends.org/• Apache, MySQL, PHP, Perl, and many more.• WampServer will install Apache, PHP5 and MySQL on
your Windows.
http://www.wampserver.com/• ZendFramework:
- the most popular framework for modern, high performance php application. (http://www.zend.com/)– Modular - Extensible– High Performing - Secure
6
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Zend Framework• the most popular framework for modern, high performance php
application. (http://www.zend.com/)– Modular - Extensible– High Performing - Secure
• Zend provides Zend engine for PHP for free• They provide other products and services for a fee
– Server side caching and other optimizations– Encoding in Zend's intermediate format to protect source
code– IDE-a developer's package with tools to make life easier
• Zend's web site is a great resource
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
PHP 5 Architecture• Zend engine as parser (Andi Gutmans and Zeev Suraski)• SAPI is a web server abstraction layer• PHP components now self contained (ODBC, Java,
LDAP, etc.)• This structure is a good general design for software
(compare to OSI model, and middleware applications)
image from http://www.zend.com/zend/art/intro.php
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
9
Understanding Binary and Source Code Installations• Binary format (or binaries) refer to compiled files, such
as executable installation programs• Source code is the original programming code in which
an application was written• Source code must be compiled, or processed, and
assembled into an executable format before it is used• Compiled programs only need to be recompiled when
their code changes
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
10
Installing and Configuring a Web Server• Apache is the most popular Web server software used
on the Internet• Microsoft IIS for Windows is the second most popular
server software• In Windows, a service refers to a program that performs
a specific function to support other programs.• Install NetBeans Software.
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
11
Testing Your Web Server
1. Open your Web browser2. Type http://localhost/ in the Address box,
click Enter
Apache’s default Web page
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
12
Testing Your Web Server (continued)
3. Type http://127.0.0.1/ in the Address box, click Enter
Web page informing you that IIS is running
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
13
Configuring Apache• To configure ports and other settings you must edit the
httpd.conf file• For UNIX/Linux
– /usr/local/apache2/conf
• For Windows– C:\Program Files\Apache Group\Apache2\conf
• Lines that begin with the pound sign (#) are informational comments
• Lines without pound signs contain directives
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
14
Configuring Apache (continued)
httpd.conf
• Directives define information about how a program should be configured
• The DocumentRoot directive identifies the default directory from where Apache serves Web pages
• The Alias directive identifies other directories that Apache can use to serve Web pages
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
15
Configuring Internet Information Services
Default Web Site Properties dialog box
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
16
Configuring Apache for PHP on Windows
1. Click the Start menu and point to All Programs
2. Select the Edit the Apache httpd.config Configuration File command
3. Add the following to the end of the file:ScriptAlias /PHP/ “C:/PHP/”AddType application/x-httpd-php .phpAction application/x-httpd-php “/PHP/php-cgi.exe”
4. Save and close the httpd.conf file5. Restart Apache and select the Restart
command
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
17
Configuring PHP
The php.ini configuration file
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
18
Testing the MySQL Server1. Check to see if MySQL is running
• For UNIX/Linux systems:
/usr/local/mysql/bin/mysqld_safe --user=mysql &
• For Windows, use the Services window
2. Run the mysqladmin version command• For UNIX/Linux systems:
/usr/local/mysql/bin/mysqladmin version• For Windows, change to the C:\Program Files\MySQL\
MySQL Server 4.1\bin\ directory and run:
mysqladmin version
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
19
Configuring the Uniform Server– php.ini is located in:
\UniServer\usr\local\php
– httpd.conf is located in:\UniServer\usr\local\apache2\conf
– my is located in:\UniServer\usr\local\mysql
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
20
Creating Basic PHP Scripts• Embedded language refers to code that is embedded within
a Web page (XHTML document)• PHP code is typed directly into a Web page as
a separate section• A Web page document containing PHP code must have an
extension of .php• PHP code is never sent to a client’s Web browser.• The Web page generated from the PHP code, and HTML or
XHTML elements found within the PHP file, is returned to the client
• A PHP file that does not contain any PHP code should have an .html extension
• .php is the default extension that most Web servers use to process PHP scripts
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
PHP Block• There are four different ways to embed the PHP code
– <?php echo(“Some PHP code”); ?>– <? echo(“Some PHP code”); ?>– <SCRIPT Language=‘php’> echo(“Some PHP code”); </SCRIPT>– <% echo(“Some PHP code”); %>
• Code declaration blocks are separate sections within a Web page that are interpreted by the scripting engine
• PHP code block is embedded within the <?php and ?> tags.• When the server encounters the PHP tags it switches from the HTML to
PHP mode.
• There are four types of code declaration blocks:– Standard PHP script delimiters– The <script> element – Short PHP script delimiters– ASP-style script delimiters
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
22
Standard PHP Script Delimiters
• A delimiter is a character or sequence of characters used to mark the beginning and end of a code segment
• The standard method of writing PHP code declaration blocks is to use the <?php and ?> script delimiters
• The individual lines of code that make up a PHP script are called statements
• The <script> Element:– The <script> element identifies a script section in a Web
page document– Assign a value of "php" to the language attribute of the <script> element to identify the code block as PHP
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Basic PHP Script<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Basic PHP Page</title>
</head>
<body>
<p>This is standard HTML.</p>
<?php
?>
</body>
</html>
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Running a PHP Script
• Must have a PHP-enabled Web server (e.g., Apache)• Place the PHP script in the Web directory• Access the file in your browser through a URL
(http…)
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Sending Data to the Browser
echo 'Hello, world!';
echo "What's new?";
print 'Hello, world!';
print "What's new?";
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Sending Data to the Browser
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Using Echo</title>
</head>
<body>
<!-- Script 1.3 - second.php -->
<p>This is standard HTML.</p>
<?php
echo 'This was generated using PHP!';
?>
</body>
</html>
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Sending HTMLto the Browser
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Hello, World!</title>
</head>
<body>
<?php
echo '<p>Hello, <b>world</b>!</p>';
?>
</body>
</html>
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Parse Errors
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
29
Defining Constants• A constant contains information that does not change during
the course of program execution• Constant names do not begin with a dollar sign• Constant names use all uppercase letters • Use the define() function to create a constant
define("CONSTANT_NAME", value);define("VOTING_AGE",18);define("VOTING_AGE",18,TRUE);
• The value you pass to the define() function can be a text string, number, or Boolean value.
• ..values that never changes
• defined() function says whether the constant exists or not.
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
30
Working with Data Types• A data type is the specific category of information
that a variable contains• Data types that can be assigned only a single value
are called primitive types
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
PHP Data Type & Primitives• Four scalar types:
Boolean, integer, double, and string• Two compound types:
array and object• Two special types:
resource and NULL• Integer & double are like those of other languages• Boolean -values are true and false (case insensitive)• PHP is an untyped language
– variables type can change on the fly.
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
PHP Variables• The variables in PHP are declared by appending the $
sign to the variable name.– For e.g
$company = “NCST”;
$sum = 10.0;
• variable’s data type is changed by the value that is assigned to the variable.
• Type casting allows to change the data type explicitly.• Rich set of functions for working with variable.
– For e.g• gettype, settype, isset, unset, is_int, intval etc etc
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
33
Working with Data Types• The PHP language supports:
– A resource data type – a special variable that holds a reference to an external resource such as a database or XML file
– Reference or composite data types, which contain multiple values or complex types of information
– Two reference data types: arrays and objects
• Language type• Strongly typed programming languages require you to declare the data
types of variables • Static or strong typing refers to data types that do not change after they
have been declared• Loosely typed programming languages do not require you to declare the
data types of variables• Dynamic or loose typing refers to data types that can change after they
have been declared
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
34
Numeric Data Types
PHP supports two numeric data types:• An integer is a positive or negative number with no
decimal places (-250, 2, 100, 10,000)• A floating-point number is a number that contains
decimal places or that is written in exponential notation (-6.16, 3.17, 2.7541)– Exponential notation, or scientific notation, is short for
writing very large numbers or numbers with many decimal places (2.0e11)
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
35
Boolean Values
• A Boolean value is a value of true or false• It decides which part of a program should execute
and which part should compare data• In PHP programming, you can only use true or false• In other programming languages, you can use
integers such as 1 = true, 0 = false
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
36
Dynamic Typing
• $Variable = "Hello World";• $Variable = 8;• $Variable = 5.367;• $Variable = TRUE;• $Variable = NULL;
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
PHP Operators
• All the operators such as arithmetic, assignment, Comparison, and logical operators are similar to the operators in C and C++.
• In PHP the string concatenation operator is denoted by ‘.’.– For e.g.
• $name = “My name is”.$myname;
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Syntactic Characteristics • PHP code can be specified in an HTML document
internally or externally:• Internally: <? php... ?>• Externally: include ("myScript.inc")
– the file can have both PHP and HTML – If the file has PHP, the PHP must be in <?php.. ?
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Syntactic Characteristics • Every variable name begins with a $
– Case sensitive
• Comments -three different kinds (Java and Perl)– // ...– # ... – /* ... */
• PHP statements terminated with ;• Compound statements are formed with braces• Compound statements cannot define locally scoped
variables (except functions)
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Reserved words of PHP
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Arithmetic & Assignment Operators
Pre- and Post-Increment and Decrement--, ++
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Comparison Operators
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Logical Operators
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Output
• Output from a PHP script is HTML that is sent to the browser
• HTML is sent to the browser through standard output• There are three ways to produce output: echo, print,
and printf• Echo and print take a string, but will coerce other
values to strings• echo “Hello there!"; echo(“Hello there!”); echo $sum;• print "Welcome!"; print(“Wellcome”); print (46);
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Writing Comments
<!-- HTML comment -->
<?php
# Single line PHP comment.
// Another type of single line PHP comment
/* This is a comment
that can go over
multiple lines. */
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Variable Types
• Boolean• Integer• Floating point• String
• Array• Objects• Resources• Null
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Variable Names
• Start with a $• Contain only letters, numbers, and the underscore• The first character after the $ cannot be a number• Are case-sensitive• Use a consistent naming scheme!
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Printing Variables
print $some_var;
print "Hello, $name";
print 'Hello, $name'; // Won't work!
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
String Values
• 'Tobias'• "In watermelon sugar"• '100'• 'August 2, 2011'
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Creating and Using Strings
$first_name = 'Tobias';
$today = 'August 2, 2011';
$var = "Define \"platitude\", please.";
$var = 'Define "platitude", please.';
echo $first_name;
echo "Hello, $first_name";
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Performing Concatenation
$city= 'Seattle';
$state = 'Washington';
$address = $city . ', ' . $state;
// Or:
$city= 'Seattle';
$state = 'Washington';
$address = $city;
$address .= ', ';
$address .= $state;
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Numeric Values
• 8• 3.14• 10823479• -4.29048• 4.4e2
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Creating and Using Numbers
$n = 3.14;
$n = round ($n); // 3
$n = 3.142857;
$n = round ($n, 3); // 3.143
$n = 20943;
$n = number_format ($n); // 20,943
$n = 20943;
$n = number_format ($n, 2); // 20,943.00
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Performing Arithmetic<?php # Script 1.8 - numbers.php
// Set the variables:
$quantity = 30; // Buying 30 widgets.
$price = 119.95;
$taxrate = .05; // 5% sales tax.
// Calculate the total:
$total = $quantity * $price;
$total = $total + ($total * $taxrate); // Calculate and add the tax.
// Format the total:
$total = number_format ($total, 2);
// Print the results:
echo '<p>You are purchasing <b>' . $quantity . '</b> widget(s) at a cost of <b>$' . $price . '</b> each. With tax, the total comes to <b>$' . $total . '</b>.</p>';
?>
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Creating and Using Constants
define ('NAME', value);
define ('USERNAME', 'troutocity');
define ('PI', 3.14);
echo "Hello, USERNAME"; // Won't work!
echo 'Hello, ' . USERNAME;
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Quotation Marks
$var = 'test';
echo "var is equal to $var";
echo 'var is equal to $var';
echo "\$var is equal to $var";
echo '\$var is equal to $var'
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Escape Sequences
Code Meaning
\" Double quotation mark
\' Single quotation mark
\\ Backslash
\n Newline
\r Carriage return
\t Tab
\$ Dollar sign
LEARN THE QUICK AND EASY WAY!
VISUAL QUICKPRO GUIDE
Basic Debugging
• Always run PHP scripts through a URL• Know the version of PHP you’re using• Enable display_errors• Check the HTML source code• Trust the error message• Take a break!