64
PHP Tutorial By RAY BALIKI && ROY GHORAYEB

PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Embed Size (px)

Citation preview

Page 1: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

PHP Tutorial

By RAY BALIKI&&

ROY GHORAYEB

Page 2: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

DEFINITION

• PHP is a powerful server-side scripting language for creating dynamic and interactive websites.

• PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code.

Page 3: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

What is PHP?• PHP stands for PHP: Hypertext Preprocessor • PHP is a server-side scripting language• 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 (OSS) • PHP is free to download and use

INTRODUCTION

Page 4: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

What is a PHP File?

• PHP files may contain text, HTML tags and scripts

• PHP files are returned to the browser as plain HTML 

• PHP files have a file extension of ".php", ".php3", or ".phtml"

INTRODUCTION

Page 5: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

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 FREE to download from the official PHP resource:

www.php.net • PHP is easy to learn and runs efficiently on the server side

Where to Start?• Install an Apache server on a Windows or Linux machine • Install PHP on a Windows or Linux machine • Install MySQL on a Windows or Linux machine

INTRODUCTION

Page 6: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• This tutorial will not explain how to install PHP, MySQL, or Apache Server.

• If your server supports PHP - you don't need to do anything! You do not need to compile anything or install any extra tools  - just create some .php files in your web directory - and the server will parse them for you. Most web hosts offer PHP support.

• However, if your server does not support PHP, you must install PHP. Below is a link to a good tutorial from PHP.net on how to install PHP5:

http://www.php.net/manual/en/install.php

INSTALLATION

Page 7: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Download PHP• Download PHP for free here: http://

www.php.net/downloads.php

Download MySQL Database• Download MySQL for free here: http://

www.mysql.com/downloads/index.html• Download Apache Server• Download Apache for free here: http://

httpd.apache.org/download.cgi

INSTALLATION

Page 8: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document.

<?php

?>

SYNTAX

Page 9: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.

• Next, we have an example of a simple PHP script which sends the text "Hello World" to the browser:

SYNTAX

Page 10: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.

• There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello World".

<html><body><?php

echo "Hello World";

?></body></html>

SYNTAX

Page 11: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.

<html><body><?php

//This is a comment

/*This is a comment block*/

?></body></html>

SYNTAX

Page 12: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or arrays.

• Below, the PHP script assigns the string "Hello World" to a variable called $txt:

<html><body><?php

$txt="Hello World";echo $txt;

?></body></html>

VARIABLES

Page 13: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• To concatenate two or more variables together, use the dot (.) operator:

• The output of the script above will be: "Hello World 1234".

<html><body><?php

$txt1="Hello World";$txt2="1234";echo $txt1 . " " . $txt2 ;

?></body></html>

VARIABLES

Page 14: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Variable Naming Rules• A variable name must start with a letter or an

underscore "_" • A variable name can only contain alpha-numeric

characters and underscores (a-Z, 0-9, and _ ) • A variable name should not contain spaces. If a

variable name should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

VARIABLESVARIABLES

Page 15: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Arithmetic OperatorsOperator

Description Example Result

+ Addition x=2x+2

4

- Subtraction x=25-x

3

* Multiplication x=4x*5

20

/ Division 15/55/2

32.5

% Modulus (division remainder) 5%210%810%2

120

++ Increment x=5x++

x=6

-- Decrement x=5x--

x=4

OPERATORS

Page 16: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Assignment Operators

Operator Example Is The Same As

= x=y x=y

+= x+=y x=x+y

-= x-=y x=x-y

*= x*=y x=x*y

/= x/=y x=x/y

%= x%=y x=x%y

OPERATORS

Page 17: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Comparison Operators

Operator

Description Example

== is equal to 5==8 returns false

!= is not equal 5!=8 returns true

> is greater than 5>8 returns false

< is less than 5<8 returns true

>= is greater than or equal to 5>=8 returns false

<= is less than or equal to 5<=8 returns true

OPERATORS

Page 18: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Logical Operators

Operator

Description Example

&& and x=6y=3 (x < 10 && y > 1) returns true

|| or x=6y=3 (x==5 || y==5) returns false

! not x=6y=3 !(x==y) returns true

OPERATORS

Page 19: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true

Conditional Statements

Page 20: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.

• The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":

if (condition) code to be executed if condition is true;else code to be executed if condition is false;

If...Else

Page 21: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

<html><body>

<?php

$d=date("D");

if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!";

?>

</body></html>

If...Else

Page 22: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:

<html><body>

<?php

$d=date("D");

if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; }

?>

</body></html>

If...Else

Page 23: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• If you want to select one of many blocks of code to be executed, use the Switch statement.

• The switch statement is used to avoid long blocks of if..elseif..else code.

switch (expression){ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2;}

SWITCH

Page 24: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

This is how it works:• A single expression (most often a variable) is evaluated

once • The value of the expression is compared with the values

for each case in the structure • If there is a match, the code associated with that case is

executed • After a code is executed, break is used to stop the code

from running into the next case • The default statement is used if none of the cases are

true

SWITCH

Page 25: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

<html><body>

<?php

switch ($x){ case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break;default: echo "No number between 1 and 3";}

?>

</body></html>

SWITCH

Page 26: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

What is an array?• When working with PHP, sooner or later,

you might want to create many similar variables.

• Instead of having many similar variables, you can store the data as elements in an array.

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

ARRAYS

Page 27: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• A numeric array stores each element with a numeric ID key.

• There are different ways to create a numeric array.

Example 1• In this example the ID key is automatically assigned:

$names = array("Peter","Quagmire","Joe");

ARRAYS

Page 28: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Example 2

In this example we assign the ID key manually:

The ID keys can be used in a script:

The code above will output:

$names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe";

<?php

$names[0] = "Peter";$names[1] = "Quagmire";$names[2] = "Joe";echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors";

?>

Quagmire and Joe are Peter's neighbors

ARRAYS

Page 29: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this.

In PHP we have the following looping statements:• while - loops through a block of code if and as long as a

specified condition is true • do...while - loops through a block of code once, and

then repeats the loop as long as a special condition is true

• for - loops through a block of code a specified number of times

• foreach - loops through a block of code for each element in an array

LOOPS

Page 30: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The while statement will execute a block of code if and as long as a condition is true.

Example• The following example demonstrates a loop that will

continue to run as long as the variable i is less than, or equal to 5. i will increase by 1 each time the loop runs:

while (condition) code to be executed;

WHILE

Page 31: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

<html><body>

<?php

$i=1;

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

?>

</body></html>

WHILE

Page 32: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.

Example• The following example will increment the value of i at

least once, and it will continue incrementing the variable i as long as it has a value of less than 5:

Do{code to be executed;}while (condition);

DO..WHILE

Page 33: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

<html><body>

<?php

$i=0;

Do { $i++; echo "The number is " . $i . "<br />"; }while ($i<5);

?>

</body></html>

DO..WHILE

Page 34: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The for statement is used when you know how many times you want to execute a statement or a list of statements.

• Note: The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop. If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.

for (initialization; condition; increment){ code to be executed;}

FOR

Page 35: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Example• The following example prints the text "Hello World!" five

times:

<html><body>

<?php

for ($i=1; $i<=5; $i++){ echo "Hello World!<br />";}

?>

</body></html>

FOR

Page 36: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The foreach statement is used to loop through arrays.• For every loop, the value of the current array element is

assigned to $value (and the array pointer is moved by one) - so on the next loop, you'll be looking at the next element.

Example• The following example demonstrates a loop that will print

the values of the given array:

foreach (array as value){ code to be executed;}

FOREACH

Page 37: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

<html><body>

<?php

$arr=array("one", "two", "three");

foreach ($arr as $value){ echo "Value: " . $value . "<br />";}

?>

</body></html>

FOREACH

Page 38: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

A function is a block of code that can be executed whenever we need it.

Creating PHP functions:• All functions start with the word "function()" • Name the function - It should be possible to understand

what the function does by its name. The name can start with a letter or underscore (not a number)

• Add a "{"  - The function code starts after the opening curly brace

• Insert the function code • Add a "}"  - The function is finished by a closing curly

brace

FUNCTIONS

Page 39: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Example• A simple function that writes my name when it is called:

<html><body>

<?php

function writeMyName(){ echo “Angelina Jolie";}

writeMyName();

?>

</body></html>

FUNCTIONS

Page 40: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Use a PHP Function• Now we will use the function in a PHP script:<html>

<body>

<?php

function writeMyName()

{

echo "Angelina Jolie";

}

echo "Hello world!<br />";

echo "My name is ";

writeMyName();

echo ".<br />That's right, ";

writeMyName();

echo " is my name.";

?>

</body>

</html>

FUNCTIONS

Page 41: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Adding parameters • Our first function (writeMyName()) is a very simple

function. It only writes a static string.• To add more functionality to a function, we can add

parameters. A parameter is just like a variable.• You may have noticed the parentheses after the function

name, like: writeMyName(). The parameters are specified inside the parentheses.

Example 1• The following example will write different first names, but

the same last name:

FUNCTIONS

Page 42: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

<html>

<body>

<?php

function writeMyName($fname)

{

echo $fname . " Jolie ";

}

echo "My name is ";

writeMyName("Angelina");

echo "My name is ";

writeMyName("Cindy");

echo "My name is ";

writeMyName("Claudia");

?>

</body>

</html>

FUNCTIONS

Page 43: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Example 2• The following function has two parameters:<html><body><?php

function writeMyName($fname,$punctuation) { echo " Student " .$fname. $punctuation ; }

echo "My name is ";writeMyName("Kai Jim",".");echo "My name is ";writeMyName("Hege","!");echo "My name is ";writeMyName("Ståle","...");

?></body></html>

FUNCTIONS

Page 44: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Return values

Functions can also be used to return values.

Example

<html><body><?php

function add($x,$y) { $total = $x + $y; return $total; }

echo "1 + 16 = " . add(1,16);

?></body></html>

FUNCTIONS

Page 45: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.

Form example:

<html><body>

<form action="welcome.php" method="post">Name: <input type="text" name="name" />Age: <input type="text" name="age" /><input type="submit" />

</form></body></html>

FORM HANDLING

Page 46: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The example HTML page above contains two input fields and a submit button. When the user fills in this form and click on the submit button, the form data is sent to the "welcome.php" file.

• The "welcome.php" file might look like this:<html><body>

Welcome <?php echo $_POST["name"]; ?>.<br />You are <?php echo $_POST["age"]; ?> years old.

</body></html>

FORM HANDLING

Page 47: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The $_GET variable is an array of variable names and values sent by the HTTP GET method.

• The $_GET variable is used to collect values from a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and it has limits on the amount of information to send (max. 100 characters).

&_GET

Page 48: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• Example

• When the user clicks the "Submit" button, the URL sent could look something like this:

<form action="welcome.php" method="get">Name: <input type="text" name="name" />Age: <input type="text" name="age" /><input type="submit" /></form>

http://www.w3schools.com/welcome.php?name=Peter&age=37

&_GET

Page 49: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The "welcome.php" file can now use the $_GET variable to catch the form data (notice that the names of the form fields will automatically be the ID keys in the $_GET array):

Welcome <?php echo $_GET["name"]; ?>.<br />You are <?php echo $_GET["age"]; ?> years old!

&_GET

Page 50: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Why use $_GET?• Note: When using the $_GET variable all variable

names and values are displayed in the URL. So this method should not be used when sending passwords or other sensitive information! However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

• Note: The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters.

&_GET

Page 51: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The $_POST variable is an array of variable names and values sent by the HTTP POST method.

• The $_POST variable is used to collect values from a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Example

<form action="welcome.php" method="post">Enter your name: <input type="text" name="name" />Enter your age: <input type="text" name="age" /><input type="submit" /></form>

&_POST

Page 52: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• When the user clicks the "Submit" button, the URL will not contain any form data, and will look something like this:

• The "welcome.php" file can now use the $_POST variable to catch the form data (notice that the names of the form fields will automatically be the ID keys in the $_POST array):

http://www.w3schools.com/welcome.php

Welcome <?php echo $_POST["name"]; ?>.<br />You are <?php echo $_POST["age"]; ?> years old!

&_POST

Page 53: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Why use $_POST?• Variables sent with HTTP POST are not shown

in the URL • Variables have no length limit • However, because the variables are not

displayed in the URL, it is not possible to bookmark the page.

&_POST

Page 54: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The PHP $_REQUEST variable contains the contents of both $_GET and $_POST.

• The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Example

Welcome <?php echo $_REQUEST["name"]; ?>.<br />You are <?php echo $_REQUEST["age"]; ?> years old!

&_REQUEST

Page 55: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Opening a File• The fopen() function is used to open files in PHP.• The first parameter of this function contains the name of

the file to be opened and the second parameter specifies in which mode the file should be opened:

<html><body><?php

$file=fopen("welcome.txt","r");

?></body></html>

FILE HANDLING

Page 56: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• The file may be opened in one of the following modes:

• Note: If the fopen() function is unable to open the specified file, it returns 0 (false).

Modes Description

r Read only. Starts at the beginning of the file

r+ Read/Write. Starts at the beginning of the file

w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist

w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist

a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist

a+ Read/Append. Preserves file content by writing to the end of the file

x Write only. Creates a new file. Returns FALSE and an error if file already exists

x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists

FILE HANDLING

Page 57: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Example• The following example generates a message if the

fopen() function is unable to open the specified file:

<html><body><?php

$file=fopen("welcome.txt","r") or exit("Unable to open file!");?></body></html>

FILE HANDLING

Page 58: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Closing a File• The fclose() function is used to close an open file:<?php

$file = fopen("test.txt","r");//some code to be executedfclose($file);

?>

FILE HANDLING

Page 59: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

What is MySQL?• MySQL is the most popular open source database

server.• MySQL is a database. A database defines a structure for

storing information.• In a database, there are tables. Just like HTML tables,

database tables contain rows, columns, and cells.• Databases are useful when storing information

categorically. A company may have a database with the following tables: "Employees", "Products", "Customers" and "Orders".

MySQL

Page 60: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• One great thing about MySQL is that it can be scaled down to support embedded database applications. Perhaps it is because of this reputation that many people believe that MySQL can only handle small to medium-sized systems.

• The truth is that MySQL is the de-facto standard database for web sites that support huge volumes of both data and end users (like Friendster, Yahoo, Google).

• you can download MySQL for free here: http://www.mysql.com/downloads/index.html

MySQL

Page 61: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Connecting to a MySQL Database• Before you can access and work with data in a

database, you must create a connection to the database.• In PHP, this is done with the mysql_connect() function.

Syntax

mysql_connect(servername,username,password);

Parameter Description

servername Optional. Specifies the server to connect to. Default value is "localhost:3306"

username Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process

password Optional. Specifies the password to log in with. Default is ""

MySQL

Page 62: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

• Note: There are more available parameters, but the ones listed above are the most important. Visit the full PHP MySQL Reference for more details.

• In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }// some code?>

MySQL

Page 63: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Closing a Connection• The connection will be closed as soon as the script ends.

To close the connection before, use the mysql_close() function.

<?php

$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }// some

codemysql_close($con);

?>

MySQL

Page 64: PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites

Thank You

SLIDES DESIGNED BY US