You are on page 1of 31

Workshop for PHP on IBM i

Part 1

Mike Pavlak
Solutions Consultant
mike.p@zend.com

© All rights reserved. Zend Technologies, Inc.


Workshop Agenda
Time Topic Details
45 Presentation 1 Fundamentals of PHP & HTML
30 Lab 1 Lab Begins
15 Break
45 Presentation 2 Functions and Arrays
30 Lab 2
60 Lunch
60 Presentation 3 DB2 Data Access
30 Lab 3
15 Break
60 Presentation 4 OOP & Program call & Mobile
30 Lab 4

| 2 Modernizing legacy applications on i5 with PHP © All rights reserved. Zend Technologies, Inc. |
PHP Virtual Lab Part 1

45-60 minutes of presentation

Covering...
• PHP Basics
• HTML Tag Language
• HTML Forms
• PHP Iterative structures

© All rights reserved. Zend Technologies, Inc.


What is PHP?
 PHP Hypertext Pre-Processor

 Developed initially by Rasmus Lerdorf in 1994

 Andi Gutmans and Zeev Suraski in 1997

 Thousands of contributors worldwide

 7 million+ developers

 244+ million web sites! (Netcraft 2013)

© All rights reserved. Zend Technologies, Inc.


Top 10 Internet Sites by WW traffic
1. Google.com 6. Wikipedia.com

2. Facebook.com 7. qq.com

3. Youtube.com 8. Linked In

4. Yahoo.com 9. Taobao.com

5. Baidu.com 10. Twitter.com 7 more


in next
10
7
© All rights reserved. Zend Technologies, Inc. Mar 2014
PHP is a Scripting Language
 Not compiled, interpreted

 Easy way to get database info to Web

 Short learning curve as OO is optional

 Forgiving and intuitive code structure

 Similar in nature to Net.Data, not as verbose

© All rights reserved. Zend Technologies, Inc.


Who uses PHP?
• Yahoo, GE, Fiat, Mazda, Disney
• On i, Harris Data, United Rentals, Starbucks, Prada
• Kids coming out of school (low $)
• Open source developers – 10,000+ projects
• Community - strong in knowledge and support
• 7 million+ developers
• IBM
 CMS

• Many, many more!


• PHP is to Java what RPG is to COBOL!
© All rights reserved. Zend Technologies, Inc.
Zend Server Under the Covers
IBM i
ILE Apache:10080 i/OS
• Default PHP file
configuration PASE
FastCGI

HTTP:10080
URL Request *PGM
Server PHP CGI
(FastCGI)
CMD
HTML

Zend Server
DB2 UDB

MySQL
MSSQL Server
| © All rights reserved. Zend Technologies, Inc.
Oracle
Geting Started with PHP on IBM i
12
How PHP works

• PHP Runs on the server


• The primary goal of PHP is to generate something to the
browser it can digest!
 HTML, JavaScript, Flax, XML, Flex, Etc.

• HTML is the primary language of the web so let’s look at


that for a bit...

© All rights reserved. Zend Technologies, Inc.


Fundamentals of HTML (Quick review)

© All rights reserved. Zend Technologies, Inc.


HTML

• The default presentation language of the web!


 Easy to learn

 Think about when you learned interactive IBM i programming


• You learned DDS, right…so you can learn HTML too!
 Tag oriented: <b>bolded text</b>

 Many tags make up the language

 Nested orientation

|
15
Modernizing legacy applications on i5 with PHP © All rights reserved. Zend Technologies, Inc. |
HTML 4 – Structure of an HTML Document

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"


"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>An HTML Document</title>
</head>
<body>
<p>Hello world! </p>
</body>
</html>

|
16
Modernizing legacy applications on i5 with PHP © All rights reserved. Zend Technologies, Inc. |
HTML

• Popular HTML tags


• <table></table> Table definition (looks like a subfile)
• <head></head> Page header
• <title></title> Page Title, usually appears in tab…
• <p></p> Paragraph
• <br /> Carriage return/line break
• Hyperlink via Anchor tag:
 <a href=“myPHPscript.php”>Text that will link</a>

| © All rights reserved. Zend Technologies, Inc.


Modernizing legacy applications on i5 with PHP
17
HTML Example

| © All rights reserved. Zend Technologies, Inc.


Modernizing legacy applications on i5 with PHP
18
What does that look like?

| © All rights reserved. Zend Technologies, Inc.


Modernizing legacy applications on i5 with PHP
19
Fundamentals of PHP

© All rights reserved. Zend Technologies, Inc.


PHP and your two new best friends

| © All rights reserved. Zend Technologies, Inc.


PHP101
21
PHP Script Structure
• PHP script structure
 <?php will start a script

 Optionally ?> terminates block or script

 Semicolon terminates simple code segment

 May embed multiple block of PHP code in single script

 May have multiple PHP lines per line in code file


<html><head><title>This is my script</title></head><body>

<?php
<html><head><title>This is my script</title></head><body>
echo ‘Hello World’;
<?php echo ‘Hello World’; ?>
?>
</body></hmtl>
</body></hmtl>
| © All rights reserved. Zend Technologies, Inc.
PHP101
22
Variables
<?php
• Rules
$field1 = 5;
 Case senstive $field2 = 10.6;
$field3 = $field1 + $field2;
 Begin with $ $field1 = “Hello World”;
• $thisIsMyVariable
?>
• $_AnotherVariable
• $ this is not a variable
 Implicit casting

 Can be re-typed (Dynamically Typed Language)

| © All rights reserved. Zend Technologies, Inc.


PHP101
23
Variables and their types
• Scalar
 Integer
• -2,147,483,648 thru 2,147,483,647
• Supports decimal, octal and hex representation

 Floating-Point
• 1.7E-308 thru 1.7E+308
• 15 digits of decimal precision

 Strings

• Big. Really big. Too big to discuss!

 Boolean
• False is 0, 0.0, false keyword, empty string, object w/no values, null. All
others are true

• Four complex types: Object, array, null and resource


| © All rights reserved. Zend Technologies, Inc.
PHP101
24
Variables…(cont.)

• Scope
 Global – Available everywhere but inside function (sort of)

 Local – Available only in a function, destroyed at end

 Static – Available only in a function, but remains

• Arrays (three types)


 Enumerated

 Associative

 Multi-dimensional

| © All rights reserved. Zend Technologies, Inc.


PHP101
25
Arrays: Value assignment
• Enumerated
 $Animals[0] = ‘Dog’

 $Animals[1] = ‘Cat’

 $Animals[2] = ‘Hamster’

• Associative
 $Barnyard[‘Cow’] = ‘Calf’

 $Barnyard[‘Chicken’] = ‘Chick’

 $Barnyard[‘Horse’] = ‘Foal’

• Multi-dimensional
 $farm[0] = $Animals

 $farm[1] = $Barnyard
| © All rights reserved. Zend Technologies, Inc.
PHP101
26
Strings

• Most of PHP is character strings


• Single quotes
 Variables not expanded

 Heavy use of concatenation operator “.”

 $string1 = ‘This is the value of variable x: ’ . $x

• Double quotes
 Variables interpolation

 $string2 = “This is the value of variable x: $x”

| © All rights reserved. Zend Technologies, Inc.


PHP101
27
Operators
• Most common operators apply (+,-,*,/,etc.)
• Concatenation
 $greeting =‘My name is ’

 DEFINE (TEACHER, ‘Mike Pavlak’) //Note uppercase…

 $salute = $greeting . TEACHER (My name is Mike Pavlak)


• Increment / decrement
 $a++, ++$a ($a = $a + 1)
 $a--, --$a ($a = $a – 1)
• Logical operators
 && and

 || or

 ! not
| © All rights reserved. Zend Technologies, Inc.
PHP101
28
Comments

• A brief comment about comments


• // (C++ style)
 indicates single line comment

 May use at end of line of live code

 Easily comments a line of code

• /* … */ (C Style)
 Looks like CL? Pretty close

 Comment block,

 Can span multiple lines

• Shell style #
| © All rights reserved. Zend Technologies, Inc.
PHP101
29
The dreaded equal sign
• = Single equal sign is assignment
 $X=3

 $Y=$X

 Now $Y = 3

• == Double equal sign is for conditions


 If ($x==$y) { do something} else {do something else}

 If you see single equal in condition, assignment will occur

• === Triple equal sign is exact equal conditions


 $X= 3 (Integer) $Y=3.0 (Float)
 If ($x==$y) will resolve to true

 If ($x===$y) will resolve to false


| © All rights reserved. Zend Technologies, Inc.
PHP101
30
Flow control
• If
 Condition

• Else
 Condition

if ($x == 5) {
One line, no parenthesis…
$x++;
$y++;
if ($x == 5)
}
$x++;
else {
else
$x--;
$x--;
$y--;
}

|
31
Modernizing legacy applications on i5 with PHP © All rights reserved. Zend Technologies, Inc. |
Flow control (cont…)
• While (condition) {
 Do something

• }
• Break and continue

While ($x < $y) {


$x++;
if ($x==5) break;
echo $x;
}

|
33
Modernizing legacy applications on i5 with PHP © All rights reserved. Zend Technologies, Inc. |
Flow control (cont…)
• Foreach
 Iterate over elements in an array
$array1[0]=‘ham’;
$array1[1]=‘salami’;
$array1[2]=‘bologna’;

foreach ($array1 as $index=>$value) {


echo ‘Element ‘ . $index . ‘ is ‘ . $value.’<br />’;
}

Element 0 is ham
Element 1 is salami
Element 2 is bologna

|
34
Modernizing legacy applications on i5 with PHP © All rights reserved. Zend Technologies, Inc. |
End of Part 1

| © All rights reserved. Zend Technologies, Inc.


Modernizing legacy applications on i5 with PHP
35
Workshop Agenda
Time Topic Details
45 Presentation 1 Fundamentals of PHP & HTML
30 Lab 1 Lab Begins
15 Break
45 Presentation 2 Functions and Arrays
30 Lab 2
60 Lunch
60 Presentation 3 DB2 Data Access
30 Lab 3
15 Break
60 Presentation 4 OOP & Program call & Mobile
30 Lab 4

|
36
Modernizing legacy applications on i5 with PHP © All rights reserved. Zend Technologies, Inc. |

You might also like