0% found this document useful (0 votes)
56 views

PW-02 PHP Fundamentals

The document provides an overview of PHP fundamentals including why PHP is commonly used, its architecture, system requirements, variables, data types, operators, functions, variable scope, arrays, and super global arrays. PHP is an open source scripting language used to build dynamic websites that is easy to learn, cross-platform, and allows embedding PHP code within HTML files. It is often paired with the Apache web server and MySQL database.

Uploaded by

Andika Nugraha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

PW-02 PHP Fundamentals

The document provides an overview of PHP fundamentals including why PHP is commonly used, its architecture, system requirements, variables, data types, operators, functions, variable scope, arrays, and super global arrays. PHP is an open source scripting language used to build dynamic websites that is easy to learn, cross-platform, and allows embedding PHP code within HTML files. It is often paired with the Apache web server and MySQL database.

Uploaded by

Andika Nugraha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Web Programming

02 – PHP Fundamentals

§ Aryo Pinandito, ST, M.MT


Web Server Administration
§ Web Server: Apache HTTP Server
§ Why Apache?
§ Extremely popular, ±45% web servers on the Internet.
§ Straightforward method of configuration, uses flat
configuration files. – a httpd.conf file
§ Often paired with the most popular server-side
scripting engine and database,
§ PHP, and
§ MySQL database
Apache Web Server
Configuration
httpd.conf > Configuration: directive

§ Server Root
§ Document Root
§ Listen Port
§ Loaded Modules (LoadModule)
§ PHP

§ Directory Index
§ index.php

§ Virtual Hosts? SSL? Manual? Languages?


PHP
§ A programming language devised by Rasmus Lerdorf in
1994 to build a dynamic and interactive web sites.
§ Formerly named from Personal Home Page but changed
to a recursively named:
§ PHP: Hypertext Preprocessor

§ PHP programs (.php) are run on a server—specifically run


on a Web server as a module.
§ OR... you may also run it manually through the shell

§ PHP often used as a middle-ware to other services on the


internet (e.g accessing data on a database or generating
documents on the fly)
Why PHP?
§ Easy to learn
§ Syntax based on Perl, Java, and C

§ 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

§ For database environment


§ MySQL Database Server
§ Can be downloaded from http://mysql.com
System and Software
Requirements (2)
§ Optional development-related software
§ Any text editor, such as Notepad or Notepad++, Emacs, vi.
§ Or… you may use Adobe Dreamweaver IDE or any other PHP script editor
with PHP syntax highlighting feature to aid you in fixing common syntax
problems that you may encounter during development. This will help you
code PHP script pretty much easier.
§ Web browsers
§ IE, Mozilla Firefox, Google Chrome, Opera
§ Browser with Firebug or Web Developer plugin installed is recommended.
§ Helpers script, PHPMyAdmin
§ PHP-based visual database management for MySQL.
§ PHP Manuals
§ Downloadable from PHP documentation downloads page:
§ http://php.net/download-docs.php
PHP Scripts
§ Typically file ends in .php
§ Separated in files within the <?php … ?> tag
§ php commands can make up an entire file, or
can be contained embedded in html
§ Program lines end in ";" or you get an error
§ Server recognizes embedded script and pass
the script to PHP parser to be executed
§ Result is passed to browser, and php script
source code isn't visible
§ Warning!
Dragons Ahead

§ You may want to turn ON your PHP-enabled web


server to test any of the following
PHP scripts provided
Two Ways
§ You can embed sections of § Or you can call html directly
php inside html: from php code:
<HTML> <?php
<BODY>
<P> echo "<HTML><BODY><P>Hello
<?php World</P></BODY></HTML>";
$myvar = ?>
"Hello World!";
echo $myvar;
?>
</P>
</BODY>
</HTML>
Hello, World!
§ <html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
Variables
<?php
§ Issues concerning
creating variables: // $a is integer
$a = 1;
§ Naming conventions
§ Data type // $a is string
$a = 'text';
§ Scope

§ Variables in PHP are very


// $a is string
$a = "text";
flexible
§ less restriction in using one // $a is array
$a = array('a','b','c');
variables for one or more
datatype at one time ?>
Variables Naming
§ Variable names begin with a <?php
dollar sign ($).
// both are two
§ The first character after the // different variables
dollar sign MUST be a letter or an
underscore. $myVariable = 0;
$myvariable = 1;
§ The remaining characters in the
name may be letters, numbers, ?>
or underscores without a fixed
limit
§ Variables are CASE SENSITIVE
§ treat two variables with the same
name but with different case as
two different variables
Variable of Variable
§ PHP allows you to create variable which contains
another variable.
<?php

$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

§ Any variable function send_data() {


$my_data = "Inside data";
used from echo $my_data;
inside function // echoes $my_data value
}

// displays an error messages


echo $my_data;

?>
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;
}

sum(); // executing sum() function


echo $b; // will echo 3

?>
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

However if you put single quotes between variable


name, PHP will produce its contents literally
<?php
$x = 20;
echo '$x'; // will print $x
Printing Variables Examples
§ <?php § echo $y[0]; // 10
§ $x = 20; § echo "$y[0]"; // 10
§ $y[] = 10; § echo '$y[0]'; // $y[0]
§ $z['name'] = "John Doe"; § echo "Name = ".$z['name'];
// Name = John Doe
§ echo "Name = $z[name]";
§ echo $x; // 20 // Name = John Doe
§ echo "x=$x"; // x=20 § echo $z[name];
§ echo 'x=$x'; // x=$x // -- throw warning
§ echo 'x='.$x; // x=20
§ echo $y; // Array
Conditional Tests: IF-ELSE
<?php <?php
$x = 1; if($x == 2) :
if($x == 1) { // true statements
// true statements else:
} else { // false statements
// false statements endif;
}
Alternative syntax
§ Applies to if, while, for, <?php
foreach, and switch
if ($a == 5):
§ Change the opening brace to a
echo "a equals 5";
colon (:)
§ Change the closing brace to an
else:
endif, endwhile, endfor, echo "a is not 5";
endforeach statement
endif;

<?php if ($x == 5): ?> ?>


x is equal to 5
<?php else: ?>
x is not 5
<?php endif; ?> 07
Conditional
Tests: <?php

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 10, why?


Iteration of Array: FOREACH
<?php
$arr = array('name'=>'John', 'age'=>20);

foreach ($arr as $key => $value) {


echo $key . '=' . $value;
}

// 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

§ Save the document into a file named: pw-tugas-2.docx


§ Send as an email attachment with subject:
§ PW: Tugas 2 – PHP Matematika
§ Before Friday, March 17th, 2017, 22:00 WIB
Assignment 2
§ Buat sebuah form sesuai dengan ketentuan di
kelas.
§ Buat hanya dalam 1 file HTML dan 1 file CSS
§ Zip dalam file bernama: html-css.zip
§ PW: Tugas 3 - Form HTML + CSS
§ Before Friday, March 17th, 2017, 22:00 WIB

You might also like