Sie sind auf Seite 1von 21

Project Website

PHP – Part 2

PICTURE

Project Website
What is this lecture about?

● Passwords
● PHP Repetition
● PHP Syntax
– Scope of variables
● Organization issues, mid term exam, …

Project Website
Passwords

● Set up of
– DB
– Samba share (again)
● Please write your
– ID +Name +
– a secret password (max. 8 characters)
on a sheet of paper and give it to me

Project Website
PHP Website – How does it work?

1 Your computer Web Host


Request Request 3
6
2
Internet PHP
Webserver
7 Interpreter
8
Response Response
4 5
HD

1. Enter page address into the web browser, e.g. www.domain.com/page.php


2. Browser sends request for page.php to the webserver
3. Webserver takes request and looks for the specified file
4. Webserver reads the file from the hard drive
5. The webserver detects a php file and invokes the PHP interpreter
6. PHP interpreter executes the code and returns the result to the webserver
7. Webserver returns the result as a HTML file to your browser
8. The browser displays the HTML

Project Website
PHP Hello World in HTML

Filename: index.php
Save this file and and copy it to
any PHP enabled webserver

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php 
echo '<p>Hello World</p>'; 
?> 
 </body>
</html>

Project Website
PHP Hello World

Filename: index.php If you access this file from a web


Save this file and and copy it to browser the source-code will look
any PHP enabled webserver like this:

<html> <html>
 <head> <head>
  <title>PHP Test</title> <title>PHP Test</title>
 </head> </head>
 <body> <body>
 <?php  <p>Hello World</p>
echo '<p>Hello World</p>';  </body>
?>  </html>
 </body>
</html>

Project Website
PHP Syntax – Functions

Example: simple function If you access this file from a web


browser the source-code will look

<html> like this:


<body> <html>
<?php <body>
function writeMyName() { Marc Scheib
echo "Marc Scheib"; </body>
} </html>
writeMyName();
?>
</body>
</html>

Project Website
PHP Syntax – Functions

If you access this file


Example:
from a web browser the
function with one parameter
source-code will look
<html> like this:
<body>
<?php <html>
function writeMyName($fname) <body>
{ Marc Scheib
</body>
echo $fname; </html>
}
writeMyName("Marc Scheib");
?>
</body>
</html>
Project Website
PHP Syntax – Functions

If you access this file


Example:
from a web browser the
Function with return value
source-code will look
<html> like this:
<body>
<?php <html>
function add($x,$y) <body>
{ 1 + 16 = 17
</body>
$total = $x + $y; </html>
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>
Project Website
Scope of Variables

● Variables can be defined anywhere in


your PHP program code
● Scope of variables = where can you
access these variables
● By default variables in PHP have a local
scope
● They can only be accessed in the code
block where they were defined
Project Website
Scope of Variables

If you access this file


Example:
from a web browser the
Function with local variables
source-code will look
<html> like this:
<body>
<?php <html>
$a = 99;
<body>
testVariable();
function testVariable() {
$a is 1
$a = $a + 1; </body>
echo '$a is ' . $a; </html>
}
?>
</body>
</html>

Project Website
Scope of Variables

If you access this file


Example:
from a web browser the
Function with local variables
source-code will look
<html> like this:
<body>
<?php <html>
$a = 99;
<body>
testVariable();
function testVariable() {
$a is 99
$a = $a + 1; </body>
} </html>
echo '$a is ' . $a;
?>
</body>
</html>

Project Website
Local Scope of Variables

● The variable $a defined outside the


function testVariable()
● $a has local scope and cannot be
accessed inside the function
testVariable()

Project Website
Scope of Variables

If you access this file


Example:
from a web browser the
Function with global variables
source-code will look
<html> like this:
<body>
<?php <html>
$a = 99;
<body>
testVariable();
function testVariable() {
$a is 100
global $a; </body>
$a = $a + 1; </html>
echo '$a is ' . $a;
}
?>
</body>
</html>

Project Website
Global Scope of Variables

● The keyword global makes variables also


accessible inside of functions
● Advantage:
– Easy access to all variables defined
anywhere
● Drawback:
– May produce some confusion and is
likely to produce errors
Project Website
Scope of Variables

If you access this file


Example:
from a web browser the
Function with $GLOBALS Array
source-code will look
<html> like this:
<body>
<?php <html>
$a = 99;
<body>
testVariable();
function testVariable() {
$a is 100
$GLOBALS['a'] = $GLOBALS['a'] + 1; </body>
} </html>
echo '$a is ' . $a;
?>
</body>
</html>

Project Website
$GLOBALS Array

● The $GLOBALS array is an associative


array
● The name of the global variable is the
key ('a')
● The content of that variable is the value
of the array element (99)

Project Website
Superglobals

● $GLOBALS is a so called superglobal


● superglobals can be accessed in any
scope
● superglobals exist since PHP version
4.1.0

Project Website
Superglobals

If you access this file


Example:
from a web browser the
Superglobals
source-code may look
<?php like this:
echo $_SERVER['SERVER_ADDR'];
echo "<br>"; 127.0.0.1
echo $_SERVER['SERVER_SOFTWARE'];
echo "<br>"; Apache/1.3.41
echo $_SERVER['SERVER_PROTOCOL']; PHP/5.2.4
echo "<br>";
echo $_SERVER['REQUEST_METHOD']; HTTP/1.1
echo "<br>"; GET
echo $_SERVER['REQUEST_TIME'];
echo "<br>"; 1211834156
echo $_SERVER['DOCUMENT_ROOT'];
?> /Library/WebServer/Doc
uments
Project Website
PHP

● Anything from todays content you might


want to discuss?
● Exercise:
– Enhancing your CV with “include”

Project Website
That's all

Thanks for attention!

Project Website

Das könnte Ihnen auch gefallen