Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to php, Thesis of Advanced Computer Programming

php - php - php - php

Typology: Thesis

2014/2015

Uploaded on 11/22/2015

YEUNI.CFC
YEUNI.CFC 🇬🇧

5

(1)

1 document

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PHP- Is a powerful Server-side scripting language for creating dynamic and interactive
website. (Scripts: are list of PHP commands which are executed on the server).
PHP is perfectly suited for web Development and can be embedded directly into the
HTML Code (HTML is used mainly for formatting text)
PHP is often used together with Apache (web server) on various operating systems. It
also supports other web servers like ISAPI, ISS and ASP
ISAP-Internet Server Application Program interface
IIS-Internet Information Services
ASP-Active Server Page
PHP files may contain text, HTML tags and Scripts. Scripts in a PHP file are executed on
the server.
PHP supports many database e.g. MySQL, Informix, Oracle, Sybase etc.
PHP is an Open Source Software (OSS)- OSS is a computer software whose sources code
is available under a license (or arrangement such as public domain )that permit users to
change and improve the software and to re-distribute in a modified or un modified form.
PHP- Is a free to download and use.
PHP files are returned to the browser as plain HTML
PHP files have a file extension of “.php” “.php3 “or “.phtml”
PHP runs on different platforms (Windows, Linux, UNIX,)
Writing PHP
You don’t need any special software, except text editor (like Notepad in Windows)
PHP scripting block always start with<?php and end with ?> A PHP scripting
block can be placed anywhere in the document.
Some other servers support scripting block with <? and end ?> However for
maximum compatibility, standard form <?php is recommended.
A PHP file normally contains HTML tags just like an HTML file and some PHP scripting
code.
An example of a simple PHP script which sends the text “Hello World” to the browser
<HTML>
<BODY>
<?php
echo “Hello World”
?>
</BODY>
</HTML>
UNDAMENTALS OF PHP domenica novembre 22, 2015
PAGE - 1 -
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Introduction to php and more Thesis Advanced Computer Programming in PDF only on Docsity!

PHP - Is a powerful Server-side scripting language for creating dynamic and interactive website. ( Scripts: are list of PHP commands which are executed on the server).

PHP is perfectly suited for web Development and can be embedded directly into the HTML Code (HTML is used mainly for formatting text)

PHP is often used together with Apache (web server) on various operating systems. It also supports other web servers like ISAPI, ISS and ASP

  • ISAP-Internet Server Application Program interface
  • IIS-Internet Information Services
  • ASP-Active Server Page

PHP files may contain text, HTML tags and Scripts. Scripts in a PHP file are executed on the server.

PHP supports many database e.g. MySQL, Informix, Oracle, Sybase etc. PHP is an Open Source Software (OSS)- OSS is a computer software whose sources code is available under a license (or arrangement such as public domain )that permit users to change and improve the software and to re-distribute in a modified or un modified form.

PHP- Is a free to download and use. PHP files are returned to the browser as plain HTML PHP files have a file extension of “.php” “.php3 “or “.phtml” PHP runs on different platforms (Windows, Linux, UNIX,)

Writing PHP You don’t need any special software, except text editor (like Notepad in Windows)

  • PHP scripting block always start with A PHP scripting block can be placed anywhere in the document.
  • Some other servers support scripting block with ^ However for

maximum compatibility, standard form <?php is recommended.

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

An example of a simple PHP script which sends the text “Hello World” to the browser

Each code line in PHP must end with a semicolon .The semicolon is a separator and is used to distinguish one set of instruction 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”

To execute your code using any browser, just type http://localhost/ folder/ filename.php.

  • If you have included index.php file in your folder, and you want to preview it, you don,t need to type it on the path.because this file is taken automatically

Comments in PHP

In PHP, we use // to make a single line comment or /*^ and */^ to more a large comment

block.

PHP Variables.

Variables are used for storing values, such as numbers, strings or function results, so that they can be used many times in a script.

All Variables in PHP start with a $ sign symbol. Variables may contain string, numbers or

arrays.

Below, the PHP Scripts assigns the String “Hello World to a variable called $ txt:

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

The following example will out put “Have nice weekends!” if the current day is Friday,

otherwise it will output “Have nice days”;

echo “Have a nice weekend!”; ?>

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

”; echo “Have a nice weekend!”; echo “See you on Monday!”; } ?>

The Elseif Statement If you want to execute some code if one of several condition are true use the else if statement

Syntax if (condition) Code to be executed if condition is true; elseif (condition) Code will be executed if condition is true; else Code will be executed if the condition is false;

Example The following example will output “Have a nice weekend!” if the current day is Friday, and “Have a nice Sunday!” If the current day is Friday otherwise it will output” Have nice days!”

$d=date (“D”); if($d= = “Fri.”) echo “Have nice weekend!”; else if ($d= = “sun”) echo”Have a nice Sunday!”; else echo”Have a nice day!”; ?>

PHP Switch Statement

The switch statement in PHP is used to perform one of several different actions based on

one of several different conditions. The switch statement 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----else if---else code.

Syntax 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 label 1 and label2; }

Example 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.

< body>

$name[2]=”Joel”

echo $name[1]. “ and “. $name[2]. “are” $name[0]. “s neighbors”; ?> The code above will output: John and Joe are Peter’s neighbors

Associative Arrays

An associative array, each ID 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.

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

$ages = array (“Peter”=>32, “Dan”=>30, “Joy”=>34);

Example 2: This example is the same as example 1, but shows a different way of creating the array:

$ages [‘Peter’] = “32”;

$ages [‘Dan’] = “30”; $ages [‘Joy’] = “34”;

The ID keys can be used in a script:

The code above will output: Peter is 32 years old.

Multidimensional arrays

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” ) );

The array above would look like this if written to the output:

Array ( [Griffin] => Array ( [0] => Peter [1]=> Lois [2]=> Megan ) [Quagmire] => Array ( [0] => Gleen ) [Brown] = Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) )

$times = 5; $x = 0; while ($x < $times) { echo "Hello World"; ++$x; }

I will now explain this code. The first two lines are just setting the variables. The $times variable holds the number of times you want to repeat the code. The $x variable is the one which will count the number of times the code has been executed. After these is the WHILE line. This tells the computer to repeat the code while $x is less than $times (or to repeat it until $x is equal to $times). This is followed by the code to be executed which is enclosed in { }.

After the echo line which prints out the text, there is another very important line:

++$x;

What this does is exactly the same as writing:

$x = $x + 1;

It adds one to the value of $x. This code is then repeated (as $x now equals 1). It continues being repeated until $x equals 5 (the value of times) when the computer will then move on to the next part of the code.

Using $x

The variable counting the number of repeats ($x in the above example) can be used for much more than just counting. For example if you wanted to create a web page with all the numbers from 1 to 1000 on it, you could either type out every single one or you could use the following code:

$number = 1000; $current = 0; while ($current < $number) { ++$current; echo "$current
"; }

There are a few things to notice about this code. Firstly, you will notice that I have placed the ++ $current; before the echo statement. This is because, if I didn't do this it would start printing numbers from 0, which is not what we want. The ++$current; line can be placed anywhere in your WHILE loop, it does not matter. It can, of course, add, subtract, multiply, divide or do anthing else to the number as well.

The other reason for this is that, if the ++$current; line was after the echo line, the loop would also stop when the number showed 999 because it would check $current which would equal 1000 (set in the last loop) and would stop, even though 1000 had not yet been printed.

Arrays

Arrays are common to many programing languages. They are special variables which can hold more than one value, each stored in its own numbered 'space' in the array. Arrays are extremely useful, especially when using WHILE loops.

Setting Up An Array

Setting up an array is slightly different to setting up a normal variable. In this example I will set up an array with 5 names in it:

$names[0] = 'John'; $names[1] = 'Paul'; $names[2] = 'Steven'; $names[3] = 'George'; $names[4] = 'David';

As you can see, the parts of an array are all numbered, starting from 0. To add a value to an array you must specify the location in the array by putting a number in [ ].

Reading From An Array

Reading from an array is just the same as putting information in. All you have to do is to refer to the array and the number of the piece of data in the array. So if I wanted to print out the third name I could use the code:

n echo "The third name is $names[2]";

Which would output:

The third name is Steven

Using Arrays And Loops

One of the best uses of a loop is to output the information in an array. For instance if I wanted to print out the following list of names:

Name 1 is John Name 2 is Paul Name 3 is Steven Name 4 is George Name 5 is David

I could use the following code:

$number = 5; $x = 0; while ($x < $number) { $namenumber = $x + 1; echo "Name $namenumber is $names[$x]
"; ++$x; }

As you can see, I can use the variable $x from my loop to print out the names in the array. You may have noticed I am also using the variable $namenumber which is always 1 greater than $x. This is because the array numbering starts from 0, so to number the names correctly in the output I must add one to the actual value.