PW-02 PHP Fundamentals
PW-02 PHP Fundamentals
02 – PHP Fundamentals
§ Server Root
§ Document Root
§ Listen Port
§ Loaded Modules (LoadModule)
§ PHP
§ Directory Index
§ index.php
§ Cross-platform:
§ Most PHP code can be processed without alteration on computers
running many different operating systems.
§ For example, a PHP script that runs on Linux generally also runs well
on Windows.
§ HTML-embedded:
§ PHP code can be written in files containing a mixture of PHP
instructions and HTML code.
§ Open source
§ You don’t have to pay in using PHP code to build dynamic websites.
PHP 5 Architecture
§ Zend engine as parser (Andi
Gutmans and Zeev Suraski) Zend Core
ODBC
MySQL
Extension
§ SAPI is a web server GD
PHP Core
API
abstraction layer (bridge IMAP
module between web server SAPI
LDAP
and PHP) …
Web Server
§ PHP components now self
contained (ODBC, Java,
LDAP, etc.)
§ This structure is a good
general design for software
(compare to OSI model, and
middleware applications)
System and Software
Requirements
§ To run PHP code you will need the following
software:
§ A computer with an operating system such as Windows, Mac,
or Linux
§ A PHP-compatible Web server software
§ Apache, Internet Information Server (IIS), or Nginx
§ PHP software
§ Can be downloaded from php.net
$a = 0;
$b = 1;
$var = 'a';
echo $$var; // this line will echo 0
$var = 'b';
echo $$var; // this line will echo 1
?>
PHP Data Types
Data type Description
Boolean Scalar; either True or False
Integer Scalar; a whole number
Float Scalar; a number which may have a decimal place
String Scalar; a series of characters
Array Compound; an ordered map (contains names mapped to
values)
Object Compound; a type that may contain properties and
methods
Resource Special; contains a reference to an external resource, such
as a handler to an open file
NULL Special; may only contain NULL as a value, meaning the
variable; explicitly does not contain any value
Common PHP Operators
§ Assignment § Negation
§= §!
§ Arithmetic § Logic
§ +, -, /, *, % § ||, &&, >, <, ==,
>=, <=, !=,
§ Concatenation ===, !===, and,
§. or
§ Increment
§ ++, --
Functions
<?php
function sum($a, $b = 2) {
// define function content here...
$v = $a + $b + 1;
// optionally put a return value
return $v;
}
$x = sum(4); // calling the function
echo $x; // will prints 7
?>
Variable Scope
§ Local Scope <?php
?>
Variable Scope: Global
<?php
§ Global
Scope $a = 1;
$b = 2;
§ Any variable
used from function sum()
{
outside a global $a, $b;
function $b = $a + $b;
}
?>
Arrays
§ Arrays are lists, or lists of lists, or list of lists of
lists, you get the idea--Arrays can be multi-
dimensional
§ Array elements can be addressed by either by
number or by name (strings)
§ If you want to see the structure of an array, use
the print_r function to recursively print an array
inside of pre tags
Arrays
Defining arrays Accessing arrays
<?php <?php
$arr = array('1','2','3'); echo $arr[0]; // prints 1
$arr[] = '4'; echo $arr[1]; // prints 2
$arr['name']='John Doe'; echo $arr[2]; // prints 3
$arr = array( echo $arr[3]; // prints 4
'name' => 'John Doe');
echo $arr['name'];
?> // prints John Doe
?>
Super Global Arrays
Array Description
$GLOBALS Has a reference to every variable that has global scope in a PHP program. Many
of the variables in it are also in other superglobal arrays
$_SERVER Includes everything sent by server in the HTTP response, such as the name of
the currently executing script, server name, version of HTTP, remote IP address,
and so on. Although most Web server software produces the same server
variables, not all do, and not all server variables necessarily have data in them
$_GET Contains all the querystring variables that were attached to the URL, or
produced as a result of using the GET method
$_POST Contains all the submitted form variables and their data. You use variables from
the $_POST or $_REQUEST arrays extensively in most of your PHP programs.
For example, to make use of a username or password (or any other data)
submitted as part of a form, you'll use PHP variables from the $_REQUEST array
Super Global arrays
Array Description
$_COOKIE Contains all cookies sent to the server by the browser. They are turned into
variables you can read from this array, and you can write cookies to the user's
browser using the setcookie() function. Cookies provide a means of
identifying a user across page requests (or beyond, depending upon when
the cookie expires) and are often used automatically in session handling
$_FILES Contains any items uploaded to the server when the POST method is used.
It's different from the $_POST array because it specifically contains items
uploaded (such as an uploaded image file), not the contents of submitted
form fields
$_ENV Contains data about the environment the server and PHP are operating in,
such as the computer name, operating system, and system drive
$_REQUEST Contains the contents of the $_GET, $_POST, and $COOKIE arrays, all in one
Printing Variables in PHP
PHP uses "echo" or "printf" to provide output to
the standard output (screen)
To print any variable:
<?php
$x = 20;
echo $x; // will print 20
Printing Variables in PHP
You may put double quotes between variable
name to produce the same output
<?php
$x = 20;
echo "$x"; // will print 20
SWITCH- $x = 1;
switch($x) {
CASE
case 0: echo $x; // do 0 statement
break;
case 1: echo $x; // do 1 statement
break;
case 2: echo $x; // do 2 statement
case 3: echo $x; // do 3 statement
break;
default: echo $x; // do default statement
break;
}
// if $x value is 2? What is going to happen?
?>
Loop: FOR
<?php
for($x = 1; $x <= 10; $x++) {
echo $x;
}
// will prints 1 to 10
Loop: WHILE
<?php
$x = 10;
while( $x > 0 ){
echo $x;
$x--;
}
// will prints 10 to 1
Loop: DO-WHILE
<?php
$x = 10;
do {
echo $x;
} while ($x < 9);
// will prints:
// name=John
// age=20
Questions?
Assignment
§ Implements any one mathematical formula into a PHP script
§ Put the formula into a single function and call it from anywhere
outside the function to make it work
§ Make a Microsoft Word .docx document that consists of:
§ The PHP code
§ Explanation for every line of code
§ The screenshot of the program output from the web browser