WS Lec8 PHP Part01 Basics
WS Lec8 PHP Part01 Basics
PHP-Part01-Basics
Lecture 8
● Server-Side Includes (SSI) - Code is embedded in HTML pages, and evaluated on the
server while the pages are being served. Add dynamically generated content to an existing
HTML page, without having to serve the entire page via a CGI (Common Gateway
Interface) program.
● Active Server Pages (ASP, Microsoft) - The ASP engine is integrated into the web server
so it does not require an additional process. It allows programmers to mix code within
HTML pages instead of writing separate programs. (Drawback(?) Must be run on a server
using Microsoft server software.)
● Java Servlets (Sun) - As CGI scripts, they are code that creates documents. These must be
compiled as classes which are dynamically loaded by the web server when they are run.
● Java Server Pages (JSP) - Like ASP, another technology that allows developers to embed
Java in web pages.
Result
Hello World!
Your IP address is 127.0.0.1
● You can find more information about php related tools on:
○ Download PHP for free here: http://www.php.net/downloads.php
○ Download MySQL for free here: http://www.mysql.com/downloads/index.html
○ Download Apache for free here: http://httpd.apache.org/download.cgi
Result (browser):
● The server executes the print and This is going to be ignored by the PHP interpreter.
While this is going to be parsed.
echo statements, substitutes output. This will also be ignored by the PHP preprocessor.
Hello and welcome to my page!
define("__FOO__", "something");
echo foo;
FOO = "anotherthing";
// will generate error page
?>
Result :
something
● Function $arr = array(5 => 43, 32, 56, "b" => 12);
$arr = array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
array(key=>value, key=>value, …)
creates an array foreach ($arr as $key => $value) {
echo $key, '=>', $value, ', ';
● key either an integer or a string }
● value any PHP type ?>
Result:
|TIR|Tires|100|
● You could place a for loop inside |OIL|Oil|10|
another for loop to output the array |SPK|Spark Plugs|4|
elements.
● Note: for loops will be explained in the
next slides
Web Systems [502315-3] Dr. Afnan Bukhari 21
Multi-Dimensional Arrays
<?php
● Or define associative array defining $products = array(
array( ‘Code’ => ‘TIR’,
key=>value for each array ‘Description’ => ‘Tires’,
‘Price’ => 100
element. ),
array( ‘Code’ => ‘OIL’,
‘Description’ => ‘Oil’,
● Then output array elements using ),
‘Price’ => 10
Result:
student mark: 80
student grade: B
Result:
item1
item2
<?php <?php
function square($num) { function takes_array($input) {
return $num * $num; echo "$input[0] + $input[1] = ",
} $input[0]+$input[1];
echo square(2); }
?> $arr = array(1,2);
takes_array($arr);
?>
Result:
4 Result:
1 + 2 = 3
$num = 2;
add_five($num);
echo $num;
?>
● The scope of a variable is the part of the script where the variable can be
referenced/used.
You can have local variables with the same name in different functions, because local
variables are only recognized by the function in which they are declared.
addition();
echo $z;
?>
● To do this, use the static keyword when you first declare the variable:
<?php Result:
function myTest() { 10
static $x = 0; 20
$x = $x + 10; 30
echo $x;
}
Then, each time the function is called, that
myTest(); variable will still have the information it
myTest(); contained from the last time the function
myTest(); was called.
?>
● Including files is very useful when you want to include the same PHP, HTML, or text on multiple
pages of a website.
● The include and require statements are identical, except upon failure:
○ require will produce a fatal error (E_COMPILE_ERROR) and stop the script
○ include will only produce a warning (E_WARNING) and the script will continue
● Syntax:
include 'filename';
or
require 'filename';
<html>
● To include the footer file in a page, use <body>
the include statement:
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php'; ?>
</body>
</html>
that the menu easily can be styled with <h1>Welcome to my home page!</h1>
CSS later): <p>Some text.</p>
<p>Some more text.</p>
</body></html>
51