php basics

21
National Institute of Management and Administration, NIMA Handouts by Rooh Ul Amin Mashwani MSc Computer Science, UOP MS Project Management (In Progress), COMSATAS UNIVERSITY [email protected], +93 766637774 Advanced Website Deigning PHP & MySQL <?php $server=mysql_connect ($host, $username, $password); ?>

Upload: roohul-amin

Post on 14-Apr-2017

173 views

Category:

Education


0 download

TRANSCRIPT

National Institute of Management and Administration, NIMA

Handouts by Rooh Ul Amin Mashwani

MSc Computer Science, UOP

MS Project Management (In Progress), COMSATAS UNIVERSITY

[email protected], +93 766637774

Advanced Website Deigning

PHP & MySQL <?php

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

?>

2

Course Title: 320 Advanced Web Designing

Course Description: The course provides the basic knowledge and ability to implement a typical WWW application. Basic requirements of setting up a website and hosting it are explained. The module provides an overview to different technologies required to understand the capabilities of the internet as well as its limitations. Different hosting solutions for hosting corporate websites and other web-based systems are also covered. The students would be able to install a PHP & MYSQL on a server, master the basic syntax of PHP & MYSQL, and know typical library functions. They can implement server-side applications with PHP & MYSQL, e.g. process data in a form, create dynamic websites, create and handle sessions and cookies etc.

Reference Book: Learning PHP, MySQL, JavaScript, CSS & HTML5, 3rd Ed.

Chapter 1: Handouts

Introduction to Dynamic Web Content

The Request/Response Procedure

The request/response process consists of a web browser asking the web server to send it a

web page and the server sending back the page. The browser then takes care of displaying

the page (see Figure 1-1).

3

For dynamic web pages, the procedure is a little more involved, because it may bring both

PHP and MySQL into the mix (see Figure 1-2).

The Benefits of PHP, MySQL

PHP stands for preprocessor hypertext. PHP is open source. PHP is simple, which create

dynamic elements on websites.

MySQL is a fast and powerful, yet easy-to-use, database system that offers just about

anything a website would need in order to find and serve up data to browsers. When

PHP allies with MySQL to store and retrieve this data, you have the fundamental parts

required for the development of social networking sites and the beginnings of Web 2.0.

4

And when you bring JavaScript and CSS into the mix too, you have a recipe for building

highly dynamic and interactive websites.

Answers to the Important Questions:

1. What four components (at the minimum) are needed to create a fully dynamic web

page?

Answer: A web server (such as Apache), a server-side scripting language (PHP), a

database (MySQL), and a client-side scripting language (JavaScript).

2. Why does the name MySQL contain the letters SQL? Answer: Like nearly all database engines, MySQL accepts commands in Structured

Query Language (SQL). SQL is the way that every user (including a PHP program)

communicates with MySQL.

3. PHP and JavaScript are both programming languages that generate dynamic results

for web pages. What is their main difference, and why would you use both of them?

Answer: PHP runs on the server, whereas JavaScript runs on the client. PHP can

communicate with the database to store and retrieve data, but it can’t alter the

user’s web page quickly and dynamically. JavaScript has the opposite benefits and

drawbacks.

4. If you encounter a bug (which is rare) in one of the open source tools, how do you

think you could get it fixed?

Answer: Some of these technologies are controlled by companies that accept bug

reports and fix the errors like any software company. But open source software

also depends on a community, so your bug report may be handled by any user who

understands the code well enough. You may someday fix bugs in an open source tool

yourself.

Chapter 2: Handouts

Setting Up a Development Server If you wish to develop Internet applications but don’t have your own development server,

you will have to upload every modification you make to a server somewhere else on the Web

before you can test it.

What Is a WAMP, MAMP, or LAMP? WAMP, MAMP, and LAMP are abbreviations for “Windows, Apache, MySQL, and PHP,” “Mac,

Apache, MySQL, and PHP,” and “Linux, Apache, MySQL, and PHP.” These abbreviations

describe a fully functioning setup used for developing dynamic Internet web pages.

Note: Installing XAMPP is students’ task.

5

Using a Program Editor Choosing an IDE can be a very personal thing, so if you intend to use one, I advise you

to download a couple or more to try them out first; they all either have trial versions

or are free to use, so it won’t cost you anything.

Table 2-1. A selection of PHP IDEs

Questions 1. What is the difference between a WAMP, a MAMP, and a LAMP?

Answer: WAMP stands for “Windows, Apache, MySQL, and PHP”; M in MAMP stands for Mac instead of Windows; and the L in LAMP stands for Linux. They all refer to complete solution for hosting dynamic web pages.

2. What do the IP address 127.0.0.1 and the URL http://localhost have in common? Answer: Both 127.0.0.1 and http://localhost are ways of referring to the local

computer. When a WAMP or MAMP is properly configured, you can type either into a

browser’s address bar to call up the default page on the local server.

3. What is the purpose of an FTP program?

FTP stands for File Transfer Protocol. An FTP program is used to transfer files

back and forth between a client and a server

4. Name the main disadvantage of working on a remote web server.

Answer: It is necessary to FTP files to a remote server in order to update it,

which can substantially increase development time if this action is carried out

many times in a session.

5. Why is it better to use a program editor instead of a plain-text editor?

Answer: Dedicated program editors are smart and can highlight problems in your

code before you even run it.

6

Chapter 3: Handouts

Introduction to PHP PHP is the language that you use to make the server generate dynamic output—output that

is potentially different each time a browser requests a page. I encourage you to develop

your PHP code in one of the IDEs listed in Chapter 2.

Incorporating PHP within HTML

By default, PHP documents end with the extension .php. When a web server encounters this extension in a requested file, it automatically passes it to the PHP processor. Of

course, web servers are highly configurable, and some web developers choose to force

files ending with .htm or .html to also get parsed by the PHP processor, usually because they want to hide the fact that they are using PHP.

PHP Program Syntax

<?php // the first part

// programing instructions goes here….

?> // closing part

A small PHP “Hello World” program might look like Example 3-1.

Example 3-1. Invoking PHP <?php

echo "Hello world";

?>

ALTERNATIVE opening and closing syntax; <?

echo "Hello world";

?>

The Structure of PHP 1. Using Comments

Single line comment

// this is comment

7

Multiline comment

/*this is a section

of multiline comments

which will not be

interpreted */

2. Basic Syntax

Semicolons

The PHP commands ended with semicolon, like this:

$x += 10;

The $ symbol

In PHP, however, you must place a $ in front of all variables. This is required to make the PHP parser faster, as it instantly knows whenever it comes across a

variable. Whether your variables are numbers, strings, or arrays, they should all

look something like those in Example 3-3.

Example 3-3. Three different types of variable assignment

<?php

$mycounter = 1;

$mystring = "Hello";

$myarray = array("One", "Two", "Three");

?>

Variables a. String variables

Example: $username = "Fred Smith";

b. Numeric variables

Examples: $count = 17; $count = 17.5;

c. Arrays

Example: $team = array('Bill', 'Mary', 'Mike', 'Chris', 'Anne');

d. Two-dimensional arrays

Example 3-5. Defining a two-dimensional array <?php

$oxo = array(array('x', ' ', 'o'),

array('o', 'o', 'x'),

array('x', 'o', ' '));

?>

Variables naming rules

When creating PHP variables, you must follow these four rules:

Variable names must start with a letter of the alphabet or the _ (underscore) character.

Variable names can contain only the characters a–z, A–Z, 0–9, and _

(underscore).

8

Variable names may not contain spaces. If a variable must comprise more than one

word, it should be separated with the _ underscore character (e.g., $user_name).

Variable names are case-sensitive. The variable $High_Score is not the same as

the variable $high_score.

Operators

Operators are the mathematical, string, comparison, and logical commands such as

plus, minus, multiply, and divide.

1. Arithmetic operators

+, -, *, /, %, ++, --

2. Assignment operators

=, +=, -=, *=, /=, .=, %=

3. Comparison operators

==, !=, >, <, >=, <=

4. Logical operators

&&(and), ||(Or), (Not), xor(Exclusive or)

Constants

Constants are similar to variables, holding information to be accessed once you define it, its value cannot be altered throughout the program.

For example: pi=3.14 here pi is constant.

Questions 1. What tag is used to cause PHP to start interpreting program code? And what is the short

form of the tag?

2. What are the two types of comment tags?

3. Which character must be placed at the end of every PHP statement?

4. Which symbol is used to preface all PHP variables?

5. What can a variable store?

6. What is the difference between $variable = 1 and $variable == 1?

7. Why do you suppose an underscore is allowed in variable names (e.g., $current_user)

whereas hyphens are not (e.g., $current-user)?

8. Are variable names case-sensitive?

9. Can you use spaces in variable names?

10. How do you convert one variable type to another (say, a string to a number)?

11. What is the difference between ++$j and $j++?

12. Are the operators && and and interchangeable?

13. How can you create a multiline echo or assignment?

14. Can you redefine a constant?

15. What is the difference between the echo and print commands?

See “Chapter 3 Answers” on page 640 in Appendix A for answers to these questions.

9

Chapter 4: Handouts

Control Flow in PHP

Conditionals

Conditionals change the program flow. Conditional statement enable programmer to ask

questions about certain things and respond to the answers you get in different ways.

Conditionals are central to dynamic web pages—the goal of using PHP in the first place—

because they make it easy to create different output each time a page is viewed.

Non-Looping Decision Making Statements

a. The if statement

b. The switch statement

c. The ? operator

The if Statement

General syntax of an If Statement

if(condition)

{

Multiple statements;

}

OR

if(condition)

statement;

10

In Example 4-19, imagine that it is the end of the month and all your bills have been

paid, so you are performing some bank account maintenance.

Example 4-19. An if statement with curly braces <?php

$bank_balance=23;

if ($bank_balance < 100)

{

$money = 1000;

$bank_balance += $money;

}

echo “next line”;

?>

In this example, you are checking your balance to see whether it is less than AFN 100. If

so, you pay yourself AFN 1,000 and then add it to the balance. If the bank balance is AFN

100 or greater, the conditional statement are ignored and program flow skips to the next

line.

The else Statement Sometimes when a conditional is not TRUE, you may not want to continue on to the main

program code immediately but might wish to do something else instead. This is where the

else statement comes in.

11

With an if ... else statement, the first conditional statement is executed if the

condition is TRUE. But if it’s FALSE, the second one is executed. One of the two

choices must be executed. Example 4-20. An if ... else statement with curly braces

<?php

$bank_balance=120;

if ($bank_balance < 100) //false condition

{

$money = 1000;

$bank_balance += $money;

}

else

{

$savings += 50;

$bank_balance −= 50;

}

?>

In this example, now that you have determined that you have AFN 120 which is more than

AFN 100, the else statement is executed, by which you place some of this money into your

savings account.

The elseif Statement In elseif statement, we have more than one conditions and else structure as well.

Sequence of conditions is checked and the true statement is picked up. The following

example explain the scenario.

Example 4-21. An if ... elseif ... else statement with curly braces <?php

$bank_balance=350;

if ($bank_balance < 100)

{

$money = 1000;

$bank_balance += $money;

}

elseif ($bank_balance > 200)

{

$savings += 100;

$bank_balance −= 100;

}

else

{

$savings += 50;

$bank_balance −= 50;

} ?>

12

The switch Statement The switch statement is useful in cases in which one variable or the result of an

expression can have multiple values, which should each trigger a different function. For

example, consider a PHP-driven menu system that passes a single string to the main menu

code according to what the user requests. Let’s say the options are Home, About, News,

Login, and Links, and we set the variable $page to one of these, according to the

User’s input.

Example 4-22. A multiple-line if ... elseif ... statement <?php

if ($page == "Home") echo "You selected Home";

elseif ($page == "About") echo "You selected About";

elseif ($page == "News") echo "You selected News";

elseif ($page == "Login") echo "You selected Login";

elseif ($page == "Links") echo "You selected Links";

?>

If we use a switch statement, the code might look like Example 4-23. Example 4-23. A switch statement

<?php

switch ($page)

{

case "Home":

echo "You selected Home";

break;

case "About":

echo "You selected About";

break;

case "News":

echo "You selected News";

break;

case "Login":

echo "You selected Login";

break;

case "Links":

echo "You selected Links";

break;

}

?>

The ? Operator / Ternary Operator The ? operator is passed an expression that it must evaluate, along with two statements

to execute: one for when the expression evaluates to TRUE, the other for when it is

FALSE.

Example 4-26 shows code we might use for writing a warning about the fuel level of a car

to its digital dashboard.

Example 4-26. Using the ? operator <?php

echo $fuel <= 1 ? "Fill tank now" : "There's enough fuel"; ?>

13

Looping Decision Making Statement

a. While loop

b. Do while loop

c. For loop

while Loops Let’s turn the digital car dashboard in Example 4-26 into a loop that continuously

checks the fuel level as you drive, using a while loop (Example 4-28).

Example 4-28. A while loop <?php

$fuel = 10;

while ($fuel > 1)

{

// Keep driving ...

echo "There's enough fuel";

}

?>

Example 4-29. A while loop to print the 12 times table <?php

$count = 1;

while ($count <= 12)

{

echo "$count times 12 is " . $count * 12 . "<br>";

++$count;

}

?>

do ... while Loops A slight variation to the while loop is the do ... while loop, used when you want a

block of code to be executed at least once and made conditional only after that.

Example 4-31 shows a modified version of the code for the 12 times table that uses such a

loop.

Example 4-31. A do ... while loop for printing the times table for 12 <?php

$count = 1;

do

echo "$count times 12 is " . $count * 12 . "<br>";

while (++$count <= 12);

?>

for Loops The final kind of loop statement, the for loop, is also the most powerful, as it

combines the abilities to set up variables as you enter the loop, test for conditions

while iterating loops, and modify variables after each iteration.

Example 4-33 shows how you could write the multiplication table program with a for loop.

Example 4-33. Outputting the times table for 12 from a for loop <?php

for ($count = 1 ; $count <= 12 ; ++$count)

echo "$count times 12 is " . $count * 12 . "<br>"; ?>

14

Questions 1. Name the three conditional statement types.

Ans: The three conditional statement types are if, switch, and the ?: operator.

2. What command can you use to skip the current iteration of a loop and move on to

the next one?

Ans: To skip the current iteration of a loop and move on to the next one, use a

continue statement.

3. Why is a for loop more powerful than a while loop?

Ans: Loops using for statements are more powerful than while loops, because they

support two additional parameters to control the loop handling.

4. How do if and while statements interpret conditional expressions of different

data types?

Ans: Most conditional expressions in if and while statements are literal (or

Boolean) and therefore trigger execution when they evaluate to TRUE. Numeric

expressions trigger execution when they evaluate to a nonzero value. String

expressions trigger execution when they evaluate to a nonempty string. A NULL

value is evaluated as false and therefore does not trigger execution.

15

Chapter 5: Handouts

PHP Functions

Introduction:

It is difficult programming practice ever, when we have to rewrite portions of very

similar code each time we need them.

To solve this issue, functions come are used.

“A function is set of statements that performs a particular function.”

Advantages of function:

Functions have many advantages over contiguous, inline code. For example, they:

Involve less typing

Reduce syntax and other programming errors

Decrease the loading time of program files

Decrease execution time, because each function is compiled only once, no matter

how often you call it

Accept arguments and can therefore be used for general as well as specific cases

PHP Functions

PHP comes with hundreds of ready-made, built-in functions, making it a very rich

language. To use a function, call it by name. For example, you can see the print unction

in action here:

print("print is a pseudo-function");

Type of functions:

Built-in functions: Already defined functions. For example: print(“string goes here…”); strrev(“.dlrow olleH”); str_repeat(“Hi; “, 3); strtoupper(“Hussain!”); strtolower(“HASHAM”); ucfirst(“gullo”);

User define functions: User define function are used to perform a specific task. It can be define as:

Defining a Function

The general syntax for a function is:

function function_name([parameter [, ...]]) {

// Statements

16

} Function is a keyword. Function names are case-insensitive, so all of the following

strings can refer to the print function: PRINT, Print, and PrInT.

Returning a Value Let’s take a look at a simple function to convert a person’s full name to lowercase and

then capitalize the first letter of each name. We will also introduce a program which

convert a person’s name to upper case.

This program return a value:

<?php

echo fix_names("WILLIAM", "henry", "gatES");

function fix_names($n1, $n2, $n3)

{

$n1 = ucfirst(strtolower($n1));

$n2 = ucfirst(strtolower($n2));

$n3 = ucfirst(strtolower($n3));

return $n1 . " " . $n2 . " " . $n3; //return a value

}

?>

Practice Program: Write a PHP program that accept a person’s

first name and last name via a form text boxes, and then

capitalize the first letter of each name.

Returning an Array The above function returning a single value. There are also ways of getting multiple values from a function. For example:

Example: Returning multiple values in an array <?php

$names = fix_names("WILLIAM", "henry", "gatES");

echo $names[0] . " " . $names[1] . " " . $names[2];

function fix_names($n1, $n2, $n3)

{

$n1 = ucfirst(strtolower($n1));

$n2 = ucfirst(strtolower($n2));

$n3 = ucfirst(strtolower($n3));

return array($n1, $n2, $n3); //return array of values

}

?>

Passing values by Reference If we write the above Example to pass references to all the parameters, and then the

function can modify these directly

Example Returning values from a function by reference <?php

$a1 = "WILLIAM";

$a2 = "henry";

$a3 = "gatES";

echo $a1 . " " . $a2 . " " . $a3 . "<br>";

fix_names($a1, $a2, $a3);

echo $a1 . " " . $a2 . " " . $a3;

function fix_names(&$n1, &$n2, &$n3)

{

$n1 = ucfirst(strtolower($n1));

$n2 = ucfirst(strtolower($n2));

$n3 = ucfirst(strtolower($n3)); } ?>

17

Rather than passing strings directly to the function, you first assign them to variables

and print them out to see their “before” values. Then you call the function as before,

but put an & symbol in front of each parameter, which tells PHP to pass the variables’

references only.

Including and Requiring Files These function are used to pull external programs or scripts in rather than copy and past

the code inside your program.

Using include, we can tell PHP to fetch a particular file and load all its contents.

Including a PHP file <?php

include "library.php";

// Your code goes here

?>

Including a PHP file only once <?php

include_once "library.php";

// Your code goes here

?>

Requiring a PHP file only once <?php

require_once "library.php";

// Your code goes here

?>

Questions 1. What is the main benefit of using a function?

2. How many values can a function return?

3. What is the difference between accessing a variable by name and by reference?

Answers 1. Using functions avoids the need to copy or rewrite similar code sections many

times over by combining sets of statements together so that they can be called by

a simple name.

2. By default, a function can return a single value. But by utilizing arrays,

references, and global variables, any number of values can be returned.

3. When you reference a variable by name, such as by assigning its value to another

variable or by passing its value to a function, its value is copied. The original

does not change when the copy is changed. But if you reference a variable, only a

pointer (or reference) to its value is used, so that a single value is referenced

by more than one name. Changing the value of the reference will change the

original as well.

18

Chapter 6: Handouts

PHP Arrays

Introduction:

A variable is a storage area holding a number or text. The problem is, a variable will

hold only one value.

An array is a special variable, which can store multiple values in one single variable.

If you have a list of items (a list of car names, for example), storing the cars in

single variables could look like this:

$cars1="Saab";

$cars2="Volvo";

$cars3="BMW";

However, what if you want to loop through the cars and find a specific one? And what if

you had not 3 cars, but 300?

The best solution here is to use an array!

An array can hold all your variable values under a single name. And you can access the

values by referring to the array name.

Each element in the array has its own index so that it can be easily accessed.

Kinds of Arrays

In PHP, there are three kind of arrays:

1. Numeric array - An array with a numeric index

2. Associative array - An array where each ID key is associated with a value

3. Multidimensional array - An array containing one or more arrays

Numeric Array A numeric array stores each array element with a numeric index.

19

There are two methods to create a numeric array.

In the following example the index are automatically assigned (the index starts at 0):

$cars=array("Saab","Volvo","BMW","Toyota");

In the following example we assign the index manually:

$cars[0]="Saab";

$cars[1]="Volvo";

$cars[2]="BMW";

$cars[3]="Toyota";

Example: In the following example you access the variable values by referring to the

array name and index: <?php

$cars[0]="Saab";

$cars[1]="Volvo";

$cars[2]="BMW";

$cars[3]="Toyota";

echo $cars[0] . “and” . $cars[1] . " are Swedish cars.";

?>

Associative array In associative array, each ID key is associated with a value.

When storing data about specific named values, a numerical array is not always the best

way to do it.

With associative arrays we can use the values as keys and assign values to them.

In this example we use an array to assign ages to the different persons:

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

This example is the same as example 1, but shows a different way of

creating the array:

$ages['Ali'] = "32";

$ages['Rafiq'] = "30";

$ages['Saad'] = "34";

Example: The ID keys can be used in a script:

<?php

$ages['Peter'] = "32";

$ages['Quagmire'] = "30";

$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";

?>

20

Multidimensional array In a multidimensional array, each element in the main array can also be an array.

And each element in the sub-array can be an array, and so on.

Example

In this example we create a multidimensional array, with automatically assigned

ID keys:

$families = array

(

"Griffin"=>array

(

"Peter",

"Lois",

"Megan"

),

"Quagmire"=>array

(

"Glenn"

),

"Brown"=>array

(

"Cleveland",

"Loretta",

"Junior"

)

);

21

Questions 1. What is the difference between a numeric and an associative array?

2. What is the main benefit of the array keyword?

3. What is the difference between foreach and each?

4. How can you create a multidimensional array?

5. How can you determine the number of elements in an array?

Answers 1. A numeric array can be indexed numerically using numbers or numeric variables.

An associative array uses alphanumeric identifiers to index elements.

2. The main benefit of the array keyword is that it enables you to assign several

values at a time to an array without repeating the array name.

3. Both the each function and the foreach ... as loop construct return elements

from an array; both start at the beginning and increment a pointer to make sure

the next element is returned each time; and both return FALSE when the end of the

array is reached. The difference is that the each function returns just a single

element, so it is usually wrapped in a loop. The foreach ... as construct is

already a loop, executing repeatedly until the array is exhausted or you

explicitly break out of the loop.

4. To create a multidimensional array, you need to assign additional arrays to

elements of the main array.

5. You can use the count function to count the number of elements in an array.

End of Module 1:

Rooh Ul Amin Mashwani

MSc Computer Science (UOP)

MS Project Management (Comsats)