Presentation is loading. Please wait.

Presentation is loading. Please wait.

Server-Side Scripting with PHP ISYS 475. PHP Manual Website

Similar presentations


Presentation on theme: "Server-Side Scripting with PHP ISYS 475. PHP Manual Website"— Presentation transcript:

1 Server-Side Scripting with PHP ISYS 475

2 PHP Manual Website http://www.php.net/manual/en/

3 Hyper Text Transfer Protocol: Request & Response Web Server Browser HTTP Request HTTP Response Web Application

4 Data Sent with Request and Response Request: – requested URL, cookies, queryString, data from a form, etc. Response: – Web page content in HTML code – Cookies – Etc.

5 PHP Language Syntax

6 PHP Variables Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. Variable names can contain letters, numbers, and underscores.For examples: $num1, $_mySalary Variable names can’t begin with a digit or two underscores. Variables do not have intrinsic types – it depends on the first data assigned to it.

7

8

9 Basic Data Types Integers Doubles: are floating-point numbers Booleans: two possible values either true or false. NULL: is a special type that only has one value: NULL. Strings: are sequences of characters Arrays: are named and indexed collections of other values. Objects: are instances of programmer-defined classes Resources: are special variables that hold references to resources external to PHP (such as database connections).

10

11

12

13 $name="$first_name $last_name"; echo $name; echo "My name is $name " Without using concatenation:

14 Note: echo and print have the same function.

15

16 Math Functions: http://www.php.net/manual/en/ref.math.php Examples: pow ( number $base, number $exp) echo pow(2, 3);

17

18

19

20 Difference between these functions: http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/

21 PHP Superglobal variables Superglobals are built-in variables that are always available in all scopes

22 $_GET An associative array of variables passed to the current script via the URL parameters (queryString).

23

24

25 $_POST An associative array of variables passed to the current script via the HTTP POST method.

26

27 Example: Compute Sum Value 1: Value 2: Sum:

28 Example: computeSum.php <?php $num1=$_POST["num1"]; $num2=$_POST["num2"]; $sum=$num1+$num2; echo " Value 1: "; echo " Value 2: "; echo " Sum: "; ?>

29 Embed PHP code in HTML Using PHP Expression: <?php $num1=$_POST["num1"]; $num2=$_POST["num2"]; $sum=$num1+$num2; ?> Value 1: " /> Value 2: " /> Sum: " />

30 Compute Future Value: Process form with various controls

31 Form Code Enter present value: Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 15-year 30-year

32 PHP Code Example <?php $myPV=$_POST["PV"]; $myRate=$_POST["Rate"]; $myYear=$_POST["Year"]; $FV=$myPV*pow(1+$myRate,$myYear); echo "FutureValue is:". $FV; ?>

33 number_format — Format a number with grouped thousands If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands. If two parameters are given, number will be formatted with decimals with a dot (".") in front, and a comma (",") between every group of thousands.

34 Examples: Comma and Currency format $number = 1234.56; $formated_number = number_format($number); // 1,235 $formated_number = number_format($number,2); // 1,235.56 $formated_number = “$”. number_format($number,2); // $1,235.56 echo "FutureValue is: $". number_format($FV,2); NOTE: money_format() is undefined in Windows.

35 Decision with If statement

36 Note: { } is optional if only one statement

37

38 PHP Loops

39

40 Compute future value uses formula or a loop $FV = $myPV; for ($i = 1; $i <= $myYear; $i++) { $FV= $FV + $FV * $myRate; } Enter present value: Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 20-year 30-year Compute using loop: Future value is :

41 PHP Code <?php $myPV=$_POST["PV"]; $myRate=$_POST["Rate"]; $myYear=$_POST["Year"]; if (isset($_POST["chkLoop"])) { $FV = $myPV; for ($i = 1; $i <= $myYear; $i++) { $FV= $FV + $FV * $myRate; }} else $FV=$myPV*pow(1+$myRate,$myYear); echo "FutureValue is: $". number_format($FV,2); ?> Note: Without checking isset($_POST["chkLoop"]) the IF statement will cause error if the checkbox is not checked.

42 Depreciation Table Straight Line Depreciation Table Enter Property Value: Enter Property Life:

43 Output

44 <?php $value=$_POST["pValue"]; $life=$_POST["pLife"]; echo "Straight Line Depreciation Table "; echo "Property Value: "; echo "Property Life: "; $depreciation=$value/$life; $totalDepreciation=$depreciation; echo " "; echo " Year Value at BeginYr "; echo " Dep During Yr Total to EndOfYr "; echo " "; for ($count = 1; $count <= $life; $count++) { echo " "; echo " $count "; echo " $". number_format($value,2). " "; echo " $". number_format($depreciation,2). " "; echo " $". number_format($totalDepreciation,2). " "; $value -= $depreciation; $totalDepreciation+=$depreciation; } ?>

45 Alternating Row Color <?php $value=$_POST["pValue"]; $life=$_POST["pLife"]; echo "Straight Line Depreciation Table "; echo "Property Value: "; echo "Property Life: "; $depreciation=$value/$life; $totalDepreciation=$depreciation; echo " "; echo " Year Value at BeginYr "; echo " Dep During Yr Total to EndOfYr "; echo " "; for ($count = 1; $count <= $life; $count++) { if ($count%2==0) $color="blue"; else $color="red"; // echo " "; echo " "; echo " $count "; echo " $". number_format($value,2). " "; echo " $". number_format($depreciation,2). " "; echo " $". number_format($totalDepreciation,2). " "; $value -= $depreciation; $totalDepreciation+=$depreciation; } ?>

46 Using PHP Expression <?php $value=$_POST["pValue"]; $life=$_POST["pLife"]; echo "Straight Line Depreciation Table "; echo "Property Value: "; echo "Property Life: "; $depreciation=$value/$life; $totalDepreciation=$depreciation; ?> Year Value at BeginYr Dep During Yr Total to EndOfYr <?php for ($count = 1; $count <= $life; $count++) { ?> <?php $value -= $depreciation; $totalDepreciation+=$depreciation; } ?>

47 Display output with inputs on the same page: Example: sum of two numbers Method 1: – The page should be a php file, not a html page. – The page calls itself to process the data. 2. the php file specified by the action attribute is called to compute the sum and use the include statement to display the sum with the input page

48 Method 1: The PHP page, SumPage.php, calls itself <?php if (!empty($_POST)) //if (count($_POST)>0) { $num1=$_POST["num1"]; $num2=$_POST["num2"]; $sum=$num1+$num2; ?> Value 1: " /> Value 2: " /> Sum: " /> <?php } else { ?> Value 1: Value 2: Sum: <?php } ?> Note: This program uses empty($_POST) to test if the page is loaded for the first time.

49 Display output with inputs on the same page: Example: sum of two numbers Method 2: – The page should be a php file, not a html page. – The page calls a PHP page to process the data which uses an include statement to include the calling page for output

50 PHP include and require Statements: include 'filename'; require 'filename'; In PHP, you can insert the content of one PHP file into another PHP file. The include and require statements are used to insert useful codes written in other files, in the flow of execution. Include and require 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

51 Method 2: The page calls a PHP page with an include statement to process the data <?php if (isset($sum)) { ?> Value 1: " /> Value 2: " /> Sum: " /> <?php } else { ?> Value 1: Value 2: Sum: <?php } ?> Note: This program use isset() function to test if the page is loaded for the first time.

52 computeSum.php <?php $num1=$_POST["num1"]; $num2=$_POST["num2"]; $sum=$num1+$num2; include ("sumForm.php"); exit(); /?>

53 Display future value with the inputs, fvForm2.php <?php if (!empty($_POST)){ $myPV=$_POST["PV"]; $myRate=$_POST["Rate"]; $myYear=$_POST["Year"]; $FV=$myPV*pow(1+$myRate,$myYear); echo " "; echo "Enter present value: "; echo "Select interest rate: "; for ($v=.04; $v<=.08;$v+=.01){ $display=$v*100; if ($v==$myRate) echo " $display% "; else echo " $display% "; } echo " "; echo "Select year: "; for ($v=10; $v<=30;$v+=10){ $display=$v. '-year'; if ($v==$myYear) echo " $display "; else echo " $display "; } $CFV="$". number_format($FV,2); echo "Future value is : "; echo " "; } else { ?> Enter present value: Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 20-year 30-year Future value is :

54 2. Display future value with the inputs, fvForm.php <?php if (isset($FV)){ echo " "; echo "Enter present value: "; echo "Select interest rate: "; for ($v=.04; $v<=.08;$v+=.01){ $display=$v*100; if ($v==$myRate) echo " $display% "; else echo " $display% "; } echo " "; echo "Select year: "; for ($v=10; $v<=30;$v+=10){ $display=$v. '-year'; if ($v==$myYear) echo " $display "; else echo " $display "; } $CFV="$". number_format($FV,2); echo "Future value is : "; echo " "; } else { ?> Enter present value: Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 20-year 30-year Future value is :

55 ComputeFV.php <?php $myPV=$_POST["PV"]; $myRate=$_POST["Rate"]; $myYear=$_POST["Year"]; $FV=$myPV*pow(1+$myRate,$myYear); // echo "FutureValue is: $". number_format($FV,2); include 'fvForm.php'; ?>


Download ppt "Server-Side Scripting with PHP ISYS 475. PHP Manual Website"

Similar presentations


Ads by Google